From 05c933e2e0eab31c7979646992341ced3cf50270 Mon Sep 17 00:00:00 2001 From: patrick Date: Thu, 16 Jul 2026 16:58:57 -0400 Subject: [PATCH] use docker info modules for health checks and allow container name override --- defaults/main.yml | 2 ++ tasks/health_check.yml | 38 ++++++++++++++++---------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 7e9d352..6eba637 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -21,7 +21,9 @@ app_compose_validate: true app_compose_pull: policy app_compose_recreate: auto app_compose_start: true +app_compose_project_name: "{{ app_dir | basename }}" +app_container_name: "{{ app_name }}" app_health_check: true app_health_check_method: docker app_health_check_retries: 30 diff --git a/tasks/health_check.yml b/tasks/health_check.yml index d9a4ee3..46dc88f 100644 --- a/tasks/health_check.yml +++ b/tasks/health_check.yml @@ -1,7 +1,7 @@ --- -- name: Wait for containers to be healthy +- name: Wait for container to be healthy community.docker.docker_container_info: - name: "{{ app_name }}" + name: "{{ app_container_name }}" register: container_info until: container_info.container.State.Health.Status | default('healthy') == 'healthy' retries: "{{ app_health_check_retries | default(30) }}" @@ -21,29 +21,23 @@ when: app_health_check | default(true) and app_health_check_method | default('docker') == 'http' and app_health_check_url is defined ignore_errors: true -- name: Check if all containers are running - ansible.builtin.command: - cmd: docker compose ps --services --filter status=running - chdir: "{{ app_dir }}" - register: running_services - when: app_health_check | default(true) - -- name: Check total services - ansible.builtin.command: - cmd: docker compose ps --services - chdir: "{{ app_dir }}" - register: total_services +- name: Inspect compose project containers + community.docker.docker_host_info: + containers: true + containers_all: true + containers_filters: + label: "com.docker.compose.project={{ app_compose_project_name }}" + register: compose_containers when: app_health_check | default(true) - name: Verify deployment success + vars: + _total: "{{ compose_containers.containers | length }}" + _running: "{{ compose_containers.containers | selectattr('State', 'equalto', 'running') | list | length }}" ansible.builtin.assert: that: - - running_services.stdout_lines | length == total_services.stdout_lines | length - fail_msg: "Not all containers are running. Running: {{ running_services.stdout_lines | length }}, Total: {{ total_services.stdout_lines | length }}" + - compose_containers.containers | length > 0 + - _running == _total + fail_msg: "Not all containers are running. Running: {{ _running }}, Total: {{ _total }}" success_msg: "All containers are running successfully." - when: app_health_check | default(true) and total_services.stdout_lines is defined - -- name: Log deployment status - ansible.builtin.debug: - msg: "Deployment {{ 'successful' if (running_services.stdout_lines | length == total_services.stdout_lines | length) else 'failed' }}" - when: app_health_check | default(true) and total_services.stdout_lines is defined \ No newline at end of file + when: app_health_check | default(true) and compose_containers.containers is defined