ansible-collections / ansible-collections/google.cloud

gcp_compute inventory: hostname fallback should prefer private_ip over instance name

Closed Beginner friendly
#741 1 comment 0 reactions 0 assignees View on GitHub
stale
Dominant language
Python
Stars
105
Forks
144
Avg merge
5d 10h
Merged PRs (30d)
4

Description

##### SUMMARY

Improve gcp_compute hostname resolution by preferring private_ip instead of instance name when hostname is not set
##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
gcp_compute inventory plugin

##### ADDITIONAL INFORMATION
Currently, when `hostnames` includes hostname and a GCE instance has no hostname set, the plugin falls back to using the instance name:
```
elif order == "hostname":
name = self.json.get("hostname", self.json["name"] + self.name_suffix)
```
This causes issues in common real-world setups:

- GCE instances are often created without a hostname, especially in environments where DNS is not yet available.
- Instance name is not guaranteed to be resolvable, and often is not even a valid DNS label.
- As a result, Ansible inventory generation succeeds, but connections fail because the chosen hostname can never resolve.

At the same time, private_ip is:

- always present on GCE instances
- always routable inside the VPC
- a much safer and more predictable fallback for Ansible connections

A better fallback behavior would be:

```
hostname → private_ip → name
```

or at least avoiding a direct fallback from hostname to name.

This change would improve usability and reduce confusing connection failures without breaking valid configurations, since setups relying on instance name resolution are already fragile.

I think we can use this very easy fix
```
elif order == "hostname":
name = self.json.get("hostname")
```

Contributor guide

Open the contributing guide

Research direction

Start at the shown `elif order == "hostname"` branch in the gcp_compute inventory plugin and inspect how instance fields are read. Add coverage for an instance without a hostname, then verify the fallback order is hostname, private_ip, and finally name; done means inventory generation selects a usable fallback without breaking configured hostnames.

Written by the indexing model from the issue text.

Assessment

Tech stack
ansible, gcp, python
Domain
cloud, devops
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.