Adding hosts to groups from the (yaml) host definition

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.

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.

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

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

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.

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

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

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.

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 ]