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.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/1a6b745e-c97b-47f0-a284-92052f4ac3bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.