From c737bd48bc4ee246c378898abb80bacdd80c0e2f Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Wed, 24 Feb 2016 12:36:50 -0600 Subject: [PATCH] Deduplicate true duplicate entries in the openstack inventory There are cases where the host list back from the cloud comes back duplicated. This causes us to report those with UUIDs, which we do to support truly different servers with the same name. However, in the case where duplicate host entries have the same UUID, we can know it's a data hiccup. --- contrib/inventory/openstack.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/contrib/inventory/openstack.py b/contrib/inventory/openstack.py index b82a042c29..1c7207a9e1 100755 --- a/contrib/inventory/openstack.py +++ b/contrib/inventory/openstack.py @@ -112,6 +112,14 @@ def get_host_groups(inventory, refresh=False): return groups +def append_hostvars(hostvars, groups, key, server, namegroup=False): + hostvars[key] = dict( + ansible_ssh_host=server['interface_ip'], + openstack=server) + for group in get_groups_from_server(server, namegroup=namegroup): + groups[group].append(key) + + def get_host_groups_from_cloud(inventory): groups = collections.defaultdict(list) firstpass = collections.defaultdict(list) @@ -130,20 +138,19 @@ def get_host_groups_from_cloud(inventory): firstpass[server['name']].append(server) for name, servers in firstpass.items(): if len(servers) == 1 and use_hostnames: - server = servers[0] - hostvars[name] = dict( - ansible_ssh_host=server['interface_ip'], - openstack=server) - for group in get_groups_from_server(server, namegroup=False): - groups[group].append(server['name']) + append_hostvars(hostvars, groups, name, servers[0]) else: + server_ids = set() + # Trap for duplicate results for server in servers: - server_id = server['id'] - hostvars[server_id] = dict( - ansible_ssh_host=server['interface_ip'], - openstack=server) - for group in get_groups_from_server(server, namegroup=True): - groups[group].append(server_id) + server_ids.add(server['id']) + if len(server_ids) == 1 and use_hostnames: + append_hostvars(hostvars, groups, name, servers[0]) + else: + for server in servers: + append_hostvars( + hostvars, groups, server['id'], servers[0], + namegroup=True) groups['_meta'] = {'hostvars': hostvars} return groups