# Variable Defaults

**URL:** <https://forum.ansible.com/t/variable-defaults/11909>\
**Category:** Ansible Project\
**Created:** [September 19, 2012, 2:55pm UTC](https://forum.ansible.com/t/variable-defaults/11909 "2012-09-19T14:55:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oli\_Yu](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Oli\_Yu](https://forum.ansible.com/u/Oli_Yu)\
**Post date:** [September 19, 2012, 2:55pm UTC](https://forum.ansible.com/t/variable-defaults/11909/1 "2012-09-19T14:55:49Z")

</div>

So, I’ve looked at the variable precedence article here:

[http://ansible.github.com/playbooks2.html#understanding-variable-precedence](http://ansible.github.com/playbooks2.html#understanding-variable-precedence)

My problem is that I’m trying to create some variable defaults within variable yaml files (or other location that is non-environment specific, like the hosts file) that I allow individual hosts to override. In my specific example, I have a set of RAID parameters for machine setup that is mostly generic, but I may want to tweak them for developer boxes or databases servers and this could even be on an individual machine basis, so I’d like to be able to override them per host. We have numerous inventory (hosts) files set up for different environments and I don’t want to force the defaults to be put into every inventory file.

Is there a best practice on how to accomplish this?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 19, 2012, 3:12pm UTC](https://forum.ansible.com/t/variable-defaults/11909/2 "2012-09-19T15:12:22Z")

</div>

> I'd like to be able to override them per host. We have numerous inventory  
> (hosts) files set up for different environments and I don't want to force  
> the defaults to be put into every inventory file.

Host\_vars and group\_vars files are very useful for things like this.

If your playbook is in /etc/ansible/hosts

This would be:

/etc/ansible/group\_vars/name\_of\_group

OR

/etc/ansible/host\_vars/name\_of\_host

as a YAML dictionary.

The templating engine is pretty cool about evaluation, so if you have  
a variable based around another variable, it should just work. Put  
whatever things you want in those that are group or host specific, and  
you can base other variables at a different level around those values,  
no problem.

AKA, if you had something in vars that was:

&nbsp;&nbsp;&nbsp;&nbsp;foo: "$alpha/$beta"

and alpha was set via a host\_vars/alpha file, it will just work.

The technical explanation is that Variable evaluation doesn't happen  
until the last possible moment, so the precedence rules don't really  
get in your way at all from doing what you want.

--Michael

---

<div class="post-metadata">

**Author:** ![Oli\_Yu](https://avatars.discourse-cdn.com/v4/letter/o/ecae2f/32.png) [@Oli\_Yu](https://forum.ansible.com/u/Oli_Yu)\
**Post date:** [September 19, 2012, 9:03pm UTC](https://forum.ansible.com/t/variable-defaults/11909/3 "2012-09-19T21:03:40Z")

</div>

How about something that overrides a default value? For example, I’d like to have a vars file with:

> raid\_level: 0

But be able to override this in my hosts file:

> db\_server raid\_level=5

And use this in my playbook:

> - name: run raid tool  
> action: something  
> only\_if: ‘$raid\_level==5’

Now, I know I could create a group\_vars file or within the hosts file itself, but we have numerous environments with different inventory files. I’d like to store a single default value that would be leveraged across multiple inventory files.

Oliver Yu

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 19, 2012, 9:06pm UTC](https://forum.ansible.com/t/variable-defaults/11909/4 "2012-09-19T21:06:38Z")

</div>

Host cars override the defaults, just like you want, so yeah 🙂

– Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 19, 2012, 9:13pm UTC](https://forum.ansible.com/t/variable-defaults/11909/5 "2012-09-19T21:13:41Z")

</div>

Tip: put defaults in a group\_vars file named “all”. Child groups get higher priority so this is a great place for globals. Alternatively, use vars\_files and include a defaults.yml – your choice.

– Michael

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [September 20, 2012, 3:50am UTC](https://forum.ansible.com/t/variable-defaults/11909/6 "2012-09-20T03:50:29Z")

</div>

> Tip: put defaults in a group\_vars file named "all". Child groups get higher  
> priority so this is a great place for globals.

I think this is worth documenting here:  
[http://ansible.github.com/playbooks2.html#understanding-variable-precedence](http://ansible.github.com/playbooks2.html#understanding-variable-precedence)

I'll send a pull request with this addition.

> Alternatively, use  
> vars\_files and include a defaults.yml -- your choice.

I'm a bit confused by this statement. Don't var\_files have the highest  
precedence? Or is it they have the lowest?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 20, 2012, 12:18pm UTC](https://forum.ansible.com/t/variable-defaults/11909/7 "2012-09-20T12:18:53Z")

</div>

If you have multiple vars of the same name, precedence matters. If you are composing one variable from another, it does not matter which composes from which.

-- Michael
