# Looping through a dictionary

**URL:** <https://forum.ansible.com/t/looping-through-a-dictionary/29392>\
**Category:** Ansible Project\
**Created:** [January 8, 2019, 3:32pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392 "2019-01-08T15:32:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [January 8, 2019, 3:32pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392/1 "2019-01-08T15:32:43Z")

</div>

Hi,  
I have a dictionary and I need to set through it to extract data but cannot figure out how to do it.

Dictionary looks like:

`

`  
“int\_output”: {

`

`

“interfaces”: [  
{  
“ipaddress”: “unassigned”,  
“method”: “NVRAM”,  
“name”: “Vlan1”,  
“ok”: “YES”,  
“protocol”: “down”,  
“status”: “administratively down”  
},  
{  
“ipaddress”: “172.31.35.8”,  
“method”: “NVRAM”,  
“name”: “Vlan99”,  
“ok”: “YES”,  
“protocol”: “up”,  
“status”: “up”  
},  
{  
“ipaddress”: “unassigned”,  
“method”: “NVRAM”,  
“name”: “Vlan252”,  
“ok”: “YES”,  
“protocol”: “down”,  
“status”: “administratively down”  
}

]  
}

I’ve tried using with\_item and now trying with with\_dict but cannot figure out how to make it work.

…

- name: display output  
debug:  
var: “Name is {{ item.name }} and status is {{ item.status }}”  
with\_dict: “{{ int\_output.interfaces[0] }}”

The above gives:

TASK [display output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [burnside-poc-sw8.gw.mcgill.ca]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was : ‘dict object’ has no attribute ‘name’\n\nThe error appears to have been in ‘/show\_int\_brief.yml’: line 32, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n \n - name: display output\n ^ here\n”}

Also tried:

- name: display output  
debug:  
var: “Name is {{ item.name }} and status is {{ item.status }}”  
with\_dict: int\_output.interfaces[0]

Above gives:

TASK [display output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [burnside-poc-sw8.gw.mcgill.ca]: FAILED! =\> {“msg”: “with\_dict expects a dict”}

Any ideas good be appreciated.

---

<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:** [January 8, 2019, 4:12pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392/2 "2019-01-08T16:12:33Z")

</div>

int\_output.interfaces is a list and you need to use with\_items.

&nbsp;&nbsp;- name: display output  
&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: "Name is {{ item.name }} and status is {{ item.status }}"  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ int\_output.interfaces }}"

---

<div class="post-metadata">

**Author:** ![Spiro\_Mitsialis](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Spiro\_Mitsialis](https://forum.ansible.com/u/Spiro_Mitsialis)\
**Post date:** [January 8, 2019, 4:53pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392/3 "2019-01-08T16:53:44Z")

</div>

Thanks it worked.  
I get all this extra debug info. Is there a way to display only the 2nd line: “msg”: “Name is Vlan1 and status is administratively down”?

TASK [display output] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [burnside-poc-sw8.gw.mcgill.ca] =\> (item={‘status’: u’administratively down’, ‘protocol’: u’down’, ‘name’: u’Vlan1’, ‘ok’: u’YES’, ‘ipaddress’: u’unassigned’, ‘method’: u’NVRAM’}) =\> {  
“msg”: “Name is Vlan1 and status is administratively down”  
}  
ok: [burnside-poc-sw8.gw.mcgill.ca] =\> (item={‘status’: u’up’, ‘protocol’: u’up’, ‘name’: u’Vlan99’, ‘ok’: u’YES’, ‘ipaddress’: u’172.31.35.8’, ‘method’: u’NVRAM’}) =\> {  
“msg”: “Name is Vlan99 and status is up”  
}  
ok: [burnside-poc-sw8.gw.mcgill.ca] =\> (item={‘status’: u’administratively down’, ‘protocol’: u’down’, ‘name’: u’Vlan252’, ‘ok’: u’YES’, ‘ipaddress’: u’unassigned’, ‘method’: u’NVRAM’}) =\> {  
“msg”: “Name is Vlan252 and status is administratively down”  
}

---

<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:** [January 8, 2019, 8:35pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392/4 "2019-01-08T20:35:41Z")

</div>

Sure, but it depend on what of the information make you want to have that line.  
Is it because the name is Vlan1 or is it because the status is administratively down or the combination?

The fist element in the list is int\_output.interfaces.0 and the second is int\_output.interfaces.1 and the third is int\_output.interfaces.2, but you can choose which element based on it values, so it depends on your requirement.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 8, 2019, 9:30pm UTC](https://forum.ansible.com/t/looping-through-a-dictionary/29392/5 "2019-01-08T21:30:29Z")

</div>

As Kia Stan Olstad already said, you definitely need to be using with\_items, because int\_output.interfaces is a list (according to the information you have supplied).

To make sure that you are referencing the correct variable, add a debug statement immediately before your "display output’ task that just outputs int\_output:

- debug:  
var: int\_output

The error you are seeing always means that the variable you are using does not contain what you think it contains, that you are using the wrong variable, or that you are incorrectly dereferencing the contents of the variable. The above debug statement should help you decide which.

Regards, K.
