Search code examples
templatesansibleconditional-statementscomparisonjinja2

Unable to check variable greater than condition in Ansible jinja2 template


My jinja2 template yields correct value for the variable

{{ vars[fruit | join("")] | default('ERR') }}

The variable fruit has value 83.6 and it gets printed by Ansible's template module.

I wish to write an if condition in jinja2 template where I want to check if the value of the variable fruit is more than 70

{% if ( vars[fruit | join("")] | int ) > 70 %}

MORE THAN 70

{% endif %}

However, the 'if' the condition fails when I expect it to succeed.

I also tried the following:

{% if ( vars[fruit | join("")] | int  > 70 ) %}

I also tried

{% if vars[fruit | join("")] | int  > 70 %}

But, none of them worked. Can you please let me know what needs to be done to meet the if condition?


Solution

  • The int filter does not accept a string with a dot. You should convert it to a float instead by rounding it down with the round filter:

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