1
votes

I have an ansible hosts file that looks like that:

[myhosts]
192.168.0.1
192.168.0.2

[group1:children]
myhosts

[group2:children]
myhosts

Along side with group vars files for group1 and group2 (and incidentally for myhosts) that are declaring some common variables but with different values.

Here is my directory layout (that follows the best pratices):

hosts
main.yml
group_vars/
    group1.yml
    group2.yml
    myhosts.yml

Therefore, both group vars files for group1 and group2 are automatically included by ansible when an host belongs to both groups.

For the record, I am callinng ansible on my playblook with the following pattern as target (specified in command line):

192.168.0.1:&group1

Considering that, how can I make ansible include only the group vars file associated to group1 (and children group myhosts) ? Is that possible ?

An alternate solution would be renaming the group vars files of group1 and group2 to something different (to prevent automatic includes) and includes those in separate "proxy" playbooks, specific to each subgroup, but this seems less clean to me.

Note: for the moment it is not the case, but in the futur it is likely that group1 and group2 would have few hosts not in common.

1

1 Answers

3
votes

The hosts value of a play is a way to set the target hosts of the play, but it it is not a view on those hosts.
If a host belongs to multiple groups it will always load vars from the group_vars of all those groups, not only the one specified in the play.