Adding hosts to groups from the (yaml) host definition

964 views
Skip to first unread message

Dag Wieers

unread,
Jul 11, 2012, 5:47:37 AM7/11/12
to ansible...@googlegroups.com
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 ]

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

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

--
-- dag wieers, d...@wieers.com, http://dag.wieers.com/
-- dagit linux solutions, in...@dagit.net, http://dagit.net/

[Any errors in spelling, tact or fact are transmission errors]

Michael DeHaan

unread,
Jul 11, 2012, 8:23:55 AM7/11/12
to ansible...@googlegroups.com


On Wednesday, July 11, 2012 at 5:47 AM, Dag Wieers wrote:

> 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.


Michael DeHaan

unread,
Jul 11, 2012, 8:35:41 AM7/11/12
to ansible...@googlegroups.com
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.

Dag Wieers

unread,
Jul 11, 2012, 4:26:21 PM7/11/12
to ansible...@googlegroups.com
On Wed, 11 Jul 2012, Michael DeHaan wrote:

> 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 :)

Michael DeHaan

unread,
Jul 11, 2012, 4:37:43 PM7/11/12
to ansible...@googlegroups.com

On Wednesday, July 11, 2012 at 4:26 PM, Dag Wieers wrote:

On Wed, 11 Jul 2012, Michael DeHaan wrote:

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.

Dag Wieers

unread,
Jul 12, 2012, 10:10:11 AM7/12/12
to ansible...@googlegroups.com
On Wed, 11 Jul 2012, Dag Wieers wrote:

> 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 ]

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

https://github.com/ansible/ansible/pull/585

Michael DeHaan

unread,
Nov 20, 2012, 8:56:06 AM11/20/12
to ansible...@googlegroups.com
You can use YAML inventory if you really want to with this:

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.


On Mon, Nov 19, 2012 at 12:15 AM, Todd Wells <twell...@gmail.com> wrote:
> Per the below thread, this feature was added via the pull request
> https://github.com/ansible/ansible/pull/585 so that group membership can be
> defined like this:
>
>
> - system01
> groups: [ linux, prod, infra, distserver ]
>
> If the YAML inventory format is deprecated, how do we define/use this in the
> INI formatted file? Or does this definition go into a host_vars file?
> --
>
>

Todd Wells

unread,
Nov 20, 2012, 4:30:17 PM11/20/12
to ansible...@googlegroups.com
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 ]


Reply all
Reply to author
Forward
0 new messages