【问题标题】:Ansible playbook conditionalsAnsible 剧本条件
【发布时间】: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


【解决方案1】:

一句话:不要

好吧,如果你想对宫缩挑剔的话,那就是两个词。

无论如何....按操作系统对您的服务器进行分组,然后分别处理它们:

---
- hosts: Linux
  gather_facts: no
# Do Linux stuff

- hosts: Windoze
  serial: 1
# Do Windoze stuff

【讨论】:

  • 这实际上是有趣的一点@Jack我需要为它创建单独的库存还是它会自行确定?
猜你喜欢
  • 1970-01-01
  • 2020-05-08
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
相关资源
最近更新 更多