---
# Check status of all Docker containers
- name: Check container status
  hosts: "{{ target_host | default('all') }}"
  gather_facts: true

  tasks:
    - name: Get list of running containers
      ansible.builtin.command:
        cmd: docker ps --format "table {{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}\t{{ '{{' }}.Image{{ '}}' }}"
      register: docker_ps
      changed_when: false
      become: "{{ ansible_become | default(false) }}"

    - name: Display running containers
      ansible.builtin.debug:
        msg: |

          === {{ inventory_hostname }} ===
          {{ docker_ps.stdout }}

    - name: Get stopped/exited containers
      ansible.builtin.command:
        cmd: docker ps -a --filter "status=exited" --format "table {{ '{{' }}.Names{{ '}}' }}\t{{ '{{' }}.Status{{ '}}' }}"
      register: docker_exited
      changed_when: false
      become: "{{ ansible_become | default(false) }}"

    - name: Display stopped containers
      ansible.builtin.debug:
        msg: |

          === Stopped containers on {{ inventory_hostname }} ===
          {{ docker_exited.stdout }}
      when: docker_exited.stdout_lines | length > 1

    - name: Get disk usage
      ansible.builtin.command:
        cmd: docker system df
      register: docker_df
      changed_when: false
      become: "{{ ansible_become | default(false) }}"

    - name: Display disk usage
      ansible.builtin.debug:
        msg: |

          === Docker disk usage on {{ inventory_hostname }} ===
          {{ docker_df.stdout }}
