From ccc8547e4db5eed2d002cb6d9c644a6b3e9f02ca Mon Sep 17 00:00:00 2001 From: Mafyuh <matt@mafyuh.com> Date: Sun, 12 Jan 2025 22:43:32 -0500 Subject: [PATCH] update docker count --- ansible/playbooks/docker-count.yml | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/ansible/playbooks/docker-count.yml b/ansible/playbooks/docker-count.yml index 2434203..585abf4 100644 --- a/ansible/playbooks/docker-count.yml +++ b/ansible/playbooks/docker-count.yml @@ -1,7 +1,7 @@ --- - name: Count Docker containers on all hosts hosts: all - gather_facts: no + gather_facts: false tasks: - name: Count running Docker containers on each host shell: "docker ps -q | wc -l" @@ -12,10 +12,16 @@ debug: msg: "Host {{ inventory_hostname }} has {{ container_count.stdout }} running Docker containers" - - name: Aggregate total count - run_once: true - delegate_to: localhost - vars: - container_counts: "{{ hostvars | map(attribute='container_count.stdout') | map('int') | list }}" +- name: Aggregate total count + hosts: localhost + gather_facts: false + tasks: + - name: Collect container counts from all hosts + set_fact: + container_counts: >- + {{ hostvars | dict2items | selectattr('value.container_count', 'defined') | + map(attribute='value.container_count.stdout') | map('int') | list }} + + - name: Display total running Docker containers debug: msg: "Total running Docker containers across all hosts: {{ container_counts | sum }}"