From f273bb03905e2329806a7503c59a2e12ee5ef41b Mon Sep 17 00:00:00 2001 From: patrick Date: Thu, 16 Jul 2026 23:06:31 -0400 Subject: [PATCH] poll docker compose ps for running+healthy state --- roles/app/tasks/health_check.yml | 44 ++++++++++---------------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/roles/app/tasks/health_check.yml b/roles/app/tasks/health_check.yml index 46dc88f..ed10aeb 100644 --- a/roles/app/tasks/health_check.yml +++ b/roles/app/tasks/health_check.yml @@ -1,14 +1,4 @@ --- -- name: Wait for container to be healthy - community.docker.docker_container_info: - name: "{{ app_container_name }}" - register: container_info - until: container_info.container.State.Health.Status | default('healthy') == 'healthy' - retries: "{{ app_health_check_retries | default(30) }}" - delay: "{{ app_health_check_delay | default(10) }}" - when: app_health_check | default(true) and app_health_check_method | default('docker') == 'docker' - ignore_errors: true - - name: Verify application HTTP endpoint ansible.builtin.uri: url: "{{ app_health_check_url }}" @@ -19,25 +9,19 @@ retries: "{{ app_health_check_retries | default(30) }}" delay: "{{ app_health_check_delay | default(10) }}" 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: 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 +- name: Wait for compose services to be running (and healthy if applicable) + ansible.builtin.command: + cmd: docker compose ps --all --format json + chdir: "{{ app_dir }}" + register: _compose_ps + changed_when: false + retries: "{{ app_health_check_retries | default(30) }}" + delay: "{{ app_health_check_delay | default(10) }}" vars: - _total: "{{ compose_containers.containers | length }}" - _running: "{{ compose_containers.containers | selectattr('State', 'equalto', 'running') | list | length }}" - ansible.builtin.assert: - that: - - 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 compose_containers.containers is defined + _services: "{{ _compose_ps.stdout_lines | map('from_json') | list }}" + until: + - _services | length > 0 + - _services | rejectattr('State', 'equalto', 'running') | list | length == 0 + - _services | selectattr('Health', 'defined') | selectattr('Health', 'in', ['unhealthy', 'starting']) | list | length == 0 + when: app_health_check | default(true) and app_health_check_method | default('docker') == 'docker'