New in 1.4 devel, a way to include variables in a role more conditionally

I’d like to share some info on a new feature Benno Joy recently added to the devel branch.

In Ansible, you could already load in OS-based variables in a play based on facts like so:

  • hosts: webservers
    vars_files:
  • [ “{{ ansible_os_distribution }}”.yml, ‘defaults.yml’ ]

What does this do?

It loads variables from a YAML file based on the type of distribution (run ansible hostname -m setup to see the value of the distribution), and if not found, it uses a defaults file.

This is nice, but you couldn’t do this in roles, because the mechanism didn’t have a way to say when to pull in variable files.

Now, the new thing.

Inside roles/foo/tasks/main.yml, imagine:

  • include_vars: “{{ ansible_os_distribution }}.yml”

That will look in

roles/foo/vars/.yml

Nice!

Basically you now have something like vars_files for roles, which was much requested for distribution type variables.

For example you can also do:

  • include_vars: “{{ item }}.yml”

with_first_found:

  • “{{ ansible_os_distribution}}.yml”
  • default.yml

and this basically completely emulates vars_files, but can also be used in some new ways.

This could of course be used for any variable or fact, not just the OS distribution, but that’s a likely simple example.

You are allowed to use any kinds of iterators you want (for instance, you could do:)

  • include_vars: “{{ item }}.yml”
    with_fileglob: “…/vars/*.yml”

And easily load all YAML files in a given directory. Just an example – I don’t expect that will be frequent, but you can do it.

And it also works with conditionals:

  • include_vars: foo.yml
    when: x == 27

Basically this allows you to do everything like vars_files, but a bit more dynamically. You couldn’t do this with vars_files before either.

vars_files is not going away – this is included to provide some creative control to role authors to make their roles more flexible.

Can you create subdirectories within …/vars/ with this? I wanted to arrange some of the vars files.

Thank you,
Mark

you can say

  • include_variables: subdir/somefile.yml

yes!

Do I have to enable some specific configuration in order to get this keyword to work?

I receive “ERROR: include_vars is not a legal parameter in an Ansible task or handler” when using the following sample playbook:

I guess it is a typo issue “include_variables” not “include_vars”

Walid,

https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/include_vars.py

https://github.com/ansible/ansible/blob/devel/library/utilities/include_vars

It’s named include_vars.

JTidell,

Make sure you are running out of the code checkout, i.e. source ./hacking/env-setup
and so on.

Awesome Michael, thanks!
I had missed out on the need to source that file on order to get Ansible to run from the devel tree, so you hit the jugular dead on.

This was the only feature I was sorely missing last I looked at Ansible, and now your baby finally has all the features I need to create an orchestration harness that is easy to read (understand), maintain, use and reuse.

excellent!