Ansible – Getting List Values from within a Dictionary (Regsiter Variable)

As the output of registered in section1_1 is a list of dictionaries. We can loop through each item, to get the dictionary keys.


- name: Get the first repo's repoid and state
    msg: "Repo ID: {{ results[0]['repoid'] }}, is {{ results[0]['state'] }}"
# This will show -- Repo ID: ansible, is enabled

Similarly we can access other elements with their number.

Or we can loop through each element of array:

- name: loop through array and conditionally do something
    msg: "Repo ID is {{ item.repoid }}, so I am going to write a playbook."
  when: item.repoid == 'ansible'
  loop: "{{ results }}"

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top