I've been recently wondering about the ternary filter in Ansible. I've seen it used in a couple of places and it's also mentioned in the documentation. As you probably know, it's not a standard Jinja filter but one created in Ansible code.
I would like to understand why was the ternary
filter
created and why is it preferred (Ansible docs doesn't even mention the
alternative syntax) over the more pythonic way of using python style
conditional expressions supported by the Jinja library.
# python style conditional expressions
- debug: msg="{{ variable if variable is defined else omit }}"
- debug: msg="{{ 'yes' if variable == 'inline' else 'no' }}"
# Using ternary filter
- debug: msg="{{ variable | ternary(variable, omit) }}"
- debug: msg="{{ (variable == 'inline') | ternary('yes', 'no') }}"
To me, the first example seems more readable and uses a syntax familiar to python developers.
The `ternary` filter is easier for people that are not familiar with
Python, but non programmers and those used to ternary structures in
other languages.
--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/mi8zuVlRR3g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7cA6WoFgc7jOBcvwzr6nhpyA_uhBO3GoVfw%3Df0ys9%2BvGw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.