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