Ansible:在剧本中使用过滤器作为条件来包含角色

Ansible: Use filter as conditional in playbook for role-inclusion

我们有几个角色要根据版本条件在 Playbook 中执行。由于此角色在许多 Playbook 中使用,因此在角色本身中进行条件检查似乎不正确,因为这会导致细节泄漏到角色中。

我们想做的事情:

    - name: Provisioninging for Windows Appservers
       hosts: WindowsAppservers
       roles:
       - { role: DotNet471, when: arenaVersion is version('18.1','>=', strict=False)  }

但是失败并出现以下错误:

FAILED! => {"msg": "The conditional check 'arenaVersion is version('18.1'' failed. The error was: template error while templating string: unexpected '}', expected ')'. String: {% if arenaVersion is version('18.1' %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/mnt/c/Code/Git/AnsiblePlaybooks/roles/DotNet471/tasks/main.yaml': line 1, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - name: Install DotNet Framework 4.7.1\n     ^ here\n"}

似乎 version filter 中的 , 符号解析不正确?

比较字符串工作正常,像这样:

- { role: VCRedist140, when: arenaVersion|string() ==  "17.2" }

我的 Ansible 版本是 2.6.2

您使用不带引号的 YAML 字符串,因此 when: 字符串以以下 ,.

结尾

引用完整 when 语句:

- { role: DotNet471, when: "arenaVersion is version('18.1','>=', strict=False)" }