Ansible: using nested groups with vars
Asked Answered
A

1

5

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!

Atonal answered 24/1, 2017 at 0:53 Comment(0)
W
10

Not sure what is your problem. You should be fine with:

hosts:

[bots-a]
bot1

[bots-b]
bot2

[bots:children]
bots-a
bots-b

dirs:

./group_vars/bots.yml
./group_vars/bots-a.yml
./group_vars/bots-b.yml

There is a concept of group depth in Ansible (at least in recent versions). In this example, group variables for host bot2 will be populated in the following order:

depth 0: group all, all.yml (missing here, ignoring)
depth 1: group bots, bots.yml
depth 2: group bots-b, bots-b.yml

You can see details and processing order here in the source code.

So if you define defaults in bots.yml and specific values in bots-b.yml, you should achieve what you expect.

Wingback answered 24/1, 2017 at 6:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.