In a previous thread, it was discussed how one could apply deployment specific vars to a play. My solution at the time was to pass in a master config “pointer” file via the commandline with the -e “@filename” idiom containing file names that could be loaded by vars_files or include_vars.
ansible-playbook -i hosts site.yml -e “@cluster_config.yml”
Then in site.yml (or a tasks file with include_vars: )
- hosts: hadoop_cluster
vars_files: - ["{{network_config}}]
- ["{{environment_config}}]
- ["{{hadoop_config}}]
Michael suggested this was an anti pattern and I did find a maintenance problem because of all those vars_files lines sprinkled about the playbook.
My preferred method is now to modify the hosts file to add additional deployment specific groups to the top of the hosts file and have the vars loaded automatically via group_vars.
Currently, the group_vars loader does what I would call “Immediate folder match”. If a top level folder in group_vars matches any of the groups, all descended files and folders ending in .yml will be loaded.
This prevents hierarchical groupings of deployments. I would like to organize my deployments like this
[kbroughton@mb-kbroughton:lynx-ansible/dev-ansible + (develop)] tree …/21ct-ansible/group_vars/
…/21ct-ansible/group_vars/
├── all
└── datacenter
├── datacenter.yml
├── production
├── production.yml # vars common to all prod systems
│ └── client1_prod
│ ├── client1_app.yml
│ └── client2_prod
│ ├── client2_app.yml
└── staging
├── staging.yml
├── client1_stag
│ ├── client1_stag.yml
│ ├── client1_app.yml
└── client2_stag
Each step of the descent, files in a matching folder are loaded but only matching subfolders will be descended into.
I’m writing this to open discussion again on how to best accomplish deployment-specific vars. At the moment, my solution is to modify the signature of the _load_vars and related methods in
ansible/lib/ansible/inventory/vars_pluginsgroup_vars.py
to pass in groups information.
Then I add the checking that only descends into a directory if it is a in groups[host].
The default behavior could remain as it is with a new variable in .ansible.cfg
group_vars_load_strategy: { one of HIERARCHICAL_FOLDER_MATCH,
IMMEDIATE_FOLDER_MATCH - the current behavior
}
Would a pull request along these lines be considered, or is there an alternate preferred method that achieves the deployment-specific vars management we need?
kesten