Variable Defaults

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

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?

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:

    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

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

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

– Michael

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

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

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?

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