# help on loop over list of dicts

**URL:** <https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974>\
**Category:** Ansible Project\
**Created:** [December 9, 2015, 7:36pm UTC](https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974 "2015-12-09T19:36:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mgsz](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Mgsz](https://forum.ansible.com/u/Mgsz)\
**Post date:** [December 9, 2015, 7:36pm UTC](https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974/1 "2015-12-09T19:36:52Z")

</div>

Error when try to iterate a list:  
This is what I am using in the playbook:  
…

- debug: msg=“{{ dev\_interfaces[0].name }}”

- name: debug message interface name  
debug: msg=“{{item.name}}”  
with\_items: “{{ dev\_interfaces }}”  
…

When executing, the loop always get error, any suggestion?

TASK [debug msg={{ dev\_interfaces[0].name }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [10.3.146.113] =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“msg”: “Ethernet0”  
},  
“module\_name”: “debug”  
},  
“msg”: “Ethernet0”  
}  
TASK [debug message interface name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [10.3.146.113]: FAILED! =\> {“failed”: true, “msg”: “ERROR! dict object has no element {u’subnet’: u’10.0.0.0/31’, u’peer\_addr’: u’10.0.0.1’, u’name’: u’Ethernet0’, u’mask’: u’255.255.255.254’, u’prefixlen’: 31, u’addr’: u’10.0.0.0’}”}

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [December 10, 2015, 4:16pm UTC](https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974/2 "2015-12-10T16:16:05Z")

</div>

Not sure what you’re doing wrong. Maybe it’s the way you define dev\_interfaces (yeah, it makes no sense why it would work in debug, but not in with\_items, but sometimes that happens).

This worked for me:

`

---

<div class="post-metadata">

**Author:** ![Mgsz](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Mgsz](https://forum.ansible.com/u/Mgsz)\
**Post date:** [December 10, 2015, 5:56pm UTC](https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974/3 "2015-12-10T17:56:19Z")

</div>

Thank you Mike for the reply!  
One of my co-worker helped me out. Basically, I was using nested with\_items in two playbook.  
I have playbook1 running with\_items to include playbook2 with tasks which also trying to use with\_items, so it failed in the 2nd playbook which the with\_items are not looping using new value, still using the items in playbook1.

Ansible team, is what I described correct? I think I may need to redo the playbook with with\_nested see if I can make it work.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 18, 2015, 11:09pm UTC](https://forum.ansible.com/t/help-on-loop-over-list-of-dicts/19974/4 "2015-12-18T23:09:04Z")

</div>

inside the include you need to use `set_fact: outside={{item}}`  
otherwise the other with\_items will overwrite the 'item' var
