Overriding role defaults with an external file

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

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

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

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.

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.