help on loop over list of dicts

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’}”}

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:

`

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.

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