diff --git a/plugins/inventory/gcp_compute.py b/plugins/inventory/gcp_compute.py index 1f31e0242..333249b22 100644 --- a/plugins/inventory/gcp_compute.py +++ b/plugins/inventory/gcp_compute.py @@ -45,7 +45,9 @@ hostnames: description: A list of options that describe the ordering for which hostnames should be assigned. Currently supported hostnames are - 'public_ip', 'private_ip', 'name' or 'labels.vm_name'. + 'public_ip', 'private_ip', 'name', 'labels.vm_name' or 'hostname'. + As 'hostname' may not be defined, + it will fallback on 'name'. default: ['public_ip', 'private_ip', 'name'] type: list auth_kind: @@ -238,6 +240,9 @@ def hostname(self): name = self._get_privateip() elif order == "name": name = self.json[u"name"] + elif order == "hostname": + # If hostname is not defined, then fallback on name + name = self.json[u"hostname"] if "hostname" in self.json else self.json[u"name"] else: raise AnsibleParserError("%s is not a valid hostname precedent" % order)