use docker info modules for health checks and allow container name override

This commit is contained in:
2026-07-16 16:58:57 -04:00
parent eba541a2e2
commit 05c933e2e0
2 changed files with 18 additions and 22 deletions
+2
View File
@@ -21,7 +21,9 @@ app_compose_validate: true
app_compose_pull: policy app_compose_pull: policy
app_compose_recreate: auto app_compose_recreate: auto
app_compose_start: true app_compose_start: true
app_compose_project_name: "{{ app_dir | basename }}"
app_container_name: "{{ app_name }}"
app_health_check: true app_health_check: true
app_health_check_method: docker app_health_check_method: docker
app_health_check_retries: 30 app_health_check_retries: 30
+16 -22
View File
@@ -1,7 +1,7 @@
--- ---
- name: Wait for containers to be healthy - name: Wait for container to be healthy
community.docker.docker_container_info: community.docker.docker_container_info:
name: "{{ app_name }}" name: "{{ app_container_name }}"
register: container_info register: container_info
until: container_info.container.State.Health.Status | default('healthy') == 'healthy' until: container_info.container.State.Health.Status | default('healthy') == 'healthy'
retries: "{{ app_health_check_retries | default(30) }}" 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 when: app_health_check | default(true) and app_health_check_method | default('docker') == 'http' and app_health_check_url is defined
ignore_errors: true ignore_errors: true
- name: Check if all containers are running - name: Inspect compose project containers
ansible.builtin.command: community.docker.docker_host_info:
cmd: docker compose ps --services --filter status=running containers: true
chdir: "{{ app_dir }}" containers_all: true
register: running_services containers_filters:
when: app_health_check | default(true) label: "com.docker.compose.project={{ app_compose_project_name }}"
register: compose_containers
- name: Check total services
ansible.builtin.command:
cmd: docker compose ps --services
chdir: "{{ app_dir }}"
register: total_services
when: app_health_check | default(true) when: app_health_check | default(true)
- name: Verify deployment success - name: Verify deployment success
vars:
_total: "{{ compose_containers.containers | length }}"
_running: "{{ compose_containers.containers | selectattr('State', 'equalto', 'running') | list | length }}"
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- running_services.stdout_lines | length == total_services.stdout_lines | length - compose_containers.containers | length > 0
fail_msg: "Not all containers are running. Running: {{ running_services.stdout_lines | length }}, Total: {{ total_services.stdout_lines | length }}" - _running == _total
fail_msg: "Not all containers are running. Running: {{ _running }}, Total: {{ _total }}"
success_msg: "All containers are running successfully." success_msg: "All containers are running successfully."
when: app_health_check | default(true) and total_services.stdout_lines is defined when: app_health_check | default(true) and compose_containers.containers 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