# include\_vars for defaults

**URL:** <https://forum.ansible.com/t/include-vars-for-defaults/15105>\
**Category:** Ansible Project\
**Created:** [April 28, 2014, 10:01pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105 "2014-04-28T22:01:54Z")\
**Posts on this page:** 11\
**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:** [April 28, 2014, 10:01pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/1 "2014-04-28T22:01:54Z")

</div>

Hi,

I’m trying to write generic roles for particular applications and keep hitting across the same problem:

I want to set role default based on the operating system or other facts, like include\_vars, but this doesn’t appear to be supported.

The only option I see practically available for template is to use the template logic to handle defaults, but that, while flexible, is not as elegant as being able to set by-fact defaults.

The code I’m working at present on is [https://github.com/WillsherPartners/ansible-sshd](https://github.com/WillsherPartners/ansible-sshd) , where there is a large set of defaults and a template. sshd\_subsystem\_sftp varies depending on OS, but I still want to give the person using the role to option to override the value if they see fit.

Are there any plans for include\_vars: (include\_defaults?) functionality in Ansible, or is there some other way to tackle per-os defaults?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 28, 2014, 11:54pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/2 "2014-04-28T23:54:57Z")

</div>

I’m a little confused by this because you seem to be saying a feature isn’t supported that is actually part of Ansible, but it’s actually a module already in Ansible:

[http://docs.ansible.com/include\_vars\_module.html](http://docs.ansible.com/include_vars_module.html)

It also works with the “when” operator for per-OS variant usage.

Were you perhaps reading an old thread talking about a language feature when it didn’t exist?

Sorry for confusion…

---

<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:** [April 29, 2014, 7:05am UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/3 "2014-04-29T07:05:05Z")

</div>

I’d like the included vars injected with the same precedence as those in defaults/main.yml rather than the precedence of vars/main.yml. The documentation is not clear on the what precendence include\_vars get.

Taking:

```auto
1. -e variables always win
2. then comes "most everything else"
3. then comes variables defined in inventory
4. then comes facts discovered about a system
5. then "role defaults", which are the most "defaulty" and lose in priority to everything.

```

I’d like the included vars to have the precedence of 5, rather than 2

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 29, 2014, 12:32pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/4 "2014-04-29T12:32:51Z")

</div>

include\_vars creates fact scoped variables.

Sorry, you’re not going to be able to get it to work every which sort of different way.

I would not assume OS specific defaults _or_ instead just put hosts in a group based on the OS, if you really need that capability, and then could use group\_vars/ files to do this.

---

<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:** [April 29, 2014, 2:05pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/5 "2014-04-29T14:05:00Z")

</div>

Testing on 1.5.5 they appear at the level of “most everything else” - a variable defined in a include\_vars file overrides an inventory variable. If they were at the level of facts, that would be great and would all default like behaviour and overriding by more environment specific data.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 29, 2014, 3:49pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/6 "2014-04-29T15:49:30Z")

</div>

these variables are facts – they override inventory variables because they are at the same depth and are loaded later

---

<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:** [April 29, 2014, 4:14pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/7 "2014-04-29T16:14:12Z")

</div>

In the docs on precedence, discovered facts come under inventory but the include\_vars facts are actually higher precedence than the inventory.

Being able to have a default\_vars module would, I maintain, be a boon for reusable roles. They can be less opinionated and fit more circumstances. It allows for user control while not requiring configuration from the end user for the role to work.

Is it something I could implement as a module for my own use or is the variable registration more ingrained than that?

Thanks for your time,  
Matt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 29, 2014, 6:51pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/8 "2014-04-29T18:51:32Z")

</div>

This is true, it’s also intentional to avoid facts clobbering explicitly set variables. But it’s all around the same ballpark.

No, we are not adding any kind of “default\_vars” module.

This doesn’t even make sense as a module as “defaults/” in a roles directory is processed by core code, it’s not a module.

I’m sorry you can’t have precedence every way you could possibly want it, but it works the way it does.

You can use include\_vars with conditionals or facts to easily load OS specific variables.

It cannot be used for OS specific defaults.

---

<div class="post-metadata">

**Author:** ![F.L\_Jonathan\_Arana\_C](https://avatars.discourse-cdn.com/v4/letter/f/ea5d25/32.png) [@F.L\_Jonathan\_Arana\_C](https://forum.ansible.com/u/F.L_Jonathan_Arana_C)\
**Post date:** [April 30, 2014, 9:54am UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/9 "2014-04-30T09:54:04Z")

</div>

I asked for this a few weeks back. Finally I managed to implement a pattern in my role to workaround this

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 30, 2014, 8:41pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/10 "2014-04-30T20:41:29Z")

</div>

Well, sort of, but unfortunately, no 🙂

While the default does control what variable gets loaded with “set\_fact” (this could have been done without the default) it is still going to get loaded at a level that is not the same as defaults, so inventory can’t override what you have loaded there.

I’d find “include\_vars” cleaner personally, if that’s all you want

- include\_vars: “{{ ansible\_os\_distribution }}.yml”

Which allows vars/RedHat.yml and vars/Debian.yml in the role.

---

<div class="post-metadata">

**Author:** ![Daniel1](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@Daniel1](https://forum.ansible.com/u/Daniel1)\
**Post date:** [November 6, 2018, 8:11pm UTC](https://forum.ansible.com/t/include-vars-for-defaults/15105/11 "2018-11-06T20:11:19Z")

</div>

I also have the need to set environment specific role defaults.

I am using include\_vars for this purpose which works fine as long as you don’t want to overwrite some of this role default variables via host\_vars.  
Because that is not going to work, because host\_vars are overwritten by included vars.

Therefore after the include\_vars for my role defaults, I include the hostvars explicitly again like this:

```auto
- name: Include hostname specific vars
  include_vars:
    file: "host_vars/{{ inventory_hostname }}.yml"

```

It works, but I am not sure if this is only working by accident.

Regrads  
Daniel
