Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I count the number of occurrences of a certain object in a hash that matches a condition in Ansible?

Tags:

ansible

jinja2

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...

like image 979
Krist van Besien Avatar asked Sep 20 '25 20:09

Krist van Besien


1 Answers

  • The task below creates the dictionary
    - set_fact:
        hpc: "{{ hpc | d({}) | combine({item.0: item.1 | length}) }}"
      loop: "{{ _hosts | groupby('cluster.name') }}"

gives

  hpc:
      BLUE: 1
      GREEN: 1
      RED: 2
  • You don't have to iterate the list. Group the cluster names
  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
like image 168
Vladimir Botka Avatar answered Sep 22 '25 18:09

Vladimir Botka