# multiline jinja2 lists returned as string?

**URL:** <https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265>\
**Category:** Ansible Project\
**Created:** [May 3, 2019, 8:15pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265 "2019-05-03T20:15:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [May 3, 2019, 8:15pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/1 "2019-05-03T20:15:52Z")

</div>

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

tasks:

- debug:  
msg: “user: {{ item }}”  
loop: “{{ users2 }}”  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 8, 2019, 8:11pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/2 "2019-05-08T20:11:33Z")

</div>

Something like this should work

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;cuser: ['user1', 'user2']  
&nbsp;&nbsp;&nbsp;&nbsp;users: "{{ cuser + ['redhat\_user'] if ansible\_distribution == 'RedHat' else cuser + ['other\_user'] }}"

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [May 8, 2019, 8:43pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/3 "2019-05-08T20:43:05Z")

</div>

thanks for the reply, I had also thought of that solution and it’s perfectly viable for my example (thanks). however was looking for something that doesn’t use multiple variables and avoids repetition. My example was just a simple one, in some cases I have more complex jinja statements and the crux of what I was after was how to nicely do complex multiline jinja statements that use lists.

---

<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:** [May 8, 2019, 8:58pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/4 "2019-05-08T20:58:00Z")

</div>

The root of the problem is a bit complicated, and rooted in the fact that historically jinja2 could only return strings. So we have code that attempts to safely convert string representations of python datastructures into actual python data structures.

What appears to be your problem is a bunch of added whitespace caused by the jinja2 templating. The following seems to resolve the problem:

users: \>-  
{%- set cusers = [‘user1’, ‘user2’] -%}  
{%- if ansible\_distribution == “RedHat” -%}  
{{ cusers.append(‘redhat\_user’) }}  
{%- else -%}  
{{ cusers.append(‘other\_user’) }}  
{%- endif -%}  
{{ cusers | list }}

Notice the use of {%- and -%} which informs jinja2 to strip whitespace. See [http://jinja.pocoo.org/docs/dev/templates/](http://jinja.pocoo.org/docs/dev/templates/)

---

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [May 8, 2019, 9:04pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/5 "2019-05-08T21:04:38Z")

</div>

dang, could have sworn I tried that. It works, thanks Matt, that’s exactly what I was looking for!! appreciate it.

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [May 8, 2019, 9:52pm UTC](https://forum.ansible.com/t/multiline-jinja2-lists-returned-as-string/30265/6 "2019-05-08T21:52:59Z")

</div>

> 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  
> &nbsp;&nbsp;connection: local  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;# THIS does not work as it's interpreted as a string, would have to  
> split in a new var?  
> &nbsp;&nbsp;&nbsp;&nbsp;# is there anything I can do here to keep a similar syntax but return a  
> list?  
> &nbsp;&nbsp;&nbsp;&nbsp;users: \>-  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% set cusers = ['user1', 'user2'] %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if ansible\_distribution == "RedHat" %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers.append('redhat\_user') }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers.append('other\_user') }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers | list }}  
> &nbsp;&nbsp;&nbsp;&nbsp;# THIS works but a little ugly with the newline  
> &nbsp;&nbsp;&nbsp;&nbsp;users2: "{% set cusers = ['user1', 'users2'] %}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if ansible\_distribution == 'RedHat' %}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers.append('redhat\_user') }}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers.append('other\_user') }}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}\  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ cusers }}"

Another way:

users2: "{{ ['user1', 'users2'] + (ansible\_distribution == 'RedHat') | ternary(['redhat\_user')], ['other\_user']) }}"

V/r,  
James Cassell
