My Inventory is nested like so:
customerA:
children:
webserver:
hosts:
host1:
host2:
dbserver:
hosts:
host3:
host4:
customerB:
children:
webserver:
hosts:
host5:
host6:
dbserver:
hosts:
host7:
host8:
So for host1
ansible loads:
host_vars/host1
group_vars/customerA
group_vars/webserver
Is there a way to create a nested_vars/customerA/webserver.yml
(same as nested_vars/webserver/customerA.yml
) structure that gets used by the inventory management?
Or do I have to use something like
group_vars/customerA
- parent: "customerA"
group_vars/webserver:
- child: "webserver"
in every playbook:
include_vars:
file: "{{ parent }}+{{ child }}.yml"
or create new groups for all combinations I need to target?
This is built upon this other answer
Create your base static inventory
inventories/default/0-hosts.yml
---
customerA:
children:
customerA_webservers:
hosts:
host1:
host2:
customerA_dbservers:
hosts:
host3:
host4:
customerB:
children:
customerB_webservers:
hosts:
host5:
host6:
customerB_dbservers:
hosts:
host7:
host8:
Make your type groups (e.g. webservers
, dbservers
) dynamic using the above naming convention with the constructed inventory plugin
inventories/default/1-typegroups_constructed.yml
---
plugin: constructed
groups:
dbservers: group_names | select('match', '^.*_dbservers$') | length > 0
webservers: group_names | select('match', '^.*_webservers$') | length > 0
You now have a complete fine grained group membership:
$ ansible-inventory -i inventories/default/ --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped",
"customerA",
"customerB",
"webservers",
"dbservers"
]
},
"customerA": {
"children": [
"customerA_webservers",
"customerA_dbservers"
]
},
"customerA_dbservers": {
"hosts": [
"host3",
"host4"
]
},
"customerA_webservers": {
"hosts": [
"host1",
"host2"
]
},
"customerB": {
"children": [
"customerB_webservers",
"customerB_dbservers"
]
},
"customerB_dbservers": {
"hosts": [
"host7",
"host8"
]
},
"customerB_webservers": {
"hosts": [
"host5",
"host6"
]
},
"dbservers": {
"hosts": [
"host3",
"host4",
"host7",
"host8"
]
},
"webservers": {
"hosts": [
"host1",
"host2",
"host5",
"host6"
]
}
}
Which makes you able to address:
inventories/default/group_vars/customerA.yml
inventories/default/group_vars/dbservers.yml
inventories/default/group_vars/customerB_webservers.yml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With