# Variable in a variable

**URL:** <https://forum.ansible.com/t/variable-in-a-variable/21734>\
**Category:** Ansible Project\
**Created:** [June 10, 2016, 2:47pm UTC](https://forum.ansible.com/t/variable-in-a-variable/21734 "2016-06-10T14:47:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [June 10, 2016, 2:47pm UTC](https://forum.ansible.com/t/variable-in-a-variable/21734/1 "2016-06-10T14:47:51Z")

</div>

Hi,  
In a Jijna2 template I need a following variable from hostvars.

hostvars['{{project}}-example.com'].ansible\_eth0.ipv4.address

As you can see part of the hostname is dynamical created based  
external variable. I don't think such operation is possible. However,  
I wonder if there is any way to access such a variable.

Thanks,  
Wawrzek

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 10, 2016, 3:21pm UTC](https://forum.ansible.com/t/variable-in-a-variable/21734/2 "2016-06-10T15:21:59Z")

</div>

Have you tried that? We used to use hostvars a lot\* and I'm sure I'd  
have done something like  
this previously.

\* (we got rid of them in the end because if 'project'-example.com was  
down, ansible couldn't run  
&nbsp;&nbsp;&nbsp;&nbsp;on any other boxes. In the end we to solve your sort of problem we  
just added DNS entries for  
&nbsp;&nbsp;&nbsp;&nbsp;those IPs and set vars explicitly in the plays)

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [June 10, 2016, 3:35pm UTC](https://forum.ansible.com/t/variable-in-a-variable/21734/3 "2016-06-10T15:35:14Z")

</div>

> In a Jijna2 template I need a following variable from hostvars.
> 
> hostvars['{{project}}-example.com'].ansible\_eth0.ipv4.address

I'm pretty sure something like

&nbsp;&nbsp;hostvars[project + '-example.com'].ansible\_eth0.ipv4.address

will work. You might need to bracket-ify the whole thing, like

&nbsp;&nbsp;hostvars[project + '-example.com']['ansible\_eth0']['ipv4']['address']

Not sure about that, I don't have a good sense of when Jinja lets you use  
dots to mean keys in a dict, and when it doesn't. And how it knows which  
are variables and which are literals. Anyway, something like that.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [June 10, 2016, 4:05pm UTC](https://forum.ansible.com/t/variable-in-a-variable/21734/4 "2016-06-10T16:05:06Z")

</div>

> \> In a Jijna2 template I need a following variable from hostvars.  
> \>  
> \> hostvars['{{project}}-example.com'].ansible\_eth0.ipv4.address

[...]

> will work. You might need to bracket-ify the whole thing, like
> 
> &nbsp;&nbsp;hostvars[project + '-example.com']['ansible\_eth0']['ipv4']['address']

Seems to work.

Thanks,  
Wawrzek
