# How can I iterate over a dictionary in a task?

**URL:** <https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704>\
**Category:** Ansible Project\
**Created:** [February 10, 2017, 9:22pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704 "2017-02-10T21:22:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [February 10, 2017, 9:22pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/1 "2017-02-10T21:22:27Z")

</div>

Say I have the following dictionary?

`  
war\_files:  
server1:

- file1.war
- file2.war  
server2:
- file1.war
- file2.war
- file3.war

`

Now I want to iterate over this, say a get\_url task:

`

- name: Get the files depending on server name  
get\_url: src=http://some\_host/{{ what to put here so I get each file for the server }}  
when: “{{item.key}} in ansible\_hostname”  
with\_dict: “{{ war\_files }}”

`

IOW, if role is running in server1, it’ll download file1.war and file2.war; if running on server2, it downloads file1.war, fiel2.war, and file3.war. I know there’s probably other ways to do this, but I want to be able to do iterate through all the dictionary values in a single task.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 10, 2017, 9:56pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/2 "2017-02-10T21:56:55Z")

</div>

- name: Get the files depending on server name  
&nbsp;&nbsp;&nbsp;get\_url: src=[http://some](http://some)\_host/{{ item }}  
&nbsp;&nbsp;&nbsp;with\_items: "{{ war\_files[ansible\_hostname] }}"

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [February 10, 2017, 10:24pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/3 "2017-02-10T22:24:33Z")

</div>

Let me as a different question. What If I want to show ALL the files in a debug task, like this

`

- debug: msg=“Key = {{ item.key }} values = {{ item.value }}”  
with\_dict: “{{ war\_files }}”  
`

I want to see an output like this

`"msg" : "Key = server1 value = file1.war" "msg" : "Key = server1 value = file2.war" "msg" : "Key = server2 value = file1.war" "msg" : "Key = server2 value = file2.war" "msg" : "Key = server2 value = file3.war"`

IOW, I want to iterate over the keys, then the values in each key.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 11, 2017, 9:34am UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/4 "2017-02-11T09:34:43Z")

</div>

Your item.value is a list so to list them as you want you will need a loop in a loop.  
Check out loop\_control  
[https://docs.ansible.com/ansible/playbooks\_loops.html#loop-control](https://docs.ansible.com/ansible/playbooks_loops.html#loop-control)

Or you can change you variable structure so you can do this in one loop.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [February 14, 2017, 2:53pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/5 "2017-02-14T14:53:06Z")

</div>

The loop\_control documentation doesn’t make really explain how I can use it in my case.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [February 14, 2017, 4:54pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/6 "2017-02-14T16:54:30Z")

</div>

I posted the same question on Stackoverflow, and got the answer I was looking for. Ugly, but it works.  
[http://stackoverflow.com/questions/42167747/how-to-loop-over-this-dictionary-in-ansible](http://stackoverflow.com/questions/42167747/how-to-loop-over-this-dictionary-in-ansible)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 14, 2017, 5:18pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/7 "2017-02-14T17:18:57Z")

</div>

Or just use loop in a loop with loop control.

Task:  
&nbsp;&nbsp;&nbsp;- include: loop.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: "{{ war\_files }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: outer\_item

And the loop.yml contain the following.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [February 14, 2017, 5:29pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/8 "2017-02-14T17:29:40Z")

</div>

Thanks, I’ll try this too.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 14, 2017, 6:10pm UTC](https://forum.ansible.com/t/how-can-i-iterate-over-a-dictionary-in-a-task/23704/9 "2017-02-14T18:10:09Z")

</div>

Hi,

I created a plugin for more complicated looping, which also covers your  
problem. You can find the plugin here:  
[https://github.com/felixfontein/ansible-dependentloop](https://github.com/felixfontein/ansible-dependentloop)  
With it, your problem could be solved as follows:

- name: Get the files depending on server name  
&nbsp;&nbsp;&nbsp;get\_url: src=[http://some](http://some)\_host/{{ item.1 }}  
&nbsp;&nbsp;&nbsp;when: "{{ item.0 }} in ansible\_hostname"  
&nbsp;&nbsp;&nbsp;with\_dependent:  
&nbsp;&nbsp;&nbsp;- "war\_files.keys()"  
&nbsp;&nbsp;&nbsp;- "war\_files[item.0]"

Cheers,  
Felix
