# Variable precedence: inventory versus host\_vars

**URL:** <https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718>\
**Category:** Ansible Project\
**Created:** [October 18, 2013, 8:29pm UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718 "2013-10-18T20:29:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 18, 2013, 8:29pm UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/1 "2013-10-18T20:29:53Z")

</div>

Something which is very unclear at either  
[http://www.ansibleworks.com/docs/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](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](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.

---

<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:** [October 20, 2013, 1:19am UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/2 "2013-10-20T01:19:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 21, 2013, 9:34am UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/3 "2013-10-21T09:34:16Z")

</div>

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.

---

<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:** [October 22, 2013, 1:16am UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/4 "2013-10-22T01:16:55Z")

</div>

“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.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Schulman](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Jeremy\_Schulman](https://forum.ansible.com/u/Jeremy_Schulman)\
**Post date:** [February 26, 2014, 5:21pm UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/5 "2014-02-26T17:21:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Azul](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Azul](https://forum.ansible.com/u/Azul)\
**Post date:** [March 2, 2014, 7:48pm UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/6 "2014-03-02T19:48:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [March 3, 2014, 2:08pm UTC](https://forum.ansible.com/t/variable-precedence-inventory-versus-host-vars/13718/7 "2014-03-03T14:08:08Z")

</div>

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