【发布时间】:2022-10-24 16:18:07
【问题描述】:
我正在尝试将适用于 Windows 和 Linux 的剧本放在一起。现在我试图在剧本中包含只有在 Windows 或 Linux 下才会使用的角色,但它总是抱怨语法。我很感激这方面的任何帮助,因为我尝试了几种不同的方法,但总是失败。
---
- hosts: all
gather_facts: no
pre_tasks:
- name: (localhost) make sure the known_hosts file is cleared
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) print out some debug message to confirm inventory is properly set up
debug:
msg: "System inventory_hostname:{{ inventory_hostname }} ansible_host:{{ ansible_host }}"
- hosts: all
tasks:
- name: Install CA Trust Certs Windows
include_tasks: tasks\install-certs-windows.yml
when: ansible_os_family == 'Windows'
- name: Install CA Trust Certs Linux
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
【问题讨论】:
-
你能提供错误输出吗?
-
去掉花括号,将when设置为同级角色
-
您还要求尝试在路径名中使用反斜杠; ansible 本身总是在 Linux 上运行,即使它针对的是 Windows 主机:
include_tasks: tasks/install-certs-windows.yml -
@Khaled 但这将消除拥有标签的可能性,不是吗?
-
@mdaniel 是的,好点。谢谢
标签: ansible