GCP inventory, inventory_hostname returning IP address

Hi.
I configured inventory according to the example [1]:

hostnames:

  • name
    compose:
    ansible_host: networkInterfaces[0].accessConfigs[0].natIP

And the inventory correctly uses hostname. But to my experience, the inventory_hostname variable (which should be the primary variable for setting the hostname) still contains the IP address. As a result, using that var for setting hostname ends up with something like 192 as machine name.
I tried several compose combination, but I’ve read in the docs this is a readonly variable and as such it cannot be manually set.

The only method I found so far to have a usable inventory_hostname is to comment the ansible_host directive in the compose and set the hostname in /etc/hosts, in order to allow Ansible to connect. But this way I’m losing the benefit of using GCP inventory.
So I was wondering what am I doing wrong.
Thanks

[1] https://docs.ansible.com/ansible/latest/collections/google/cloud/gcp_compute_inventory.html#examples

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.

Thanks for the feedback!

Unfortunately, I’m not that familiar with Python to grab the whole process, but I checked what you suggested, and at least in the REST I see when opening the instance detail, I don’t see the “hostname” key. I only have the “name” one. But that should be correct, according to the config above, it should take the hostname from the “name” attribute.

it will take inventory_hostname from that, but not ansible_hostname,
again, this is a fact gathered at runtime from the host.

The first variable represents the 'unique identifier' with which the
host was added to the inventory, the second is a result of running
gather_facts/setup on the remote system.

Thanks again for the feedback. I finally sorted it out, and of course it was a fault on my side.

I didn’t notice in the playbook someone had overridden hostname with:
hostname: “{{ ansible_hostname }}”
thus causing this behavior.