IF/Else-Statement at vars

Hi community,
I heard somewhere that you shouldn’t use an if-else statement in jinja2 format inside a variable. Unfortunately, I can’t find anything about this in any best practices guide.

Here’s a code example:

-  ansible.builtin.set_fact:
    myvar: "{{'value' if (my_group_var) else 'other_value'}}"

Thank you for your help

No matter what you heard, use what works for you, as an alternative, if you really must change, I offer ternary:

myvar: "{{ my_group_var|ternary('value', 'other_value') }}"

I advocate for not using “if” statements inside jinja statements and jinjas in general. But I agree with previous comment:

The argument being…?

Can you please explain your comment?

I heard dont use if statements, its better to use ansible filters and when not create your own. But to create every time a new filter its a little bit oversized. In my opinion.

Jinja is a text engine, not all data is text, when something a bit more complex is being used - a lot of problems appear all of the suddent, e.g. jinja spacing issues when you want result to be dict or list. Coertion from text to other data types (implicit in old ansible-core versions, explicit in new versions) is also kind of a problem, I prefer to be sure that I work with “native” data sctructures.

Some more arguments are in this thread

(How to solve ansible challenge from ansible-core maintainer)

if statements are fine from my point of view, jinja is the problem, as this is a text engine, not data processing engine. Ansible filters are much more suitable to process data.

There are plenty of filters in ansible and they cover a lot of functionality, but sometimes you have to create your own. There is nothing inherently wrong about it.

For instance, some functionality might be missing in ansible:

https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/combine_default.py

Some functionality might be domain specific:

https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/valid_hostname.py

And sometimes you just create filters to understand how they work:

https://github.com/redhat-sap/rh_operations/blob/galaxy/plugins/filter/all.py

Filters and test plugins are easy, you just have to start.

BTW this is not jinja if, so this is “safe”
This is jinja if

 {%- if something -%}  value  {%- endif -%}

Its not jinja? What is it called?