iac/ansible/playbooks/docker-count.yml

28 lines
905 B
YAML
Raw Normal View History

2024-12-20 01:45:42 -05:00
---
- name: Count Docker containers on all hosts
hosts: all
2025-01-12 22:43:32 -05:00
gather_facts: false
2024-12-20 01:45:42 -05:00
tasks:
- name: Count running Docker containers on each host
shell: "docker ps -q | wc -l"
register: container_count
changed_when: false
- name: Display count for each host
debug:
msg: "Host {{ inventory_hostname }} has {{ container_count.stdout }} running Docker containers"
2025-01-12 22:43:32 -05:00
- 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
2024-12-20 01:45:42 -05:00
debug:
msg: "Total running Docker containers across all hosts: {{ container_counts | sum }}"