# complex LOOPS in playbook

**URL:** <https://forum.ansible.com/t/complex-loops-in-playbook/24867>\
**Category:** Ansible Project\
**Created:** [June 30, 2017, 9:58pm UTC](https://forum.ansible.com/t/complex-loops-in-playbook/24867 "2017-06-30T21:58:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Herbet](https://avatars.discourse-cdn.com/v4/letter/f/a88e57/32.png) [@Francois\_Herbet](https://forum.ansible.com/u/Francois_Herbet)\
**Post date:** [June 30, 2017, 9:58pm UTC](https://forum.ansible.com/t/complex-loops-in-playbook/24867/1 "2017-06-30T21:58:49Z")

</div>

Hi Guys,

hope someone can guide me here!

i succesfully get facts from one of my network devices

ansible\_facts:  
interfaces\_ip:  
GigabitEthernet0/0/0/2.1004:  
ipv4:  
172.24.70.157:  
prefix\_length: 30  
GigabitEthernet0/0/0/2.1031:  
ipv4:  
10.255.72.253:  
prefix\_length: 30

[…]

i’ve played with some of the example given here \> [http://docs.ansible.com/ansible/playbooks\_loops.html](http://docs.ansible.com/ansible/playbooks_loops.html) but without success

how can i get the interface [3rd level] linked to an IP adress [5th level] i already know (from host\_vars)

thanks in advance!

\ Francois

---

<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:** [July 3, 2017, 2:47pm UTC](https://forum.ansible.com/t/complex-loops-in-playbook/24867/2 "2017-07-03T14:47:00Z")

</div>

Not pretty, but something like this should work if the IP is only shown once in the facts:

&nbsp;&nbsp;&nbsp;- debug: msg="Interface for {{ item.value.ipv4.keys().0 }} is {{ item.key }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.value.ipv4.keys().0 == '10.255.72.253'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_dict: '{{ ansible\_facts.interfaces\_ip }}'
