--- # Update all Docker containers (pull new images and recreate) - name: Update Docker containers hosts: "{{ target_host | default('all') }}" gather_facts: true vars: services: "{{ host_services | default([]) }}" tasks: - name: Display update info ansible.builtin.debug: msg: "Updating {{ services | length }} services on {{ inventory_hostname }}" - name: Pull latest images for each service ansible.builtin.command: cmd: docker compose pull chdir: "{{ docker_data_path }}/{{ item.stack_dir }}" loop: "{{ services }}" loop_control: label: "{{ item.name }}" when: item.enabled | default(true) register: pull_result changed_when: "'Downloaded' in pull_result.stdout" failed_when: false become: "{{ ansible_become | default(false) }}" - name: Recreate containers with new images ansible.builtin.command: cmd: docker compose up -d --remove-orphans chdir: "{{ docker_data_path }}/{{ item.stack_dir }}" loop: "{{ services }}" loop_control: label: "{{ item.name }}" when: item.enabled | default(true) register: up_result changed_when: "'Started' in up_result.stdout or 'Recreated' in up_result.stdout" failed_when: false become: "{{ ansible_become | default(false) }}" - name: Clean up unused images ansible.builtin.command: cmd: docker image prune -af when: prune_images | default(true) changed_when: false become: "{{ ansible_become | default(false) }}"