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

Ansible跨Windows/Linux剧本角色条件引用语法错误求助

问题描述

编写同时适配Windows和Linux系统的Ansible Playbook时,尝试根据操作系统类型条件性引入角色,始终遭遇语法报错,多次调整写法均失败,附上相关代码及报错信息求助。

原Playbook代码

---
- hosts: all
  gather_facts: no
  pre_tasks:
    - name: (localhost) 清空known_hosts文件
      lineinfile:
        path: ~/.ssh/known_hosts
        state: absent
        regexp: "^{{ ansible_host | replace('.', '\\\\.') }}.*$"
      delegate_to: 127.0.0.1

- hosts: all
  serial: 1
  pre_tasks:
    - name: (debug) 打印调试信息确认Inventory配置
      debug:
        msg: "System inventory_hostname:{{ inventory_hostname }} ansible_host:{{ ansible_host }}"

- hosts: all
  tasks:
  - name: 安装Windows CA信任证书
    include_tasks: tasks\\install-certs-windows.yml
    when: ansible_os_family == 'Windows'

  - name: 安装Linux CA信任证书
    include_tasks: tasks/install-certs-linux.yml
    when: ansible_os_family != 'Windows'

  roles:
  - { role: ansible-role-runnersbasics, tags: ["basics"] }
  - { role: ansible-role-docker, tags: ["docker"] }
  - { role: ansible-role-gitlab-runner }
    when: ansible_os_family == 'Windows'

首次报错信息

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  did not find expected key

The error appears to be in 'playbook.yml': line 33, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  - { role: ansible-role-gitlab-runner }
    when: ansible_os_family == 'Windows'
    ^ here

修改写法后的报错信息

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in 'playbook.yml': line 30, column 43, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  roles:
    role: ansible-role-runnersbasics, tags: ["basics"] }
                                          ^ here

错误原因及修复方案

1. 首次报错的核心问题

YAML语法规则要求:角色的条件判断when必须和角色定义放在同一个映射(字典)结构中,不能单独作为下一行的独立键。原写法中- { role: ansible-role-gitlab-runner }是一个完整的列表项,下一行的when无法被解析为该角色的属性,导致语法错误。

修复方式:将when条件合并到角色的字典定义中,两种写法任选其一:

  • 紧凑字典格式:
    - { role: ansible-role-gitlab-runner, when: ansible_os_family == 'Windows' }
    
  • 展开多行格式(可读性更强):
    - role: ansible-role-gitlab-runner
      when: ansible_os_family == 'Windows'
    

2. 修改后报错的问题

修改时破坏了YAML字典的语法结构,正确的角色列表写法要么用完整的{ key: value, key: value }格式,要么用多行展开格式,不能混合错误的括号和逗号。

3. 额外注意事项

你的Playbook中设置了gather_facts: no,但后续用到了ansible_os_family这个系统事实变量,这会导致变量未定义的错误。必须开启事实收集:

  • 要么删除gather_facts: no(默认值为yes)
  • 要么在pre_tasks中手动添加事实收集任务:
    - name: 收集系统事实信息
      setup:
    

修复后的完整角色部分代码

roles:
  - { role: ansible-role-runnersbasics, tags: ["basics"] }
  - { role: ansible-role-docker, tags: ["docker"] }
  - role: ansible-role-gitlab-runner
    when: ansible_os_family == 'Windows'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 21:15:44