Compose happens to late to set inventory_hostname, this is not a
'settable variable' but a variable that reflects the name with which
the host was added to inventory.
Being able to choose what to use as inventory_hostname depends on the
inventory plugin,
many allow you to set inventory_hostname others do not.
In this case, the gcp_compute inventory plugin does allow it, and from
the docs it should work with 'name'
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'.
default: ['public_ip', 'private_ip', 'name']
But docs can be wrong, so I looked at the code:
https://github.com/ansible-collections/google.cloud/blob/0cd64a4a68b7d4393c97b801e3f24e6b63882294/plugins/inventory/gcp_compute.py#L231,
this does seem to use the hostnames options/ordering to assign the
name. That code is then used to create the host
https://github.com/ansible-collections/google.cloud/blob/0cd64a4a68b7d4393c97b801e3f24e6b63882294/plugins/inventory/gcp_compute.py#L302
(which sets inventory_hostname to hostname()).
So the only thing I can think of .. the API is not returning 'name' as
part of the host information.
----------
Brian Coca