# Variable name includes a variable

**URL:** <https://forum.ansible.com/t/variable-name-includes-a-variable/19754>\
**Category:** Ansible Project\
**Created:** [November 12, 2015, 11:39pm UTC](https://forum.ansible.com/t/variable-name-includes-a-variable/19754 "2015-11-12T23:39:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 12, 2015, 11:39pm UTC](https://forum.ansible.com/t/variable-name-includes-a-variable/19754/1 "2015-11-12T23:39:38Z")

</div>

This is something I have been struggling with for a while: a variable whose name includes a variable. For instance, Ansible facts for a server includes the following information on the network interfaces:

“ansible\_eth0”: {  
“active”: true,  
“device”: “eth0”,  
“ipv4”: {  
“address”: “10.105.12.24”,  
“netmask”: “255.255.252.0”,  
“network”: “10.105.12.0”  
},  
“macaddress”: “00:50:56:92:3c:b2”,  
“module”: “vmxnet3”,  
“mtu”: 1500,  
“promisc”: false,  
“type”: “ether”  
},  
“ansible\_eth1”: {  
“active”: true,  
“device”: “eth1”,  
“ipv4”: {  
“address”: “10.104.24.25”,  
“netmask”: “255.255.255.248”,  
“network”: “10.104.24.24”  
},  
“macaddress”: “00:50:56:92:6a:40”,  
“module”: “vmxnet3”,  
“mtu”: 1500,  
“promisc”: false,  
“type”: “ether”  
},  
“ansible\_interfaces”: [  
“lo”,  
“eth1”,  
“eth0”  
],

In a playbook, I want to walk through the list of interfaces and extract information about each one:

- debug: msg="interface = {{item}}, address = {{ansible\_{{item}}.ipv4.address}}  
with\_items: ansible\_interfaces  
when: ‘item != “lo”’

This, of course, does not work because of the nested delimiters. So how do I call out something like this?  
-Mark

---

<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:** [November 13, 2015, 6:36am UTC](https://forum.ansible.com/t/variable-name-includes-a-variable/19754/2 "2015-11-13T06:36:57Z")

</div>

mustaches don't stack, to do dynamic vars you need to use hostvars:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="interface = {{item}}, address = {{  
hostvars[inventory\_hostname]['ansible\_' + item].ipv4.address }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: ansible\_interfaces  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: 'item != "lo"'

---

<div class="post-metadata">

**Author:** ![mtovey](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@mtovey](https://forum.ansible.com/u/mtovey)\
**Post date:** [November 13, 2015, 3:52pm UTC](https://forum.ansible.com/t/variable-name-includes-a-variable/19754/3 "2015-11-13T15:52:37Z")

</div>

I knew it would be simple if I just asked! That worked perfectly!  
Thanks!  
-Mark
