# setting facts and nesting vars

**URL:** <https://forum.ansible.com/t/setting-facts-and-nesting-vars/19242>\
**Category:** Ansible Developer\
**Created:** [September 11, 2015, 9:02pm UTC](https://forum.ansible.com/t/setting-facts-and-nesting-vars/19242 "2015-09-11T21:02:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [September 11, 2015, 9:02pm UTC](https://forum.ansible.com/t/setting-facts-and-nesting-vars/19242/1 "2015-09-11T21:02:14Z")

</div>

First post …

In my playbook, I have a variable called mgmt\_network. I want to iterate through ansible\_interfaces and set a fact when ansible\_[item].ipv4.network = mgmt\_network. Then later, use that fact to define a route

vars:  
mgmt\_network = 10.10.10.0

- set\_fact:  
mgmt\_interface: "{{ item }}’  
when: “{{ [‘ansible\_’ + item + ‘.ipv4.network’] }}” == “{{ mgmt\_network }}”  
with\_items: "{{ ansible\_interfaces }}

then later …  
lineinfile: dest=/etc/sysconfig/network-scripts/route-{{mgmt\_interface}} line=“12.10.10.0/24 via dev {{mgmt\_interface}}”

but I’m having trouble with that when statement, so I’m wondering if somebody can lend me a hand in order to understand why this isn’t working.

Thanks for reading.

Arthur

---

<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:** [September 11, 2015, 9:06pm UTC](https://forum.ansible.com/t/setting-facts-and-nesting-vars/19242/2 "2015-09-11T21:06:54Z")

</div>

try

when: hostvars[inventory\_hostname]['ansible\_' +  
item]['ipv4']['network'] == mgmt\_network

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [September 11, 2015, 9:11pm UTC](https://forum.ansible.com/t/setting-facts-and-nesting-vars/19242/3 "2015-09-11T21:11:19Z")

</div>

Brilliant. Exactly what I needed.

Thank you for your time.
