I have a situation where we have 3 tiers of boxes, in each tier we apply different variables settings (like where the cache dir is), but there are a bunch of defaults. I also need to override on a per node basis, which is usually done via inventory vars on the host itself. I am not sure what is the best way to organize the hosts so that the precedence works in my favor.
Here are the different things I have tried. In each case I have entries in the inventory file like this:
[bots-fancy]
fancy-1
[bots-super-fancy]
super-1
[bots-magical]
magic-1
magic-2 provider=aws
At first, I had each of them with a long string of variable definitions. I also had different group_var/bots/[bots-magical | bots-super-fancy | bots-fancy].yaml
files. This quickly became untenable.
attempt 1: with playbook variables In the playbook I had something like this:
---
hosts:
- bots
vars_files:
- "group_vars/bots/defaults.yml"
- "group_vars/bots/{{ groups_names[0] }}.yml"
roles:
- somethign
this worked (though yes brittle) but it wouldn't let me override on a per host basis. I had to set things different on nodes occasionally, but not on the whole group.
attempt 2: using group_vars
for each
I added
[bots:children]
bots-fancy
bots-super-fancy
bots-magical
to the hosts file. Removed any vars_files
from the playbook and created group_vars
for each group. I added the default/shared settings to group_vars/bots.yaml
. When I'd run the playbook, it would only load the bots
group_vars it seemed. Ideally, I want it to load the bots
and then override it with the bots-fancy
. And then finally the values from the hosts file.
I am not sure the best way to structure these groups, so any input would be very helpful!