无法在 Ansible jinja2 模板中检查大于条件的变量

Ash*_*har 4 comparison templates jinja2 conditional-statements ansible

我的 jinja2 模板为变量生成正确的值

{{ vars[fruit | join("")] | default('ERR') }}
Run Code Online (Sandbox Code Playgroud)

变量 Fruit 的值为 83.6,它由 Ansible 的模板模块打印。

我想在jinja2模板中编写一个if条件,我想检查变量fruit的值是否大于70

{% if ( vars[fruit | join("")] | int ) > 70 %}
Run Code Online (Sandbox Code Playgroud)

超过70个

{% 万一 %}

然而,当我期望它成功时,“如果”条件却失败了。

我还尝试了以下方法:

{% if ( vars[fruit | join("")] | int  > 70 ) %}
Run Code Online (Sandbox Code Playgroud)

我也尝试过

{% if vars[fruit | join("")] | int  > 70 %}
Run Code Online (Sandbox Code Playgroud)

但是,它们都不起作用。您能告诉我需要做什么才能满足 if 条件吗?

blh*_*ing 6

过滤int器不接受带点的字符串。您应该将其转换为浮点数,而不是使用round过滤器将其向下舍入:

{% if ( vars[fruit | join("")] | round(method='floor')) > 70 %}
Run Code Online (Sandbox Code Playgroud)