Initial commit
This commit is contained in:
49
tasks/health_check.yaml
Normal file
49
tasks/health_check.yaml
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
- name: Wait for containers to be healthy
|
||||
community.docker.docker_container_info:
|
||||
name: "{{ app_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 }}"
|
||||
method: GET
|
||||
status_code: "{{ app_health_check_status_codes | default([200, 201, 202]) }}"
|
||||
register: http_health_check
|
||||
until: http_health_check.status in (app_health_check_status_codes | default([200, 201, 202]))
|
||||
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: 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
|
||||
when: app_health_check | default(true)
|
||||
|
||||
- name: Verify deployment success
|
||||
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 }}"
|
||||
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
|
||||
Reference in New Issue
Block a user