# when do variables become hostvars ?

**URL:** <https://forum.ansible.com/t/when-do-variables-become-hostvars/13993>\
**Category:** Ansible Project\
**Created:** [November 25, 2013, 10:59am UTC](https://forum.ansible.com/t/when-do-variables-become-hostvars/13993 "2013-11-25T10:59:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [November 25, 2013, 10:59am UTC](https://forum.ansible.com/t/when-do-variables-become-hostvars/13993/1 "2013-11-25T10:59:59Z")

</div>

Hello,

some of my plays on hosts need dynamic information from other hosts so I am trying to understand when/which variable in the playbooks become available in hostvars.

I undestand that :

- a variable defined via gather\_facts is available via hostvars[host]
- a variable defined in host\_vars is available via hostvars[host]
- a variable defined in group\_vars/group is available via hostvars[host] if the host belong to the group
- a variable set on a host during a play via the register: syntax is available via hostvars[host]
- a call to set\_fact: on a host makes the variable available via hostvars[host]

but a variable defined

- at the play/vars: level
- at the play/vars\_files: level
- at the role/vars level
- role/default level  
are not made available to hostvars[host]

Is this right ? is there a documentation explaining this along this angle that I could read ?

on a vocabulary perspective, is it correct to say that

- hostvars[host] contains all facts currently know about a host at a given step in the playbook execution
- all facts known about a given host at a given step in the playbook execution are available in hostvars[host]

Is there a simple way to express which variables become host facts ?

Thank you  
Jerome

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 25, 2013, 3:00pm UTC](https://forum.ansible.com/t/when-do-variables-become-hostvars/13993/2 "2013-11-25T15:00:34Z")

</div>

For everyone else’s benefit, let me step back a little and provide some background info.

(A)

Ansible has a directory called “host\_vars/” where you can store variables that are host specific

(B)

“hostvars” refers to an datastructure that can be used for getting variables from other hosts.

For instance, if I want the OS family of a server named “[foo.example.com](http://foo.example.com)” when accessed from a template about _another_ host:

{{ hostvars[“[foo.example.com](http://foo.example.com)”][“ansible\_os\_family”] }}

This is usually unneccessary as if you want the variable for your current host it’s just

{{ ansible\_os\_family }}

So the question is not, “how does the code work”, but what problem are you trying to solve that you are having trouble with?

(C)

Your question was “some of my plays on hosts need dynamic information from other hosts so I am trying to understand when/which variable in the playbooks become available in hostvars.”

If you need to get a “fact” (some information discovered about a remote system that is not explicitly set as a variable, but derived from talking to it), you need to talk to that system in an earlier play.

For instance if the DB servers need the IP addresses of all the webservers, this is common:

- hosts: webservers  
tasks: 

- hosts: dbservers  
tasks:

- 
# things go here

Talking to the webservers before will get their fact information.

Variables set otherwise are already availble, and do not require talking to the hosts.

Sometime in 1.5 timeframe, we’ll also be implementing a way to store facts from previous runs, so you don’t have to talk to nodes within the same play.

---

<div class="post-metadata">

**Author:** ![Cyril\_Scetbon](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@Cyril\_Scetbon](https://forum.ansible.com/u/Cyril_Scetbon)\
**Post date:** [December 21, 2014, 4:48pm UTC](https://forum.ansible.com/t/when-do-variables-become-hostvars/13993/3 "2014-12-21T16:48:12Z")

</div>

Hi,

You do not answer the question concerning the fact that variables set at the play/vars level are not made available through hostlers which would be a damn helpful thing !  
I can’t understand the reason it’s not made available and can’t find where it’s documented …
