inside each inventory folder, combinations of dynamic and static inventory sources
inside each inventory folder, environment specific group vars
Currently I am copy-pasting variables common to all of the inventories (environments) between the all.yml group_vars files.
My question: is there a way to have a ‘global’ group variables file I can use for this type of variable?
I know I could source a file with include_vars: at the start of each of the multiple plays inside the playbooks, but hoped there might be something more elegant and/or automatic (like group_vars) available. Maybe even a simple restructure I can’t think of.
Have you thought about using role default variables instead? If all environments have the same setting, this would be simpler. Role default variables can then be overridden for specific groups in the group vars file.
Already doing that where possible, but for things like microservices that have templated configs that use the same variable, having it in defaults still requires copy-paste, but at that (potentially larger) level instead.
Might it be possible to add an ansible configuration param that forces a particular file to always be loaded, in addition to normal?
Abuse a vars_plugin to always return the vars in a file…?
I’d like to hear advices for this as well. I do have one idea that I don’t like however. You should be able to use group_vars relative to your playbooks as well. So put a group_vars dir outside of your inventories, next to your playbooks, and that will be evaluated for all envs.
Thanks Hafai,
Confirming that works, unbelievably. A group_vars/all.yml next to playbooks, as you say, is evaluated as well as in the inner group_vars.
I’m warming to the idea of writing a vars plugin that is essentially just a pointer to a file somewhere. Advantage is that the target could be anywhere you like.
Problem with a vars plugin is that you’ll have to specify it in each playbook. New idea: you could put another inventory script in your dir that evaluates your common file.
I have, and and still are, struggling with this as well. Our inventory is very similar to yours. We have three AWS environments, and dozens of other on-prem environments. For the AWS we need to share a bunch of variables. Putting those in playbook group_vars does not work because the are only for AWS, not the other environments.
Hence I have resorted to soft-linking an aws-common.yml into each of the aws group_vars. Since git handles the links its really no pain, just a bit of confusion when searching.
I do a similar thing in the playbooks for other reasons. But now reading below the under playbooks ansible will look up the tree for group_vars I may not need those.
Thanks to Hagai I have a good solution worth sharing.
add common_vars.sh bash script to each of the inventories I wish to share variables. Make it executable. Point it where your common Ansible YAML variables are (in my case inventory/common_vars.yml).
add common_vars.yml and populate it
Run your playbooks with the specified inventory path as normal (inventory/aws-dev, inventory/aws-prod etc)
Benefits
inventory files are all kept in the inventory folder
normal Ansible group_vars are left alone
only need one extra file per inventory, and no copy paste
if you need a few top level common_vars, you can just point your common_vars to onprem_common.yml and cloudstack_common.yml, or as you like.
feels like a proper Ansible way to do things without hacks
New dir structure below (compared to original). Bash script code after.