--- # Fix Container Health Checks - name: Check if application stack directory exists stat: path: "{{ app_stack_path }}" register: app_stack_dir - name: Fail if application stack directory doesn't exist fail: msg: "Application stack directory not found at {{ app_stack_path }}" when: not app_stack_dir.stat.exists - name: Copy updated docker-compose.yml to production copy: src: "{{ playbook_dir }}/../../stacks/application/docker-compose.yml" dest: "{{ app_stack_path }}/docker-compose.yml" owner: "{{ ansible_user }}" group: "{{ ansible_user }}" mode: '0644' register: compose_updated - name: Recreate containers with new health checks shell: | cd {{ app_stack_path }} docker compose up -d --force-recreate nginx queue-worker scheduler args: executable: /bin/bash when: compose_updated.changed register: containers_recreated - name: Wait for containers to be healthy shell: | cd {{ app_stack_path }} timeout=120 elapsed=0 while [ $elapsed -lt $timeout ]; do healthy=$(docker compose ps --format json | jq -r '[.[] | select(.Name=="nginx" or .Name=="queue-worker" or .Name=="scheduler") | .Health] | all(.=="healthy" or .=="")') if [ "$healthy" = "true" ]; then echo "All containers are healthy" exit 0 fi sleep 5 elapsed=$((elapsed + 5)) done echo "Timeout waiting for containers to become healthy" docker compose ps exit 1 args: executable: /bin/bash register: health_wait failed_when: false changed_when: false - name: Check final container status shell: | cd {{ app_stack_path }} docker compose ps args: executable: /bin/bash register: final_status - name: Display final container status debug: msg: "{{ final_status.stdout_lines }}" - name: Display summary debug: msg: - "=== Health Check Fix Complete ===" - "Containers recreated: {{ 'Yes' if containers_recreated.changed else 'No (no changes)' }}" - "Health wait result: {{ 'SUCCESS' if health_wait.rc == 0 else 'TIMEOUT or ERROR - check logs' }}"