# Convert list to string in rendered Jinja template

**URL:** <https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403>\
**Category:** Ansible Project\
**Created:** [March 6, 2020, 8:04am UTC](https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403 "2020-03-06T08:04:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 8:04am UTC](https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403/1 "2020-03-06T08:04:18Z")

</div>

I have a variable files like below:

`  
layers:

- name: APP  
things:

- cactus

- lotus

- jasmine

- rose  
bgcolor:

- sky blue

- name: WAS  
things:

- mango

- apple  
bgcolor:

- yellow  
`

Below is my jinja template file:

`  
{% for layer in layers %}

{% for item in layer.things %} {% endfor %}

| | {{ item }} |
| --- | --- |

{% endfor %} `

I run this with the below playbook:

`

---

<div class="post-metadata">

**Author:** ![Mark\_Whaite](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@Mark\_Whaite](https://forum.ansible.com/u/Mark_Whaite)\
**Post date:** [March 6, 2020, 9:00am UTC](https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403/2 "2020-03-06T09:00:12Z")

</div>

Hi

You can find all about Jinja filters here [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html)

But you can convert a list to a string using

```auto
{{ list | join(" ") }}

```

This will add a space between each element. I am not sure if that is what you need.

```auto

```

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 9:01am UTC](https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403/3 "2020-03-06T09:01:32Z")

</div>

> I have a variable files like below:
> 
> \>  
> layers:  
> -name:APP  
> things:  
> -cactus  
> -lotus  
> -jasmine  
> -rose  
> bgcolor:  
> -sky blue
> 
> ```
> \-name:WAS
> things:
> \-mango
> \-apple
> bgcolor:
> \-yellow
> 
> ```
> 
> \>
> 
> Below is my jinja template file:  
> \>  
> {%forlayer inlayers %}  
> \<table bgcolor="{{ layer.bgcolor | list}}"\>  
> \<tr\>  
> \<th\>\</th\>  
> {% for item in layer.things %}  
> \<th align="center"\>{{ item }}\</th\>  
> {%endfor %}  
> \</tr\>  
> \</table\>  
> {%endfor %}  
> \>

Try the join filter {{ layer.bgcolor | join(' ') }}. It is fruitless to apply the list  
filter to a list :-).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 9:08am UTC](https://forum.ansible.com/t/convert-list-to-string-in-rendered-jinja-template/32403/4 "2020-03-06T09:08:12Z")

</div>

Thank you @Stefan,

That solves the issue !!
