Variable precedence: inventory versus host_vars

Something which is very unclear at either
http://www.ansibleworks.com/docs/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

or
http://www.ansibleworks.com/docs/intro_inventory.html#splitting-out-host-and-group-specific-data
is the precedence between

host_vars/ # yaml format

and direct entries in the inventory file ( = =)

Some experimentation has shown that a variable set in the inventory file appears to take precedence over host_vars.

Is this guaranteed? Have I missed some documentation?

Thanks,

Brian.

Specifically what sentence or statement is unclear? I’d be happy to clarify

As for “Some experimentation has shown that a variable set in the inventory file appears to take precedence over host_vars.”

These are equivalent ways to set inventory variables.

OK, so what I mean is: if you set an inventory variable in the hosts file and in host_vars, which takes precedence, and can I depend on this?

The reason: I want to distribute some re-usable configs (these are virtual lab setups) to multiple users. I want to put the “normal” configs somewhere, and also have a single place where the user can override them. e.g.

host_vars/s1
lan_address: 10.10.0.241

So everyone who builds the lab will have normally have s1’s lan address as 10.10.0.241. But if they want to, they can edit the hosts file as an override:

s1 ansible_connection=local lan_address=1.2.3.4

Regards,

Brian.

“if you set an inventory variable in the hosts file and in host_vars, which takes precedence, and can I depend on this?”

No, you can’t.

Best practices would suggest putting variables only in host_vars/group_vars, unless it’s something like an ansible_ control variable, where having that in inventory can be convient.

Hi,

I ran across this same “not issue” thinking that my host_vars/x.yml would override the inventory file. I get the Ansible Zen approach, and now that I know the answer from MD above, I am A-OK. I think it would be nice if this information was covered in the Ansible docs specifically so folks can easy access to this answer.

Thank you.
– Jeremy

I had similar doubts last week, got my answers on the irc channel.

Quick question for Michael, do you use git for your documentation pages or is it wiki based? I would be happy to submit a pull request with enhancements to the docs.
It would also help me with my bad memory

docs are inside same git repo as the rest of ansible, PRs for docs work just like PRs to code.