designermonkey
designermonkey

Reputation: 1108

Ansible group_vars files to match multiple groups

This may be a weird question, and I am sure I have seen this done, but neglected to bookmark it.

I have hosts that exist in multiple groups:

[group1]
host1

[group2]
host2

[group3]
host1
host3

I want to set group vars files for these hosts (that's the simple obvious part):

group_vars/group1.yml
group_vars/group2.yml
group_vars/group3.yml

, and also group vars for hosts that exist using the following pattern:

group1:&group3

Can this be done with a group_vars file? Example:

group_vars/group1:&group3.yml

I am sure I have seen filenames with that style of pattern but I can't figure out how to get it to work.

Upvotes: 3

Views: 9294

Answers (1)

Zeitounator
Zeitounator

Reputation: 44605

(Edit: This answer is still mostly valid but has an update below)

Extract of the documentation

Note:

Group loading follows parent/child relationships. Groups of the same ‘parent/child’ level are then merged following alphabetical order. This last one can be superceeded by the user via ansible_group_priority, which defaults to 1 for all groups. This variable, ansible_group_priority, can only be set in the inventory source and not in group_vars/ as the variable is used in the loading of group_vars/.

So based on this, all you can pretty much do is:

  • have group3 being a parent group for group1 and group2. Vars defined for group3 will then be automatically used for group1 and group2:

    [group1]
    host1
    
    [group2]
    host2
    
    [group3:children]
    group1
    group2
    
  • define vars everywhere and rely on the order in which they are loaded or fix this order in your inventory source with ansible_group_priority.

Now if you where thinking of having different vars values based on the group you target in your play, forget about it: ansible loads all vars from all groups in your inventory/playbook whatever is your target.

I suggest you have a deep read of the explanations regarding where to place a variable


(update)

Situation has a bit changed since my above answer. Although the conclusion does not really change and that you still cannot decide which variable value to get based on the hosts target in your play, ansible has introduced some flexibility to let you define an ansible_group_priority to override the default ASCII order at inventory parse time.

Please note that this variable can only be set in the inventory source and not in the group_vars files

This is now explained in a new part of the documentation: How variables are merged. Relevant extract:

The order/precedence is (from lowest to highest):

  • all group (because it is the ‘parent’ of all other groups)
  • parent group
  • child group
  • host

By default Ansible merges groups at the same parent/child level in ASCII order, and the last group loaded overwrites the previous groups. For example, an a_group will be merged with b_group and b_group vars that match will overwrite the ones in a_group.

You can change this behavior by setting the group variable ansible_group_priority to change the merge order for groups of the same level (after the parent/child order is resolved). The larger the number, the later it will be merged, giving it higher priority. This variable defaults to 1 if not set.

Upvotes: 6

Related Questions