use docker info modules for health checks and allow container name override
This commit is contained in:
+16
-22
@@ -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
|
||||
when: app_health_check | default(true) and compose_containers.containers is defined
|
||||
|
||||
Reference in New Issue
Block a user