Ansible Collections: How to use Google Cloud Compute Engine inventory source

938 Views Asked by At

What are the correct commands for using the inventory file along with a playbook when using Ansible Collections google.cloud.gcp_compute. You can find an example of the inventory file very similar to what I'm using at the bottom of this article this article

I'm using this update.yml playbook:

- name: Update apt-get repo and cache
  apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

This is my inventory-gcp_compute.yml inventory file:

plugin: google.cloud.gcp_compute
zones: # populate inventory with instances in these regions
  - us-central1-a
projects:
  - vpn-server-sasp
auth_kind: serviceaccount
scopes:
  - 'https://www.googleapis.com/auth/cloud-platform'
  - 'https://www.googleapis.com/auth/compute.readonly'
keyed_groups:
  # Create groups from GCE labels
  - prefix: gcp
    key: labels
hostnames:
  # List host by name instead of the default public ip
  - name
compose:
  # Set an inventory parameter to use the Public IP address to connect to the host
  # For Private ip use "networkInterfaces[0].networkIP"
  ansible_host: networkInterfaces[0].accessConfigs[0].natIP

I've tried these commands:

ansible-playbook -i inventory-gcp_compute.yml update.yml

I got this error:

ansible-playbook 2.9.14
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/cheo/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.17 (default, Jul 20 2020, 15:37:01) [GCC 7.5.0]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as it did not pass its verify_file() method
virtualbox declined parsing /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 280, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/yaml.py", line 112, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml with constructed plugin: Incorrect plugin name in file: google.cloud.gcp_compute
  File "/usr/lib/python2.7/dist-packages/ansible/inventory/manager.py", line 280, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/constructed.py", line 109, in parse
    self._read_config_data(path)
  File "/usr/lib/python2.7/dist-packages/ansible/plugins/inventory/__init__.py", line 224, in _read_config_data
    raise AnsibleParserError("Incorrect plugin name in file: %s" % config.get('plugin', 'none found'))
[WARNING]: Unable to parse /home/cheo/sergio/ansible-gce/inventory-gcp_compute.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'apt' is not a valid attribute for a Play

The error appears to be in '/home/cheo/sergio/ansible-gce/update.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Update apt-get repo and cache
  ^ here

1

There are 1 best solutions below

0
On

Check the docs. It's looking for a file ending in .gcp_compute.yml or .gcp.yml, so while inventory.gcp_compute.yml would qualify, inventory-gcp_compute.yml does not. The errors you're getting says that that file isn't an inventory, but none of the plugin parsers claimed it.