From 4486acfca0c43824f46b9cccab5ef117e7c1dd2e Mon Sep 17 00:00:00 2001 From: saltydk Date: Sat, 21 Oct 2023 03:30:27 +0200 Subject: [PATCH] docker: add network container check fails if network_mode container does not exist or is unhealthy. closes #172 --- .../tasks/docker/create_docker_container.yml | 4 ++++ .../network_container_health_status.yml | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 resources/tasks/docker/network_container_health_status.yml diff --git a/resources/tasks/docker/create_docker_container.yml b/resources/tasks/docker/create_docker_container.yml index 69669e01c2..31413b1d51 100644 --- a/resources/tasks/docker/create_docker_container.yml +++ b/resources/tasks/docker/create_docker_container.yml @@ -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) }}" diff --git a/resources/tasks/docker/network_container_health_status.yml b/resources/tasks/docker/network_container_health_status.yml new file mode 100644 index 0000000000..7375a96953 --- /dev/null +++ b/resources/tasks/docker/network_container_health_status.yml @@ -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')