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

**URL:** <https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831>\
**Category:** Ansible Project\
**Created:** [November 2, 2013, 1:46pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831 "2013-11-02T13:46:48Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [November 2, 2013, 1:46pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/1 "2013-11-02T13:46:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [November 5, 2013, 4:11am UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/2 "2013-11-05T04:11:06Z")

</div>

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

Thank you,  
Mark

---

<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:** [November 6, 2013, 1:54pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/3 "2013-11-06T13:54:28Z")

</div>

you can say

- include\_variables: subdir/somefile.yml

yes!

---

<div class="post-metadata">

**Author:** ![jtidell](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jtidell](https://forum.ansible.com/u/jtidell)\
**Post date:** [November 18, 2013, 2:55am UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/4 "2013-11-18T02:55:32Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [November 18, 2013, 4:04am UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/5 "2013-11-18T04:04:16Z")

</div>

I guess it is a typo issue “include\_variables” not “include\_vars”

---

<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:** [November 18, 2013, 1:51pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/6 "2013-11-18T13:51:24Z")

</div>

Walid,

[https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action\_plugins/include\_vars.py](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](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.

---

<div class="post-metadata">

**Author:** ![jtidell](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jtidell](https://forum.ansible.com/u/jtidell)\
**Post date:** [November 18, 2013, 5:40pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/7 "2013-11-18T17:40:28Z")

</div>

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.

---

<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:** [November 18, 2013, 5:51pm UTC](https://forum.ansible.com/t/new-in-1-4-devel-a-way-to-include-variables-in-a-role-more-conditionally/13831/8 "2013-11-18T17:51:10Z")

</div>

excellent!
