From a96f6813522c5ae8b2be4514a2de56a775c6b7b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag=20Wie=C3=ABrs?= Date: Thu, 12 Jul 2012 12:30:43 +0200 Subject: [PATCH] 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 ] ---- --- lib/ansible/inventory/yaml.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/ansible/inventory/yaml.py b/lib/ansible/inventory/yaml.py index 0c2853ad944..c63a975d098 100644 --- a/lib/ansible/inventory/yaml.py +++ b/lib/ansible/inventory/yaml.py @@ -106,6 +106,7 @@ class InventoryParserYaml(object): elif type(item) == dict and 'host' in item: host = self._make_host(item['host']) + vars = item.get('vars', {}) if type(vars)==list: varlist, vars = vars, {} @@ -113,5 +114,23 @@ class InventoryParserYaml(object): vars.update(subitem) for (k,v) in vars.items(): 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: 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]