On Fri, May 3, 2019, at 4:15 PM, Adam E wrote:
> Hi there, I have some logic that I want to apply when creating assigning a 
> list to a variable.
> 
> See my sample playbook below, Ideally i'd like a nice clean way to have a 
> logic based list. the "users" approach works, however it's always 
> interpreted as a string.  The only solution I can think of to fix this one 
> is to store it as a string and then split it later.  But I would like to 
> know if it's possible to return a list somehow.   Maybe there's a different 
> syntax that i'm not aware of.  Can anyone offer a better way to do 
> something like below?
> 
> - hosts: localhost
>   connection: local
>   vars:
>     # THIS does not work as it's interpreted as a string, would have to 
> split in a new var? 
>     # is there anything I can do here to keep a similar syntax but return a 
> list?
>     users: >-
>       {% set cusers = ['user1', 'user2'] %}
>       {% if ansible_distribution == "RedHat" %}
>       {{ cusers.append('redhat_user') }}
>       {% else %}
>       {{ cusers.append('other_user') }}
>       {% endif %}
>       {{ cusers | list }}
>     # THIS works but a little ugly with the newline
>     users2: "{% set cusers = ['user1', 'users2'] %}\
>              {% if ansible_distribution == 'RedHat' %}\
>              {{ cusers.append('redhat_user') }}\
>              {% else %}\
>              {{ cusers.append('other_user') }}\
>              {% endif %}\
>              {{ cusers }}"

Another way:

users2: "{{ ['user1', 'users2'] + (ansible_distribution == 'RedHat') | 
ternary(['redhat_user')], ['other_user']) }}"

V/r,
James Cassell

> 
>   tasks:
> 
>      - debug:
>          msg: "user: {{ item  }}"
>        loop: "{{ users2 }}"
> 
>  
> 

-- 
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/1557352379.1591804.1669157976.5234DC88%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to