diff --git a/lib/ansible/plugins/inventory/gcp_compute.py b/lib/ansible/plugins/inventory/gcp_compute.py index 4e1310e6fac..861ebd29038 100644 --- a/lib/ansible/plugins/inventory/gcp_compute.py +++ b/lib/ansible/plugins/inventory/gcp_compute.py @@ -236,8 +236,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if navigate_hash(result, ['error', 'errors']): module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) - if result['kind'] != 'compute#instanceAggregatedList' and result['kind'] != 'compute#zoneList': - module.fail_json(msg="Incorrect result: {kind}".format(**result)) return result @@ -260,6 +258,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if 'subnetwork' in network: network['subnetwork'] = self._format_network_info(network['subnetwork']) + if 'metadata' in host: + # If no metadata, 'items' will be blank. + # We want the metadata hash overriden anyways for consistency. + host['metadata'] = self._format_metadata(host['metadata'].get('items', {})) + host['project'] = host['selfLink'].split('/')[6] host['image'] = self._get_image(host, project_disks) return items @@ -300,6 +303,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): 'selfLink': address } + def _format_metadata(self, metadata): + ''' + :param metadata: A list of dicts where each dict has keys "key" and "value" + :return a dict with key/value pairs for each in list. + ''' + new_metadata = {} + print(metadata) + for pair in metadata: + new_metadata[pair["key"]] = pair["value"] + return new_metadata + def _get_hostname(self, item): ''' :param item: A host response from GCP