Skip to content

Commit ccc8547

Browse files
author
Mafyuh
committedJan 13, 2025
update docker count
1 parent 5410fd2 commit ccc8547

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed
 

‎ansible/playbooks/docker-count.yml

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: Count Docker containers on all hosts
33
hosts: all
4-
gather_facts: no
4+
gather_facts: false
55
tasks:
66
- name: Count running Docker containers on each host
77
shell: "docker ps -q | wc -l"
@@ -12,10 +12,16 @@
1212
debug:
1313
msg: "Host {{ inventory_hostname }} has {{ container_count.stdout }} running Docker containers"
1414

15-
- name: Aggregate total count
16-
run_once: true
17-
delegate_to: localhost
18-
vars:
19-
container_counts: "{{ hostvars | map(attribute='container_count.stdout') | map('int') | list }}"
15+
- name: Aggregate total count
16+
hosts: localhost
17+
gather_facts: false
18+
tasks:
19+
- name: Collect container counts from all hosts
20+
set_fact:
21+
container_counts: >-
22+
{{ hostvars | dict2items | selectattr('value.container_count', 'defined') |
23+
map(attribute='value.container_count.stdout') | map('int') | list }}
24+
25+
- name: Display total running Docker containers
2026
debug:
2127
msg: "Total running Docker containers across all hosts: {{ container_counts | sum }}"

0 commit comments

Comments
 (0)
Please sign in to comment.