Machines in multiple groups

My final question today, promise :slight_smile:

It seems that you can have the same machine in multiple groups. Is this frowned upon? For example:

[macmini]
s1
s2
s3

[dnsserver]
s1
s2

[dhcpserver]
s2

s3

This I can see as being useful. There are a number of orthogonal traits which might be applied to a machine based not only on what is function(s) are but what hardware it is.

On the other hand, this introduces a management headache: if I want to comment out a server (e.g. itā€™s gone away temporarily), I have to find all the places where that server is mentioned.

That wouldnā€™t be an issue if you could do group membership the other way round (y is a member of group x, rather than group x contains y):

s1 groups=macmini,dnsserver
s2 groups=macmini,dnsserver,dhcpserver
s3 groups=macmini,dhcpserver

However, that doesnā€™t seem to be supported. It might be possible to fake it up using variables and group_by I guess.

I realise that there are ā€œrolesā€, but in the above example, to avoid the same host being in different groups I would have to synthesise groups for each combination of roles used, e.g.

[macmini_dnsserver]
s1

[macmini_dnsserver_dhcpserver]
s2

[macmini_dhcpserver]
s3

Then the playbook could have appropriate bindings between groups and roles.

Am I missing something obvious here? Is there a way to disable a host, without removing it from all groups?

Thanks,

Brian.

I don't think I have a machine in less than 4 groups and no, no shortcut,
you need to remove all instances of a host. unless you want to run all
commands with --limit 'all:!<host>'

machines can be in multiple groups, and groups can be in groups.

Itā€™s a quite intentional feature.

Groups often describe geography or purpose or environment and all sorts of things.

OK thatā€™s good, it means Iā€™m not abusing things.

So my problem is: whatā€™s the cleanest way to say ā€œmachine s2 is currently not present, donā€™t try to connect to it?ā€

If itā€™s in lots of groups, I need to comment it out of every one. And when itā€™s back, I need to remember to uncomment it from all.

Something like ā€œansible_connection=noneā€ would do, I guess.

What Iā€™d really like is to be able to say

s1 groups=g1,g2,g3

group_by: key={{groups}}

but there is little documentation for group_by, and whether thereā€™s any way to give it a multi-valued item (either a list or a JSON object)

Regards,

Brian.

Of course I meant YAML not JSON :slight_smile:

What Iā€™d really like is to be able to say

s1 groups=g1,g2,g3

Actually, it turns out that the ā€œadd_hostsā€ module supports exactly this syntax. It dynamically creates the groups if they donā€™t exist, then adds the host to them. It understands four special parameters - ā€˜nameā€™, ā€˜hostnameā€™, ā€˜groupnameā€™, ā€˜groupsā€™ - and anything else is considered to be setting a host variable.

So, how would people feel about adding the same to the main inventory file too?

Looks like it would be a small change to lib/ansible/inventory/ini.py

I donā€™t understand what you are asking.

Iā€™m fine with adding a variable that says ā€œansible_skip: Trueā€ or something on the host or group though. Seems clearer.