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

711 views
Skip to first unread message

nusenu

unread,
Jan 17, 2016, 5:22:07 AM1/17/16
to Ansible Project
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

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
file: path={{ tor_DataDir }}/{{ item[0] }}_{{ item.1.orport }}
state=directory
owner={{ tor_user }}
with_nested:
- "{{ tor_ips }}"
- "{{ tor_ports }}"
when: ansible_pkg_mgr != 'apt'


- name: Ensure directory exists and is owned by tor_user
file: path={{ tor_DataDir }}/{{ item[0] }}_{{ item.1.orport }}
state=directory
owner=_tor-{{ item[0] }}_{{ item.1.orport }}
with_nested:
- "{{ tor_ips }}"
- "{{ tor_ports }}"
when: ansible_pkg_mgr == 'apt'

==========================================

attempt to use ternary:

- name: Ensure Tor "keys" directory exists and is owned by tor_user
file: path={{ tor_DataDir }}/{{ item[0] }}_{{ item.1.orport }}/keys
state=directory
owner="{{ (ansible_pkg_mgr != 'apt')| ternary('{{ tor_user }}',
'_tor-{{ item[0] }}_{{ item.1.orport }}') }}"
with_nested:
- "{{ tor_ips }}"
- "{{ tor_ports }}"


Fails with:

msg: chown failed: failed to look up user _tor-{# item[0] #}_{#
item.1.orport #}

so it tries to use it as a literal string.

Is what I'm trying to achieve possible or should I simply stick with the
duplicate tasks?

thanks!


signature.asc

Matt Martz

unread,
Jan 17, 2016, 9:56:17 AM1/17/16
to ansible...@googlegroups.com
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) }}"
--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/569B6B3C.4030503%40openmailbox.org.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

nusenu

unread,
Jan 17, 2016, 1:16:20 PM1/17/16
to ansible...@googlegroups.com


Matt Martz:
> (ansible_pkg_mgr != 'apt')| ternary(tor_user,
> '_tor-' ~ item[0] ~ '_' ~ item.1.orport)


thanks, works!
Reply all
Reply to author
Forward
0 new messages