# How to loop over hashes having an undefined number of sub-elements?

**URL:** <https://forum.ansible.com/t/how-to-loop-over-hashes-having-an-undefined-number-of-sub-elements/22857>\
**Category:** Ansible Project\
**Created:** [October 21, 2016, 6:31pm UTC](https://forum.ansible.com/t/how-to-loop-over-hashes-having-an-undefined-number-of-sub-elements/22857 "2016-10-21T18:31:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![actionmystique](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/actionmystique/32/1678_2.png) [@actionmystique](https://forum.ansible.com/u/actionmystique)\
**Post date:** [October 21, 2016, 6:31pm UTC](https://forum.ansible.com/t/how-to-loop-over-hashes-having-an-undefined-number-of-sub-elements/22857/1 "2016-10-21T18:31:58Z")

</div>

Hi everyone,

This is a tricky question which I have been unable to solve so far despîte multiple attempts, maybe it is not feasible with Ansible.  
I pull some facts from a networking device, some of them end up in {{ facts.ansible\_facts.ansible\_net\_neighbors }}:

“ansible\_net\_neighbors”: {  
“Gi0/0”: [  
{  
“host”: “IOSv\_L2\_10.actionmystique.net”,  
“port”: “Gi0/1”  
}  
],  
“Gi0/3”: [  
{  
“host”: “IOSv\_Leaf\_13.actionmystique.net”,  
“port”: “Gi0/1.10”  
},  
{  
“host”: “IOSv\_Leaf\_13.actionmystique.net”,  
“port”: “Gi0/1.20”  
},  
{  
“host”: “IOSv\_Leaf\_13.actionmystique.net”,  
“port”: “Gi0/1”  
}  
],  
“Gi0/4”: [  
{  
“host”: “IOSv\_Leaf\_14.actionmystique.net”,  
“port”: “Gi0/1”  
},  
{  
“host”: “IOSv\_Leaf\_14.actionmystique.net”,  
“port”: “Gi0/1.20”  
},  
{  
“host”: “IOSv\_Leaf\_14.actionmystique.net”,  
“port”: “Gi0/1.10”  
}  
],

…  
The number & names of ansible\_net\_neighbors sub-elements “Gin/p” (which stand for GigabitEthernet n/p) are dynamic: we have no clue in advance what they’ll be.  
Same characteristic for the number of host/port duo(s) inside each “Gin/p”

So, how can I loop over all those sub-elements? The tricky part for me is the fact that there is no name for the field “Gin/p”.  
I have unsuccessfully tried the following for instance:

- name: Updating the description of all interfaces  
ios\_config:  
provider: “{{ connections.ssh }}”  
parents:
- “interface {{ item }}”  
lines:
- “description Connected to {{ item.host }} on its port {{ item.port }}”  
with\_dict: “{{ facts.ansible\_facts.ansible\_net\_neighbors }}”  
register: result

Any ingenious trick in mind?
