Ternary filter vs conditional expressions

2,164 views
Skip to first unread message

rba...@redhat.com

unread,
Apr 11, 2018, 5:04:03 AM4/11/18
to Ansible Project

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.

Brian Coca

unread,
Apr 11, 2018, 10:10:34 AM4/11/18
to Ansible Project
Even though Ansible is written in Python, Python devs are not the main
target of the tool, so being 'pythonic' is not really an aim of it.

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.

--
----------
Brian Coca

rba...@redhat.com

unread,
Apr 12, 2018, 11:56:11 AM4/12/18
to Ansible Project


On Wednesday, April 11, 2018 at 3:10:34 PM UTC+1, Brian Coca wrote:

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.


I'm used to the different ternary structures from different programming languages and I find it hard to believe that the ternary filter is easier for non programmers. Is there any evidence of it being the case?

The python style syntax reads more like English and you can immediately see the control flow.

On the other hand, the first time I've seen the ternary filter I had to look up how it works. Is the first argument for the false or for the true condition.
You don't have this problem with the other syntax.


Brian Coca

unread,
Apr 12, 2018, 4:26:06 PM4/12/18
to Ansible Project
Other people found it confusing and wanted something they could easily
lookup up, like a filter.

Once I added it, I stopped hearing complaints, so not sure how much
that is worth other than anecdotal evidence.

Use whichever way suits you best.





--
----------
Brian Coca

Rastislav Barlik

unread,
Apr 13, 2018, 5:19:08 AM4/13/18
to ansible...@googlegroups.com
Thanks Brian,

If people find the filter useful then that's fair enough. Everybody has a difference preference. I probably suffer from Python's "There should be one -- and preferably only one -- obvious way to do it."



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

Reply all
Reply to author
Forward
0 new messages