Ansible 尝试动态形成条件时在“when”内模板化的替代方法

Ansible 尝试动态形成条件时在“when”内模板化的替代方法,ansible,ansible-2.x,ansible-template,Ansible,Ansible 2.x,Ansible Template,我试图通过声明以下变量动态创建一个条件 in_标记:在ansible_运行标记中或在ansible_运行标记中的“全部” 在这样的情况下使用它 -包括:zsh.yml 当:“'zsh”{{in_tags}}” 然而,当我这样做时,Ansible给出了以下警告 [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "

我试图通过声明以下变量动态创建一个条件

in_标记:在ansible_运行标记中或在ansible_运行标记中的“全部”
在这样的情况下使用它

-包括:zsh.yml
当:“'zsh”{{in_tags}}”
然而,当我这样做时,Ansible给出了以下警告

[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {%
%}. Found: "zsh" {{ in_tags }}
删除
{{}
会导致此操作失败

fatal: [localhost]: FAILED! => {"msg": "The conditional check '\"zsh\" in_tags' failed. The error was: template error while templating string: expected token 'end of statement block', got 'in_tags'. String: {% if \"zsh\" in_tags %} True {% else %} False {% endif %}\n\nThe error appears to be in '[REDACTED]/playbook/roles/fzf/tasks/zsh.yml': line 1, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Ensure zsh integration\n  ^ here\n"}

有没有更好的方法来编写此代码,以避免在条件语句中使用模板?

为了在
块中实现动态求值,当
块时,这样做是否正确

mkdir测试插件

cat>test\u plugins/my\u when\u test.py您是否尝试过
debug
in\u标签中的
变量?里面有
zsh
吗?这非常有帮助,我想这可能是我能做的最接近我想做的事情,而不会有那个恼人的警告。我真的希望有一种方法可以将它作为部分函数存储在某个地方,比如让测试返回lambda,但这似乎并不奇怪。不过非常感谢!顺便说一下,您有一个小的输入错误,您将测试定义为
my\u when\u test
,然后尝试将其引用为
my\u when\u filter
@AdrianOrdonez您可以自己编辑答案来更正它。但看起来在你评论之前就已经完成了:)@Zeitounator哦,我不知道。谢谢你让我知道!=)