Search code examples
ansibleansible-2.xansible-inventoryansible-factsansible-template

Iterate Over 2 dictionary in ansible


I Have 2 dictionary:

- Test1:
   1: pass
   2: fail
   3: pass

- Test2:
   1.1.1.1: val1
   2.2.2.2: val2
   3.3.3.3: val3

Condition is when Test1.value contians fail

- name: test
  debug:
    msg: "{{item.1.value}} {{item.1.key}} {{item.0.key}} {{item.0.value}}"
  with_together:
    - "{{Test1}}"
    - "{{Test2}}"
  when: item.0.value == "fail"

This is not working as expected unable to get both key and value of 2 dict in one loop


Solution

  • I achieved this by :
    converting dict to list using filter -> |list
    since both dict of same size I was able to get data of both dict in single loop:

    - name: test
      debug:
        msg: "{{item.0}} {{item.1}} {{item.2}} {{item.3}}"
      with_together:
        - "{{ Test1.values() | list }}"
        - "{{ Test2.values() | list }}"
        - "{{ Test1.keys() | list }}"
        - "{{ Test2.keys() | list }}"
      when: item.0 == "fail"