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

Github Actions中运行Ansible Playbook无法读取hosts.yaml清单文件

Ansible清单文件在Github Actions中解析失败问题

我正把自动化流程从Jenkins迁移到Github Actions,大量自动化任务依赖Ansible Playbook。Ansible需要通过inventory清单文件确定主机及其变量,但在Github Actions中运行时出现如下错误:

Warning: :  * Failed to parse /__w/aws-infra/aws-infra/inventories/hosts.yaml with auto plugin: no root 'plugin' key found, '/__w/aws-infra/aws-infra/inventories/hosts.yaml' is not a valid YAML inventory plugin config file
Warning: :  * Failed to parse /__w/aws-infra/aws-infra/inventories/hosts.yaml with yaml plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: Expecting property name enclosed in double quotes: line 1 column 25 (char 24)  Syntax Error while loading YAML.   did not find expected ',' or '}'  The error appears to be in '<string>': line 2, column 40, but may be elsewhere in the file depending on the exact syntax problem.
Warning: :  * Failed to parse /__w/aws-infra/aws-infra/inventories/hosts.yaml with ini plugin: We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: Expecting property name enclosed in double quotes: line 1 column 25 (char 24)  Syntax Error while loading YAML.   did not find expected ',' or '}'  The error appears to be in '<string>': line 2, column 40, but may be elsewhere in the file depending on the exact syntax problem.
Warning: : Unable to parse /__w/aws-infra/aws-infra/inventories/hosts.yaml as an inventory source
Warning: : No inventory was parsed, only implicit localhost is available
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting property name enclosed in double quotes: line 1 column 25 (char 24)

我的流水线步骤配置如下:

- name: run ansible
  run: |
    ansible-playbook -i ./inventories/hosts.yaml \
    ./create_cluster.yaml

我猜测问题可能源于容器内的权限设置,当前使用自定义Ubuntu镜像(已构建至ghcr并在流水线启动时拉取),该镜像包含Jenkins环境运行所需的全部依赖。

已尝试的排查措施:

  • 将老旧Ansible版本升级至最新版
  • 使用yamllint、ansible-lint检查hosts.yaml文件
  • 在Github Actions中成功执行ansible-inventory --graph命令
  • 提升流水线权限为全写
  • 尝试多种清单文件路径,如${{ github.workspace }}/、./及简单路径
  • 查阅大量Stack Overflow帖子与谷歌搜索结果

预期结果:Ansible能够正常读取清单文件并执行任务。

补充说明:
hosts.yaml文件内容格式符合官方文档规范(因未使用独立Ansible配置文件,故以all作为顶层节点):

all:
  children:
    development:
      hosts:
        host1:
        host2:
    other-category:
      hosts:
        host1:
        host2:

在Github Actions中运行时仍触发上述错误,转换为JSON格式后问题依旧。

版本信息:

  • Ansible-collections: 8.6.0
  • Ansible-core: 2.16.6
  • Ansible: 9.5.1
  • Python3等依赖为ubuntu:22.04环境下的最新版本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 03:05:16