From e36e2d38fee397a0df06c7b848a4a01426764e14 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Fri, 4 Apr 2014 09:27:44 +0200 Subject: [PATCH] InventoryDir: another fix for the host.groups list In some cases, where a host is mentioned in multiple groups, and those groups are referenced in multiple ini files, a group could still contain multiple instances of a group in its host,groups list, where only one of them is the right group, that exists in the inventory. --- lib/ansible/inventory/dir.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/ansible/inventory/dir.py b/lib/ansible/inventory/dir.py index f2c32a063e1..2f56181ee2b 100644 --- a/lib/ansible/inventory/dir.py +++ b/lib/ansible/inventory/dir.py @@ -171,8 +171,15 @@ class InventoryDirectory(object): # same host name but different object, merge self._merge_hosts(grouphosts[host.name], host) else: - # new membership + # new membership, add host to group from self + # group from self will also be added again to host.groups, but + # as different object group.add_host(self.hosts[host.name]) + # now remove this the old object for group in host.groups + for hostgroup in [g for g in host.groups]: + if hostgroup.name == group.name and hostgroup != self.groups[group.name]: + self.hosts[host.name].groups.remove(hostgroup) + # group child membership relation for newchild in newgroup.child_groups: @@ -204,6 +211,18 @@ class InventoryDirectory(object): if host.name != newhost.name: raise errors.AnsibleError("Cannot merge host %s with %s" % (host.name, newhost.name)) + # group membership relation + for newgroup in newhost.groups: + # dict with existing groups: + hostgroups = dict([(g.name, g) for g in host.groups]) + # check if new group is already known as a group + if newgroup.name not in hostgroups: + if newgroup.name not in self.groups: + # group does not exist yet in self, import him + self.groups[newgroup.name] = newgroup + # group now exists but doesn't have host yet + self.groups[newgroup.name].add_host(host) + # variables host.vars = utils.combine_vars(host.vars, newhost.vars)