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.
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.
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: