# GCP inventory, inventory\_hostname returning IP address

**URL:** <https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753>\
**Category:** Ansible Project\
**Tags:** gcp\
**Created:** [June 20, 2022, 6:16am UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753 "2022-06-20T06:16:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo\_Milesi](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Lorenzo\_Milesi](https://forum.ansible.com/u/Lorenzo_Milesi)\
**Post date:** [June 20, 2022, 6:16am UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753/1 "2022-06-20T06:16:28Z")

</div>

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](https://docs.ansible.com/ansible/latest/collections/google/cloud/gcp_compute_inventory.html#examples)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 21, 2022, 2:52pm UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753/2 "2022-06-21T14:52:35Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Milesi](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Lorenzo\_Milesi](https://forum.ansible.com/u/Lorenzo_Milesi)\
**Post date:** [June 23, 2022, 7:52am UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753/3 "2022-06-23T07:52:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 23, 2022, 3:19pm UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753/4 "2022-06-23T15:19:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lorenzo\_Milesi](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Lorenzo\_Milesi](https://forum.ansible.com/u/Lorenzo_Milesi)\
**Post date:** [July 1, 2022, 6:31am UTC](https://forum.ansible.com/t/gcp-inventory-inventory-hostname-returning-ip-address/36753/5 "2022-07-01T06:31:38Z")

</div>

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.
