Skip to content

gcp_compute inventory fails due to instances with STOPPING status #724

@jgtr-blis

Description

@jgtr-blis
SUMMARY

The gcp_compute ansible inventory plugin fails to parse the current GCE instances and fails if there are any instances with filter status = STOPPING.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

gcp_compute ansible inventory plugin

ANSIBLE VERSION
ansible [core 2.18.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
Collection   Version
------------ -------
google.cloud 1.9.0 
CONFIGURATION
CONFIG_FILE() = /etc/ansible/ansible.cfg

GALAXY_SERVERS:
OS / ENVIRONMENT

Ubuntu 24.04.3 LTS

STEPS TO REPRODUCE
  1. Create a gcp_plugin inventory file similar to the one below with either filters: [] or a filter on status = STOPPING as shown below
  2. Confirm that you have instances in the STOPPING state via gcloud compute instances list
  3. Run ansible-inventory -vvvv -i inv.gcp.yml --graph

Note: In my situation the instances in the project that were starting/stopping frequently were due to one zone being out of resources so it's possible that this scenario results in unusual values being returned from the compute API call which breaks the plugin parsing causing it to fail. I have however experienced intermittent errors in the past with projects using no status filter and I believe this is likely the cause of those as instance groups in our projects have autoscaling so will periodically stop instances breaking the plugin.

plugin: gcp_compute

projects:
  - your-gcp-project-here
filters:
  - status = STOPPING
hostnames:
  - private_ip
groups:
  app: "'app' == labels['deployment']"
scopes:
  - https://www.googleapis.com/auth/compute
auth_kind: application
EXPECTED RESULTS

The command should return a valid inventory list containing one or more instances and not a cryptic error suggesting that the plugin yaml config file is invalid.

ACTUAL RESULTS

The command fails with the error message suggesting that there is an issue with the yaml config file which is misleading. Running the command when there are no stopping instances in the project is successful. Changing to a filter of status = RUNNING (or any of the other possible states with the exception of STOPPING) means the command runs as expected.

ansible-inventory [core 2.18.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/user/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-inventory
  python version = 3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
Loading collection ansible.builtin from 
host_list declined parsing /home/user/test/inv.gcp.yml as it did not pass its verify_file() method
script declined parsing /home/user/test/inv.gcp.yml as it did not pass its verify_file() method
redirecting (type: inventory) ansible.builtin.gcp_compute to google.cloud.gcp_compute
Loading collection google.cloud from /usr/lib/python3/dist-packages/ansible_collections/google/cloud
Using inventory plugin 'ansible_collections.google.cloud.plugins.inventory.gcp_compute' to process inventory source '/home/user/test/inv.gcp.yml'
toml declined parsing /home/user/test/inv.gcp.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/user/test/git-
infrastructure2/inv.gcp.yml with auto
plugin: No valid name found for host
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 292, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/auto.py", line 58, in parse
    plugin.parse(inventory, loader, path, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible_collections/google/cloud/plugins/inventory/gcp_compute.py", line 625, in parse
    self._add_hosts(value, config_data, project_disks=project_disks)
  File "/usr/lib/python3/dist-packages/ansible_collections/google/cloud/plugins/inventory/gcp_compute.py", line 444, in _add_hosts
    self._populate_host(host)
  File "/usr/lib/python3/dist-packages/ansible_collections/google/cloud/plugins/inventory/gcp_compute.py", line 311, in _populate_host
    hostname = item.hostname()
               ^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/ansible_collections/google/cloud/plugins/inventory/gcp_compute.py", line 257, in hostname
    raise AnsibleParserError("No valid name found for host")
[WARNING]:  * Failed to parse /home/user/test/git-
infrastructure2/inv.gcp.yml with yaml
plugin: Plugin configuration YAML file, not YAML inventory
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 292, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/yaml.py", line 113, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/user/test/git-
infrastructure2/inv.gcp.yml with ini
plugin: Invalid host pattern 'plugin:' supplied, ending in ':' is not allowed,
this character is reserved to provide a port.
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 292, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/ini.py", line 137, in parse
    raise AnsibleParserError(e)
ERROR! Completely failed to parse inventory source /home/user/test/inv.gcp.yml

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions