File tree 1 file changed +12
-6
lines changed
1 file changed +12
-6
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
- name : Count Docker containers on all hosts
3
3
hosts : all
4
- gather_facts : no
4
+ gather_facts : false
5
5
tasks :
6
6
- name : Count running Docker containers on each host
7
7
shell : " docker ps -q | wc -l"
12
12
debug :
13
13
msg : " Host {{ inventory_hostname }} has {{ container_count.stdout }} running Docker containers"
14
14
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
20
26
debug :
21
27
msg : " Total running Docker containers across all hosts: {{ container_counts | sum }}"
You can’t perform that action at this time.
0 commit comments