# Looking up variables in another host's host\_var file

**URL:** <https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613>\
**Category:** Ansible Project\
**Created:** [October 26, 2015, 5:00am UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613 "2015-10-26T05:00:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 26, 2015, 5:00am UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/1 "2015-10-26T05:00:19Z")

</div>

So, I have a bit of an odd one here. Is it possible for host A to read values defined in the host\_vars files for hosts X, Y, and Z? I’m trying to render templates on host A using information defined for X, Y, and Z. I _could_ move all that information into a top-level group file, and do a dict keyed off of host name, but I’d rather not. Ideas?

---

<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:** [October 26, 2015, 6:05pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/2 "2015-10-26T18:05:59Z")

</div>

through the hostvars variable you should have access to all vars from all hosts.

{{ hostvars['X']['vardefinedinhostsfile']}}

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 26, 2015, 6:43pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/3 "2015-10-26T18:43:00Z")

</div>

> through the hostvars variable you should have access to all vars from all  
> hosts.
> 
> {{ hostvars['X']['vardefinedinhostsfile']}}

Cool, thanks!

> From initial testing, it seems that only includes the host\_vars for hosts that

in the current run. In other words, if host A is in the vm group, and hosts  
X, Y, ans Z are in other groups (my use case; X/Y/Z are guests) which are not  
being contacted on this run, then their variables won't be in hostvars? Any  
way to add their variables to hostvars even when they aren't being contacted?  
is there a lookup I can do? Maybe a custom lookup plugin to get information  
about them even when they aren't in the current run?

j

---

<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:** [October 26, 2015, 11:50pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/4 "2015-10-26T23:50:40Z")

</div>

Vars should be there, facts on the other hand, either need to be  
gathered or use a persistent fact cache.

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 27, 2015, 12:22am UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/5 "2015-10-27T00:22:41Z")

</div>

Thanks! It is! I was doing this:

-m shell -a "echo '{{ hostvars }}'"

And it wasn't showing what I was looking for. Either 1) something odd was  
going on where the echo was getting cut off due to shell rules or some such, or  
2) (more likely) it wasn't actually loaded until I did this:

-m shell -a "echo '{{ hostvars['192.168.122.12'] }}'"

That will work great. Thank you!

j

---

<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:** [October 27, 2015, 12:25am UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/6 "2015-10-27T00:25:26Z")

</div>

hostvars is special and lazily loaded, you NEED to specify a host or  
it might appear missing.

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 27, 2015, 6:36am UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/7 "2015-10-27T06:36:31Z")

</div>

I want to say thank again. It turns out the very thing I needed was documented  
right here:

[http://docs.ansible.com/ansible/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

I had read that page, but when it came time to use that knowledge, it didn't  
click. Thanks again for your patience and instruction!

j

---

<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:** [October 27, 2015, 2:43pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/8 "2015-10-27T14:43:54Z")

</div>

> hostvars is special and lazily loaded, you NEED to specify a host or  
> it might appear missing.

> I want to say thank again. It turns out the very thing I needed was  
> documented right here:
> 
> [http://docs.ansible.com/ansible/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

That page \*doesn't\* say two of the interesting/important things about the  
hostvars dict, though: (1) The one Brian just mentioned; (2) One we ran  
into a little while ago, that some of the keys in hostdict aren't hosts.  
Those are both really important if you might otherwise think you could use  
'with\_dict: hostvars' to do something useful.

(If y'all like PRs for things in docsite, I'd be happy to try to add  
something along those lines.)

&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)

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:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 27, 2015, 3:46pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/9 "2015-10-27T15:46:31Z")

</div>

Those would be useful additions. I've submitted document additions/corrections  
before that were accepted.

j

---

<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:** [October 27, 2015, 4:31pm UTC](https://forum.ansible.com/t/looking-up-variables-in-another-hosts-host-var-file/19613/10 "2015-10-27T16:31:41Z")

</div>

if you just want to iterate over all the hosts, use this instead of hostvars:

with\_items: groups['all']
