Given this list in an ansible variable:
---
hosts:
- address: host1.local
cluster:
name: RED
- address: host2.local
cluster:
name: RED
- address: host3.local
cluster:
name: GREEN
- address: host4.local
cluster:
name: BLUE
I now want to count somehow the number of hosts in each cluster. So I want to end up with:
hosts_per_cluster:
RED: 2
BLUE: 1
GREEN: 1
How would one go about this?
My first attempt was something like this:
- name: Get number of hosts per cluster
set_fact:
hosts_per_cluster[item.cluster.name]={{ hosts_per_cluster[item.cluster.name] | default(0) | int +1 }}
loop: "{{ hosts }}"
That did however not work...
- set_fact:
hpc: "{{ hpc | d({}) | combine({item.0: item.1 | length}) }}"
loop: "{{ _hosts | groupby('cluster.name') }}"
gives
hpc:
BLUE: 1
GREEN: 1
RED: 2
ca: "{{ _hosts | groupby('cluster.name') }}"
gives
ca:
- - BLUE
- - address: host4.local
cluster:
name: BLUE
- - GREEN
- - address: host3.local
cluster:
name: GREEN
- - RED
- - address: host1.local
cluster:
name: RED
- address: host2.local
cluster:
name: RED
and create the lists
cluster: "{{ ca | map('first') }}"
address: "{{ ca | map('last') | map('map', attribute='address') }}"
size: "{{ address | map('length') }}"
gives
cluster: ['BLUE', 'GREEN', 'RED']
address: [['host4.local'], ['host3.local'], ['host1.local', 'host2.local']]
size: [1, 1, 2]
Then, create the dictionary
hpc: "{{ dict(cluster | zip(size)) }}"
gives the same result
hpc:
BLUE: 1
GREEN: 1
RED: 2
Example of a complete playbook for testing
- hosts: localhost
vars:
_hosts:
- address: host1.local
cluster: {name: RED}
- address: host2.local
cluster: {name: RED}
- address: host3.local
cluster: {name: GREEN}
- address: host4.local
cluster: {name: BLUE}
ca: "{{ _hosts | groupby('cluster.name') }}"
cluster: "{{ ca | map('first') }}"
address: "{{ ca | map('last') | map('map', attribute='address') }}"
size: "{{ address | map('length') }}"
hpc2: "{{ dict(cluster | zip(size)) }}"
tasks:
- debug:
var: _hosts | groupby('cluster.name')
- set_fact:
hpc: "{{ hpc | d({}) | combine({item.0: item.1|length}) }}"
loop: "{{ _hosts | groupby('cluster.name') }}"
- debug:
var: hpc
- debug:
msg: |
cluster: {{ cluster }}
address: {{ address }}
size: {{ size }}
- debug:
var: hpc2