Variable precedence: inventory versus host_vars

1,440 views
Skip to first unread message

candlerb

unread,
Oct 18, 2013, 4:29:53 PM10/18/13
to ansible...@googlegroups.com
Something which is very unclear at either
http://www.ansibleworks.com/docs/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
or
is the precedence between

host_vars/<hostname>   # yaml format

and direct entries in the inventory file (<hostname> <var1>=<val1> <var2>=<val2>)

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.

Michael DeHaan

unread,
Oct 19, 2013, 9:19:16 PM10/19/13
to ansible...@googlegroups.com
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.   






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

candlerb

unread,
Oct 21, 2013, 5:34:16 AM10/21/13
to ansible...@googlegroups.com
On Sunday, 20 October 2013 02:19:16 UTC+1, Michael DeHaan wrote:
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.

Michael DeHaan

unread,
Oct 21, 2013, 9:16:55 PM10/21/13
to ansible...@googlegroups.com
"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.






--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeremy Schulman

unread,
Feb 26, 2014, 12:21:47 PM2/26/14
to ansible...@googlegroups.com
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

Azul Inho

unread,
Mar 2, 2014, 2:48:12 PM3/2/14
to ansible...@googlegroups.com
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

Brian Coca

unread,
Mar 3, 2014, 9:08:08 AM3/3/14
to ansible...@googlegroups.com
docs are inside same git repo as the rest of ansible, PRs for docs work just like PRs to code.
Reply all
Reply to author
Forward
0 new messages