Allow groups to be defined on a per-host basis

This makes it possible to define on a per-host basis what groups a host is in.
When managing a large set of systems it makes it easier to ensure each of the
systems is defined in a set of groups (e.g. production/qa/development,
linux/solaris/aix) rather than having to add systems to multiple disconnected
groups.

----
  - host: system01

  - host: system02

  - host: system03

  - group: linux
    hosts:
    - system01
    - system02

  - group: solaris
    hosts:
    - system03

  - group: production
    hosts:
    - system01
    - system03

  - group: qa
    - system02

  - group: dbserver
    hosts:
    - system01

  - group: ntpserver
    hosts:
    - system02

  - group: webserver
    - system03
----

Can be redefined as:

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

  - host: system02
    groups: [ linux, qa, ntpserver ]

  - host: system03
    groups: [ solaris, production, webserver ]
----
This commit is contained in:
Dag Wieërs 2012-07-12 12:30:43 +02:00
parent 35df9621e2
commit a96f681352

View file

@ -106,6 +106,7 @@ class InventoryParserYaml(object):
elif type(item) == dict and 'host' in item: elif type(item) == dict and 'host' in item:
host = self._make_host(item['host']) host = self._make_host(item['host'])
vars = item.get('vars', {}) vars = item.get('vars', {})
if type(vars)==list: if type(vars)==list:
varlist, vars = vars, {} varlist, vars = vars, {}
@ -113,5 +114,23 @@ class InventoryParserYaml(object):
vars.update(subitem) vars.update(subitem)
for (k,v) in vars.items(): for (k,v) in vars.items():
host.set_variable(k,v) host.set_variable(k,v)
groups = item.get('groups', {})
if type(groups) in [ str, unicode ]:
groups = [ groups ]
if type(groups)==list:
for subitem in groups:
if subitem in self.groups:
group = self.groups[subitem]
else:
group = Group(subitem)
self.groups[group.name] = group
all.add_child_group(group)
group.add_host(host)
grouped_hosts.append(host)
if host not in grouped_hosts: if host not in grouped_hosts:
ungrouped.add_host(host) ungrouped.add_host(host)
# make sure ungrouped_hosts is the complement of grouped_hosts
ungrouped_hosts = [host if host not in grouped_hosts for host in ungrouped_hosts]