# Can ternary() be used with vars or static strings only?

**URL:** <https://forum.ansible.com/t/can-ternary-be-used-with-vars-or-static-strings-only/20243>\
**Category:** Ansible Project\
**Created:** [January 17, 2016, 10:21am UTC](https://forum.ansible.com/t/can-ternary-be-used-with-vars-or-static-strings-only/20243 "2016-01-17T10:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [January 17, 2016, 10:21am UTC](https://forum.ansible.com/t/can-ternary-be-used-with-vars-or-static-strings-only/20243/1 "2016-01-17T10:21:48Z")

</div>

Hi,

to avoid copying many tasks I was trying to use the ternary() filter as  
seen in  
[https://groups.google.com/forum/#!msg/ansible-project/4bhkNQvrbhc/Vs3u8QQVCAAJ](https://groups.google.com/forum/#!msg/ansible-project/4bhkNQvrbhc/Vs3u8QQVCAAJ)

goal:  
If I'm on debian based systems I'd like the owner to be  
\_tor-{{ item[0] }}\_{{ item.1.orport }}

on other systems it should just be  
{{ tor\_user }}

this is one example of a currently working tasks which I would like to  
condense into one by using the ternary() filter:

- name: Ensure directory exists and is owned by tor\_user  
&nbsp;&nbsp;file: path={{ tor\_DataDir }}/{{ item[0] }}\_{{ item.1.orport }}  
&nbsp;&nbsp;&nbsp;&nbsp;state=directory  
&nbsp;&nbsp;&nbsp;&nbsp;owner={{ tor\_user }}  
&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;- "{{ tor\_ips }}"  
&nbsp;&nbsp;&nbsp;- "{{ tor\_ports }}"  
&nbsp;&nbsp;when: ansible\_pkg\_mgr != 'apt'

- name: Ensure directory exists and is owned by tor\_user  
&nbsp;&nbsp;file: path={{ tor\_DataDir }}/{{ item[0] }}\_{{ item.1.orport }}  
&nbsp;&nbsp;&nbsp;&nbsp;state=directory  
&nbsp;&nbsp;&nbsp;&nbsp;owner=\_tor-{{ item[0] }}\_{{ item.1.orport }}  
&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;- "{{ tor\_ips }}"  
&nbsp;&nbsp;&nbsp;- "{{ tor\_ports }}"  
&nbsp;&nbsp;when: ansible\_pkg\_mgr == 'apt'

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 17, 2016, 2:56pm UTC](https://forum.ansible.com/t/can-ternary-be-used-with-vars-or-static-strings-only/20243/2 "2016-01-17T14:56:10Z")

</div>

You cannot nest {{ }}. You can think of anything inside of {{ }} as a code block, so you just use bare variable names.

I think this should work:

owner=“{{ (ansible\_pkg\_mgr != ‘apt’)| ternary(tor\_user,  
‘_tor-’ ~ item[0] ~ '_’ ~ item.1.orport) }}”

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [January 17, 2016, 6:16pm UTC](https://forum.ansible.com/t/can-ternary-be-used-with-vars-or-static-strings-only/20243/3 "2016-01-17T18:16:08Z")

</div>

Matt Martz:

> (ansible\_pkg\_mgr != 'apt')| ternary(tor\_user,  
> '\_tor-' ~ item[0] ~ '\_' ~ item.1.orport)

thanks, works!
