首页 > 解决方案 > Ansible Collections:如何使用 Google Cloud Compute Engine 库存源

问题描述

在使用 Ansible Collections google.cloud.gcp_compute 时,使用清单文件和剧本的正确命令是什么。您可以在本文底部找到与我使用的非常相似的清单文件示例

我正在使用这个update.yml剧本:

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

这是我的inventory-gcp_compute.yml库存文件:

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

我试过这些命令:

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

我收到了这个错误:

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

标签: ansibleansible-inventory

解决方案


检查文档。它正在寻找以 .gcp_compute.yml 或 .gcp.yml 结尾的文件,因此虽然 inventory.gcp_compute.yml 符合条件,但是 inventory-gcp_compute.yml 没有。您收到的错误表明该文件不是清单,但没有一个插件解析器声称它。


推荐阅读