# Ternary filter vs conditional expressions

**URL:** <https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241>\
**Category:** Ansible Project\
**Created:** [April 11, 2018, 9:04am UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241 "2018-04-11T09:04:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbarlik](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rbarlik](https://forum.ansible.com/u/rbarlik)\
**Post date:** [April 11, 2018, 9:04am UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241/1 "2018-04-11T09:04:03Z")

</div>

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.

```auto

# python style conditional expressions
- debug: msg="{{ variable if variable is defined else omit }}"
- debug: msg="{{ 'yes' if variable == 'inline' else 'no' }}"
 

```

```auto
# 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 11, 2018, 2:10pm UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241/2 "2018-04-11T14:10:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbarlik](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rbarlik](https://forum.ansible.com/u/rbarlik)\
**Post date:** [April 12, 2018, 3:56pm UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241/3 "2018-04-12T15:56:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 12, 2018, 8:25pm UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241/4 "2018-04-12T20:25:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbarlik](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@rbarlik](https://forum.ansible.com/u/rbarlik)\
**Post date:** [April 13, 2018, 9:18am UTC](https://forum.ansible.com/t/ternary-filter-vs-conditional-expressions/27241/5 "2018-04-13T09:18:47Z")

</div>

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