# Adding hosts to groups from the (yaml) host definition

**URL:** <https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661>\
**Category:** Ansible Project\
**Created:** [July 11, 2012, 9:47am UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661 "2012-07-11T09:47:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [July 11, 2012, 9:47am UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/1 "2012-07-11T09:47:37Z")

</div>

Hi,

There is a need to have multiple groups where it is expected that each system belongs exactly to one group (of a certain set). For example, if you have 3 environments (prod, qa and dev) each system should be in only one of those 3.

On the other hand, we may want to run a (set of) playbooks on a certain set of system (e.g. the production systems).

Is there a way to set such groups from the host definition, so that we do not have to maintain lists of host (per set of groups), e.g.

- system01  
&nbsp;&nbsp;&nbsp;groups: [linux, prod, infra, distserver]

where the groups would be resp. the os-group, the environment-group, the application-group and the servertype-group. The reason for having a separate group for the os is because we want to match certain plays based on a group (I don't think this is possible based on facts ?)

&nbsp;&nbsp;&nbsp;os-group consists of: linux, aix, solaris  
&nbsp;&nbsp;&nbsp;environment-group consists of: prod, qa, dev  
&nbsp;&nbsp;&nbsp;application-group consists of: about 120 different codes  
&nbsp;&nbsp;&nbsp;servertype-group consists of: distserver, dbserver, webserver, custom

The examples in the documentation expects one to keep seperate lists of hosts per group which is impossible to maintain properly.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 11, 2012, 12:23pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/2 "2012-07-11T12:23:55Z")

</div>

> Hi,
> 
> There is a need to have multiple groups where it is expected that each  
> system belongs exactly to one group (of a certain set). For example, if  
> you have 3 environments (prod, qa and dev) each system should be in only  
> one of those 3.
> 
> On the other hand, we may want to run a (set of) playbooks on a certain  
> set of system (e.g. the production systems).
> 
> Is there a way to set such groups from the host definition, so that we do  
> not have to maintain lists of host (per set of groups), e.g.

> - system01  
> groups: [linux, prod, infra, distserver]

no, the parser is group first in terms of syntax. I don't like the idea of that being ambitious and supporting both, but you could definitely write  
an external inventory script to return the data however you like (feed a chmod +x script to ansible's -i).

> where the groups would be resp. the os-group, the environment-group, the  
> application-group and the servertype-group. The reason for having a  
> separate group for the os is because we want to match certain plays based  
> on a group (I don't think this is possible based on facts ?)
> 
> os-group consists of: linux, aix, solaris  
> environment-group consists of: prod, qa, dev  
> application-group consists of: about 120 different codes  
> servertype-group consists of: distserver, dbserver, webserver, custom

Yep, sounds like you may want to write your own inventory script to read them in how you wish to define them.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 11, 2012, 12:35pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/3 "2012-07-11T12:35:41Z")

</div>

One thing I should also add is that for users with clear differences between environments (stage, production, QA, etc) -- it is easy to maintain multiple inventory files.

This prevents addressing production when you want to talk to stage.

ansible -i /etc/ansible/stage\_hosts /path/to/playbooks/some\_playbook\_affecting\_stage.yml

In other words, I would not want to keep all my application servers for QA defined with my production ones in the same file, and it's not required that you do.

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [July 11, 2012, 8:26pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/4 "2012-07-11T20:26:21Z")

</div>

But how would one reuse the same definitions for the groups ? A webserver in production should have the same variables as a webserver in QA. But having different hosts files for each would require one to duplicate those definitions. Or I miss something 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 11, 2012, 8:37pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/5 "2012-07-11T20:37:43Z")

</div>

> > One thing I should also add is that for users with clear differences  
> > between environments (stage, production, QA, etc) – it is easy to  
> > maintain multiple inventory files.
> > 
> > This prevents addressing production when you want to talk to stage.
> > 
> > ansible -i /etc/ansible/stage\_hosts /path/to/playbooks/some\_playbook\_affecting\_stage.yml
> > 
> > In other words, I would not want to keep all my application servers for  
> > QA defined with my production ones in the same file, and it’s not  
> > required that you do.
> 
> But how would one reuse the same definitions for the groups ? A webserver  
> in production should have the same variables as a webserver in QA. But  
> having different hosts files for each would require one to duplicate those  
> definitions. Or I miss something 🙂

Stick the variables in your playbook. Playbooks are all about telling what particular groups of systems should behave like.

I generally see inventory variables as for being for things about IP addresses that are location specific.

- hosts: webservers  
vars:
- …

And you could actually use that same playbook between different inventory files if you wanted.

If you needed to assign environment specific vars, you could do something (I think) like:

[groupname:vars]  
x=2  
y=3  
z=4

Assigning the environment specific vars in the inventory file, using the group.

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [July 12, 2012, 2:10pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/6 "2012-07-12T14:10:11Z")

</div>

As to not being accused of asking for more ponies, here's the pull-request 😉

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://github.com/ansible/ansible/pull/585](https://github.com/ansible/ansible/pull/585)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 20, 2012, 1:56pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/7 "2012-11-20T13:56:06Z")

</div>

You can use YAML inventory if you really want to with this:

[https://github.com/ansible/ansible/blob/devel/plugins/inventory/yaml.py](https://github.com/ansible/ansible/blob/devel/plugins/inventory/yaml.py)

just use this as your inventory file by chmod +x'ing it

host\_vars seeing it only sets vars (not groups) won't do what you want.

---

<div class="post-metadata">

**Author:** ![Todd\_Wells](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@Todd\_Wells](https://forum.ansible.com/u/Todd_Wells)\
**Post date:** [November 20, 2012, 9:30pm UTC](https://forum.ansible.com/t/adding-hosts-to-groups-from-the-yaml-host-definition/11661/8 "2012-11-20T21:30:17Z")

</div>

Thanks, yaml inventory now works, but the defined methods in the yaml.py file for defining groups under a host or in format do not appear to work with this file.

twells@twlaptop:/etc/ansible$ ./hosts -l  
{“ungrouped”: [“localhost”, “system01”], “prod”: [“myhost1”, “myhost2”]}  
twells@twlaptop:/etc/ansible$ cat hosts.yml

- host: localhost  
vars:

- myvar: value

- myvbr: vblue  
groups:

- mygroup1

- mygroup2

- group: prod  
vars:

- groupvar: value  
hosts:

- myhost1

- myhost2

- host: system01  
groups: [linux, production, dbserver]
