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

时间:2018-11-12 15:19:05

标签: filter ansible conditional version jinja2

我们希望根据版本条件在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

1 个答案:

答案 0 :(得分:2)

您使用未加引号的YAML字符串,因此when:字符串以以下,结尾。

引用完整的when语句:

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