# Use a variable from another host's variables

**URL:** <https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754>\
**Category:** Ansible Project\
**Created:** [February 23, 2019, 5:53pm UTC](https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754 "2019-02-23T17:53:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adsquaired](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@adsquaired](https://forum.ansible.com/u/adsquaired)\
**Post date:** [February 23, 2019, 5:53pm UTC](https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754/1 "2019-02-23T17:53:20Z")

</div>

Hello all,

I have an inventory file populated with two hosts and I would like to use the value of host B’s variable on host A. I had no luck with using hostvars in the template.

Example inventory file. Note this will be a dynamically generated file.

[host\_a]  
x.x.x.x  
[host\_a:vars]  
eth0=192.168.1.1  
eth1=192.168.2.1

[host\_b]  
x.x.x.x  
[host\_b:vars]  
eth0=192.168.1.2

eth1=192.168.2.2

I want to use 192.168.2.1 from host\_a’s variable in a template that will generate a config file on both host a and b. For example, the config on both hosts will have an entry like this: clustered\_ips = 192.168.2.1, 192.168.2.2

I tried this in the template file but get undefined variable.

{{ hostvars[host\_a][eth0] }}

How can I accomplish this?

Thanks in advance.

Steven

---

<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:** [February 23, 2019, 6:04pm UTC](https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754/2 "2019-02-23T18:04:45Z")

</div>

Without quotes inside they are taken as variables, so host\_a and eth0 is taken as variables.  
So you're looking for this

&nbsp;&nbsp;{{ hostvars['host\_a']['eth0'] }}

---

<div class="post-metadata">

**Author:** ![adsquaired](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@adsquaired](https://forum.ansible.com/u/adsquaired)\
**Post date:** [February 25, 2019, 1:46pm UTC](https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754/3 "2019-02-25T13:46:21Z")

</div>

The issue was not so much with the quoting ( I tried that as well before posting ) but the value host\_a after hostvars. Ansible was looking for the IP address under the host heading.  
Ex. {{ hostvars[‘x.x.x.x’][eth0] }}

Is there another way to do this? I would prefer not to put the IP or a DNS name in the template so real values don’t get checked into source code.

Thanks,

---

<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:** [February 25, 2019, 2:02pm UTC](https://forum.ansible.com/t/use-a-variable-from-another-hosts-variables/29754/4 "2019-02-25T14:02:22Z")

</div>

> The issue was not so much with the quoting ( I tried that as well before  
> posting ) but the value host\_a after hostvars. Ansible was looking for the  
> IP address under the host heading.  
> Ex. {{ hostvars['x.x.x.x'][eth0] }}

Of course, is't a group name.

> Is there another way to do this? I would prefer not to put the IP or a DNS  
> name in the template so real values don't get checked into source code.

If you only have one host in the group this should work.

{{ hostvars[group['host\_a'][0]]['eth0'] }}
