# Including vars file based on another variable value in roles

**URL:** <https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338>\
**Category:** Ansible Project\
**Created:** [August 21, 2013, 7:06am UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338 "2013-08-21T07:06:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/matt/32/1267_2.png) [@matt](https://forum.ansible.com/u/matt)\
**Post date:** [August 21, 2013, 7:06am UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/1 "2013-08-21T07:06:23Z")

</div>

Hi,

In the pre-role world, we were using syntax like:

var\_files: var/{{ ansible\_os\_distribution }}.yml  
tasks:

- action: {{ ansible\_pkg\_mgr }} name={{ item }}  
with\_items: {{ software }}

this kind of thing would be useful as a role. Is there anyway to include a varfile either directly based on OS, or via a variable name as in the above example?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [August 26, 2013, 3:38pm UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/2 "2013-08-26T15:38:53Z")

</div>

That is quite elegant way how to install same packages with different names on different distributions.

I`m trying to mimic it in roles, but so far no luck. I cannot find a way to include different vars for different distributions in the same role :.

---

<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:** [August 26, 2013, 7:55pm UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/3 "2013-08-26T19:55:13Z")

</div>

I actually don’t find that elegant.

First – One really should not use “ansible\_pkg\_manager” because it bypasses with the with\_items optimizations that allow yum and apt packages to be installed in the same transaction set.

Second --No, you can’t load roles based on facts, and should not do this.

Rather, two options:

(A)

use the group\_by module to select your webservers that are Ubuntu and your webservers that are Fedora (for example) and then apply the appropriate  
roles to each,

(B) use the “when” parameter on the role like so:

roles:

- { role: ubuntu\_stuff, when: ansible\_os\_distribution == “Ubuntu” }
- …
- { role: common\_stuff }

The group\_by method will produce cleaner output when the playbook is run.

In many cases though, you should know what OS each of your servers are, so many people will not have to worry about dynamic application.

If you have a web farm where some servers are randomly differnet distributions, I recommend straightening that out 🙂

---

<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:** [August 26, 2013, 8:06pm UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/4 "2013-08-26T20:06:33Z")

</div>

in pre role i used group by and just tasks that would execute for yum or apt, each with their own vars list {{ansible\_pkg\_manager}}\_webapp\_deps.yml

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [August 27, 2013, 12:10pm UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/5 "2013-08-27T12:10:45Z")

</div>

Michael,

> I actually don't find that elegant.
> 
> First -- One really should not use "ansible\_pkg\_manager" because it  
> bypasses with the with\_items optimizations that allow yum and apt packages  
> to be installed in the same transaction set.

Was about to ask about odd behaviour if ansible\_pkg\_manage used, thanks for  
explaining.

> Second --No, you can't load roles based on facts, and should not do this.
> 
> Rather, two options:
> 
> (A)
> 
> use the group\_by module to select your webservers that are Ubuntu and your  
> webservers that are Fedora (for example) and then apply the appropriate  
> roles to each,
> 
> (B) use the "when" parameter on the role like so:
> 
> roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { role: ubuntu\_stuff, when: ansible\_os\_distribution == "Ubuntu" }  
> &nbsp;&nbsp;&nbsp;&nbsp;- ...  
> &nbsp;&nbsp;&nbsp;&nbsp;- { role: common\_stuff }
> 
> The group\_by method will produce cleaner output when the playbook is run.
> 
> In many cases though, you should know what OS each of your servers are, so  
> many people will not have to worry about dynamic application.
> 
> If you have a web farm where some servers are randomly differnet  
> distributions, I recommend straightening that out 🙂

Working for several customers with different distributions, maybe not so  
common use case :). So basically, if I want to have clean output, I should  
have LAMP-Debian and LAMP-CentOS roles?

I can surely take duplicated code into separate .yml files and share it  
between roles, is that 'best practise'?

David

---

<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:** [August 27, 2013, 12:44pm UTC](https://forum.ansible.com/t/including-vars-file-based-on-another-variable-value-in-roles/13338/6 "2013-08-27T12:44:54Z")

</div>

Yes, definitely sharing common tasks from two different roles is doable and a good way to do it.

In 1.3, James also added role dependencies, which provide a cleaner way to do this, so you could have a fooapp\_centos role that requires all the common  
parts, but also the right OS specific parts as well.

So you can just have

- hosts: XyzCustomerWebservers  
roles:
- fooapp\_centos
