# iterating a hash in a task

**URL:** <https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302>\
**Category:** Ansible Project\
**Created:** [August 14, 2013, 12:51pm UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302 "2013-08-14T12:51:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali-Akber\_Saifee](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Ali-Akber\_Saifee](https://forum.ansible.com/u/Ali-Akber_Saifee)\
**Post date:** [August 14, 2013, 12:51pm UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/1 "2013-08-14T12:51:35Z")

</div>

The question was asked a few times on freenode (by myself included). Just sharing my solution in case it’s useful for someone else:  
[https://coderwall.com/p/rxsmvw](https://coderwall.com/p/rxsmvw)

best,  
./A

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 14, 2013, 5:50pm UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/2 "2013-08-14T17:50:56Z")

</div>

Please send a pull request to add something like this to core, but rather than a filter plugin, it really should be a lookup plugin.

with\_hash\_items: ahash

etc

–Michael

---

<div class="post-metadata">

**Author:** ![Ali-Akber\_Saifee](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Ali-Akber\_Saifee](https://forum.ansible.com/u/Ali-Akber_Saifee)\
**Post date:** [August 14, 2013, 11:02pm UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/3 "2013-08-14T23:02:14Z")

</div>

I was hoping you’d say that.

If this functionality is going to be part of core, I feel the following is a better way to access the individual items:

task:

- name: echo
- action: command echo “{{ item.key item.value }}”  
with\_hash\_items: ${var}

… instead of item[0] for key and item[1] for value.

Open to suggestions of course.

best,  
./A

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 15, 2013, 1:42am UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/4 "2013-08-15T01:42:27Z")

</div>

"If this functionality is going to be part of core, I feel the following is a better way to access the individual items:

task:

- name: echo
- action: command echo “{{ item.key item.value }}”  
with\_hash\_items: ${var}"

Public service announcement: Using ${var} is unneccessary here.

with\_hash\_items: varname

You can do this for all lookup plugins.

Nobody should be using variables of form $x or ${x} anymore. They are included for backwards compatibility only.

---

<div class="post-metadata">

**Author:** ![Alan\_Grosskurth](https://avatars.discourse-cdn.com/v4/letter/a/a698b9/32.png) [@Alan\_Grosskurth](https://forum.ansible.com/u/Alan_Grosskurth)\
**Post date:** [August 18, 2013, 6:18am UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/5 "2013-08-18T06:18:08Z")

</div>

Ali-Akber Saifee wrote:

> task:
> 
> - name: echo
> - action: command echo “{{ item.key item.value }}”  
> with\_hash\_items: ${var}

FWIW, I had to do something similar today and ended up using the “dictsort” Jinja2 filter:

task:

- name: echo  
action: command echo “{{ item[0] item[1] }}”  
with\_items: var|dictsort

It even has parameters to control the sort order:

[http://jinja.pocoo.org/docs/templates/#dictsort](http://jinja.pocoo.org/docs/templates/#dictsort)

You do have to user the item[0] and item[1] syntax, though, rather than item.key and item.value.

—Alan

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 18, 2013, 1:03pm UTC](https://forum.ansible.com/t/iterating-a-hash-in-a-task/13302/6 "2013-08-18T13:03:59Z")

</div>

Nice, wasn’t aware of this one!

We should add this to the section where we are discussing loops.
