Skip to content

Commit

Permalink
docker: add network container check
Browse files Browse the repository at this point in the history
fails if network_mode container does not exist or is unhealthy.
closes #172
  • Loading branch information
saltydk committed Oct 21, 2023
1 parent 568bc60 commit 4486acf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions resources/tasks/docker/create_docker_container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
if (var_prefix is defined)
else role_name }}"

- name: Resources | Tasks | Docker | Create Docker Container | Network Container Health Status
ansible.builtin.include_tasks: "{{ resources_tasks_path }}/docker/network_container_health_status.yml"
when: ('container:' in lookup('vars', _var_prefix + '_docker_network_mode', default=docker_networks_name_common))

- name: Resources | Tasks | Docker | Create Docker Container | Create Docker Container # noqa args[module]
community.docker.docker_container:
auto_remove: "{{ lookup('vars', _var_prefix + '_docker_auto_remove', default=omit) }}"
Expand Down
23 changes: 23 additions & 0 deletions resources/tasks/docker/network_container_health_status.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################################
# Title: Saltbox: Resources | Tasks | Docker | Networked Container Health Status #
# Author(s): salty #
# URL: https://github.com/saltyorg/Saltbox #
# -- #
############################################################################################
# GNU General Public License v3.0 #
############################################################################################
---
- name: Resources | Tasks | Docker | Network Container Health Status | Check health of network linked container
community.docker.docker_container_info:
name: "{{ lookup('vars', _var_prefix + '_docker_network_mode', default=docker_networks_name_common).split(':')[1] }}"
register: docker_info

- name: Resources | Tasks | Docker | Network Container Health Status | Fail if network linked container does not exist
ansible.builtin.fail:
msg: "{{ lookup('vars', _var_prefix + '_docker_container', default=_var_prefix) }} is configured to use {{ lookup('vars', _var_prefix + '_docker_network_mode', default=docker_networks_name_common).split(':')[1] }} for networking but it does not exist"
when: not docker_info.exists

- name: Resources | Tasks | Docker | Network Container Health Status | Fail if network linked container is not healthy
ansible.builtin.fail:
msg: "{{ lookup('vars', _var_prefix + '_docker_container', default=_var_prefix) }} is configured to use {{ lookup('vars', _var_prefix + '_docker_network_mode', default=docker_networks_name_common).split(':')[1] }} for networking but it is not healthy"
when: ('State' not in docker_info.container or 'Health' not in docker_info.container.State or 'Status' not in docker_info.container.State.Health or docker_info.container.State.Health.Status != 'healthy')

0 comments on commit 4486acf

Please sign in to comment.