# Overriding role defaults with an external file

**URL:** <https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883>\
**Category:** Ansible Project\
**Created:** [August 7, 2014, 5:18pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883 "2014-08-07T17:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [August 7, 2014, 5:18pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883/1 "2014-08-07T17:18:21Z")

</div>

I’ve got a situation where I’m applying roles to a system locally, much in the style of ansible-pull. The role has a number of defaults, but I wish to override those defaults with custom details specific to this particular environment. My wish is to provide those overrides via an external file to the role, something we can iterate on without having to change the role code itself (as it is distributed through a different method).

I’m looking for ideas on how to make this work. I had originally thought to make use of ansible local facts, in /etc/ansible/facts.d/\*.fact files However that results in variables named ansible.local.\<filename\>.\<blockname\>.\<key\> which obviously cannot override a role default as-is.

Any thoughts you all might have would be appreciated!

-jlk

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [August 7, 2014, 5:21pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883/2 "2014-08-07T17:21:21Z")

</div>

and having just sent this, I remembered about include\_vars, which I could put in my role’s entry point to load variables from an external file. That would likely work, but I’m still curious how others would solve this issue.

-jlk

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [August 7, 2014, 5:25pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883/3 "2014-08-07T17:25:41Z")

</div>

You can also specify a JSON or YAML file with --extra-vars, which is also supported with ansible-pull.

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [August 7, 2014, 6:56pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883/4 "2014-08-07T18:56:18Z")

</div>

I encountered this recently. I solved it with group\_vars and a simple trick in my hosts file:

#hostsfile

localhost ansible\_python\_interpreter=“/usr/bin/env python” ansible\_connection=local

[fancyapp-blue]  
localhost

[fancyapp-violet]  
localhost

##end hostsfile

Then, in my groupvars/fancyapp-blue and groupvars/fancyapp-violet, I override the role defaults.

I make calls to specific “environments” like

- hosts: fancyapp-blue  
gather\_facts: no  
roles:
- asg

In this case, the group vars for fancyapp-blue will override the role defaults for asg. You could also do some neat things like applying the same role against groups of hosts, if you wanted to create things in parallel.

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [August 7, 2014, 9:07pm UTC](https://forum.ansible.com/t/overriding-role-defaults-with-an-external-file/15883/5 "2014-08-07T21:07:03Z")

</div>

So I’ve realized that approach is incorrect, instead of using groups and group\_vars, I use host & host\_vars:

## hosts file

localhost ansible\_python\_interpreter=“/usr/bin/env python” ansible\_connection=local  
fancyapp-blue  
fancyapp-violet

[apps]  
fancyapp-blue  
fancyapp-violet

[apps:vars]  
ansible\_python\_interpreter=“/usr/bin/env python”  
ansible\_connection=local

Then, in my host\_vars/fancyapp-blue and host\_vars/fancyapp-violet, I override the role defaults.

I make calls to specific “environments” like

- hosts: fancyapp-blue  
gather\_facts: no  
roles:
- asg

In this case, the host\_vars for fancyapp-blue will override the role defaults for asg.
