when do variables become hostvars ?

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

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” when accessed from a template about another host:

{{ hostvars[“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.

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 …