luqo33
luqo33

Reputation: 8361

Ansible - overriding variables of globally installed roles

I am wondering what's the best practice for customizing/overriding variables used in globally installed Ansible roles (/etc/local/ansible) that are used across many of our playbooks?

Such roles might include variables in defaults/main.yml as well as in vars/.

After requiring a globally installed role in my local playbook, it would be natural to customize those variables. Since there is no direct access to the role's directory, is my only option to override these variables in group_vars / host_vars? Or, perhaps passing overriding vars in the playbook directly, but this does not seem like a good idea?

Upvotes: 0

Views: 2547

Answers (3)

Sebin John
Sebin John

Reputation: 1

You could define all the variables you want to override in JSON/YAML file and pass to the playbook using --extra-vars. Eg: ansible-playbook site.yml -i inventory --extra-vars @vars.json

Upvotes: 0

udondan
udondan

Reputation: 59989

vars of roles are not meant to be overridden. If they were, they were (or should be) defaults. vars can only be overridden with --extra-vars passed from command line.

Upvotes: 0

Ben Whaley
Ben Whaley

Reputation: 34416

Consult the docs on variable precedence. In particular, they state:

Basically, anything that goes into “role defaults” (the defaults folder inside the role) is the most malleable and easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in namespace. The idea here to follow is that the more explicit you get in scope, the more precedence it takes with command line -e extra vars always winning. Host and/or inventory variables can win over role defaults, but not explicit includes like the vars directory or an include_vars task.

Hence, given the precedence list you should locate the variables wherever it seems most sensible.

Note that the precedence rules differ between Ansible versions 1 and 2.

Upvotes: 0

Related Questions