Skip to content

Instantly share code, notes, and snippets.

@rektide
Last active December 15, 2021 16:39
Show Gist options
  • Save rektide/ac35e777b3231509c8a1a0905e050ed6 to your computer and use it in GitHub Desktop.
Save rektide/ac35e777b3231509c8a1a0905e050ed6 to your computer and use it in GitHub Desktop.
Ansible dictionary iteration
---
- hosts: all
vars:
places:
alpha: /home
epsilon: /nopenopenope
tasks:
- name: "changed_when depending on result, in with_items"
shell: "[[ '{{item}}' != 'alpha' ]]"
with_items: "{{ places.keys() }}"
register: places_arr
changed_when: places_arr.rc != 0
failed_when: False
- name: "print places_arr result"
debug:
msg: "{{places_arr.results}}"
- name: "changed_when depending on result, in with_dict"
stat:
path: "{{item.value}}"
with_dict: "{{places}}"
register: places_dict
changed_when: not places_dict.stat.exists
- name: "print places_dict result"
debug:
msg: "{{places_dict.results}}"
- name: "iterate through with_dict, via re-indexing original array (bad way to iterate through with_dict results)"
debug:
msg: "{{ places[item[1]] }} {{ 'existed' if places_dict.results[item[0]].stat.exists else 'didnt exist' }}"
with_indexed_items: "{{ places.keys() }}"
- name: "iterate through with_dict, using result's item property (good way to iterate through with_dict results)"
debug:
msg: "{{ item.item.value }} {{ 'existed' if item.stat.exists else 'didnt exist' }}"
with_items: "{{places_dict.results}}"
# originally i was doing this because i did not see that results included an .item property, which made iterating through the results, as above, hard
- name: "a dict iterated via with with_indexed_items"
stat:
path: "{{ places[item[1]] }}"
with_indexed_items: "{{ places.keys() }}"
register: places_indexed
changed_when: not places_indexed.stat.exists
- name: "print places_indexed result"
debug: msg="{{places_indexed.results}}"
- name: "iterate through indexed results"
debug:
msg: "{{ places[item.item[1]] }} {{ 'existed' if item.stat.exists else 'didnt exist' }}"
with_items: "{{places_indexed.results}}"
@rektide
Copy link
Author

rektide commented Feb 2, 2017

Originally this was an example of me not getting how to iterate though a dictionary & do a changed_when for each entry.
But ok! bcoca told me what I wasn't getting: it's only after looping that results are turned into an array. While looping the register variable is set to the current iterations result.

After understanding how to issue a changed_when, I was still not sure how I would iterate through the results of a with_dict at a latter time, since the results are recorded as an array. I assumed I would still have to use the _with_indexed_items/myDict.keys() form to give me a primary key- an enumerated number, as well as the dict's key as items. But this turns out to not be necesssary: results are stored with a .item property that tells you what the current element is when processing results.

I have filed ansible/ansible#20991 to try to improve the documentation on the first of these issues. And I've elaborated a bit further on techniques for iterating through dicts in this gist, for reference sake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment