# Machines in multiple groups

**URL:** <https://forum.ansible.com/t/machines-in-multiple-groups/13720>\
**Category:** Ansible Project\
**Created:** [October 18, 2013, 9:11pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720 "2013-10-18T21:11:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 18, 2013, 9:11pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/1 "2013-10-18T21:11:10Z")

</div>

My final question today, promise 🙂

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 18, 2013, 9:14pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/2 "2013-10-18T21:14:31Z")

</div>

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\>'

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 20, 2013, 1:21am UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/3 "2013-10-20T01:21:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 20, 2013, 5:12pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/4 "2013-10-20T17:12:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 20, 2013, 5:12pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/5 "2013-10-20T17:12:50Z")

</div>

Of course I meant YAML not JSON 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://forum.ansible.com/u/Brian_Candler)\
**Post date:** [October 20, 2013, 7:22pm UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/6 "2013-10-20T19:22:18Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 22, 2013, 1:14am UTC](https://forum.ansible.com/t/machines-in-multiple-groups/13720/7 "2013-10-22T01:14:14Z")

</div>

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.
