1

I'm trying to loop through a nested ansible inventory looks like this:

    inventory:
      group_one:
        - name: 'entry-one-a'
          description: 'one-a'
          group_two:
            - name: 'entry-two-aa'
              description: 'two-aa'
              group_three:
                - name: 'entry-three-aaa'
                  description: 'three-aaa'
                - name: 'entry-three-aab'
                  description: 'three-aab'

I've tried it with the following loop, but without success:

  - name: print vars
    ansible.builtin.debug:
      msg: '{{ item }}'
    loop: '{{ inventory.group_one.group_two|subelements("group_three") }}'

Any good idea how to loop through the inventory?

jost84
  • 11
  • 1

1 Answers1

1

Iterate the third loop in the included task, e.g.

shell> cat group_three.yml
- debug:
    msg: "{{ item.0.name }} {{ item.1.name }} {{ item2.name }}"
  loop: "{{ item.1.group_three }}"
  loop_control:
    loop_var: item2
    - include_tasks: group_three.yml
      with_subelements:
        - "{{ inventory.group_one }}"
        - group_two

gives

  msg: entry-one-a entry-two-aa entry-three-aaa
  msg: entry-one-a entry-two-aa entry-three-aab
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • It looks like two files are necessary for this. I found the this [post](https://stackoverflow.com/questions/49772057/ansible-with-subelements-nested-levels/49772324#49772324) which looks easier to implement. – jost84 Nov 15 '21 at 12:20