You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible GCP Compute库存文件无法被Playbook解析求助

Ansible GCP Compute库存解析失败排查

问题情况

  • GCP Compute库存文件无法在任何Playbook中正常工作,包括此前运行正常的Playbook
  • 库存文件结构简单,Ansible已更新至最新版本
  • 凭证文件在Terraform配置中可正常使用,确认凭证有效
  • 解析库存时出现多种错误,最终无法识别库存源

库存文件内容

plugin: gcp_compute
projects:
  - projectName
auth_kind: serviceaccount
service_account_file: /home/.../.config/gcloud/application_default_credentials.json
keyed_groups:
  - prefix: gcp
    key: labels
  - prefix: tags
    key: tags

错误日志

config file = None
configured module search path = ['/home/.../.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/.../.local/lib/python3.10/site-packages/ansible
ansible collection location = /home/.../.ansible/collections:/usr/share/ansible/collections
executable location = /home/.../.local/bin/ansible-playbook
python version = 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] (/usr/bin/python3)
jinja version = 3.0.3
libyaml = True
No config file found; using defaults
host_list declined parsing /home/.../inventory.gcp.yaml as it did not pass its verify_file() method
redirecting (type: inventory) ansible.builtin.gcp_compute to google.cloud.gcp_compute
Using inventory plugin 'ansible_collections.google.cloud.plugins.inventory.gcp_compute' to process inventory source '/home/.../inventory.gcp.yaml'
toml declined parsing /home/.../inventory.gcp.yaml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/.../inventory.gcp.yaml with script plugin: problem running
/home/.../inventory.gcp.yaml --list ([Errno 8] Exec format error:
'/home/.../inventory.gcp.yaml')
  File "/home/.../.local/lib/python3.10/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
 ...
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/.../.local/lib/python3.10/site-packages/ansible/plugins/inventory/yaml.py", line 114, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/.../inventory.gcp.yaml with ini plugin: Invalid host pattern
'plugin:' supplied, ending in ':' is not allowed, this character is reserved to provide a port.
  File "/home/.../.local/lib/python3.10/site-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/.../.local/lib/python3.10/site-packages/ansible/plugins/inventory/ini.py", line 137, in parse
    raise AnsibleParserError(e)
[WARNING]: Unable to parse /home/.../inventory.gcp.yaml 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 

排查建议

  • 安装/更新google.cloud集合:日志显示Ansible将ansible.builtin.gcp_compute重定向到google.cloud.gcp_compute,若未安装该集合会导致解析失败。执行命令:
    ansible-galaxy collection install google.cloud
    
  • 验证库存文件命名与插件识别:确保库存文件名能被Ansible识别为插件配置文件,可尝试将文件名改为inventory.gcp.yml(部分环境对.yaml后缀识别存在问题)
  • 运行详细库存测试:用以下命令获取更详细的解析日志,定位具体错误点:
    ansible-inventory -i inventory.gcp.yaml --list -vvv
    
  • 检查服务账号文件路径与权限:确保service_account_file中的路径是完整的实际路径(替换掉...),且Ansible运行用户对该文件有读取权限
  • 确认服务账号权限:确保服务账号拥有compute.instances.list权限(GCP库存插件需要此权限来列出实例),可在GCP IAM控制台为账号添加Compute Viewer角色
  • 配置Ansible启用插件:创建ansible.cfg文件,指定启用GCP库存插件:
    [inventory]
    enable_plugins = google.cloud.gcp_compute
    
  • 验证插件可用性:执行以下命令确认google.cloud.gcp_compute插件可正常加载:
    ansible-doc -t inventory google.cloud.gcp_compute
    

内容的提问来源于stack exchange,提问作者VinDiesel_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:40:25