Convert list to string in rendered Jinja template

6,969 views
Skip to first unread message

Shifa Shaikh

unread,
Mar 6, 2020, 3:04:18 AM3/6/20
to Ansible Project
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 %}
<table bgcolor="{{ layer.bgcolor | list}}">
<tr>
<th> </th>
{% for item in layer.things %}
<th align="center">{{ item }}</
th>
{% endfor %}
</tr>
</
table>
{% endfor %}

I run this with the below playbook:

---
 
- name: Demonstrating variables in Jinja2 Loops
    hosts
: localhost
    connection
: local
    vars_files
:
     
- vars.yml
    gather_facts
: no
    tasks
:
     
- name: Create the Jinja2 based templateone

       
template: src=./varloop_new.j2 dest=./output.txt



The output recieved has a problem. 

Output.txt:

-bash-4.2$ cat output.txt
<table bgcolor="[u'sky blue']">
<tr>
<th> </th>
<th align="center">cactus</
th>
<th align="center">lotus</th>
<th align="center">jasmine</
th>
<th align="center">rose</th>
</
tr>
</table>
<table bgcolor="[u'yellow']">
<tr>
<th> </
th>
<th align="center">mango</th>
<th align="center">apple</
th>
</tr>
</
table>


It displays bgcolor="[u'sky blue']" instead of bgcolor="sky blue"

How can I convert the list to a string in the jinja template.

Mark Whaite

unread,
Mar 6, 2020, 4:01:13 AM3/6/20
to ansible...@googlegroups.com
Hi


But you can convert a list to a string using 
{{ list | join(" ") }}
This will add a space between each element. I am not sure if that is what you need. 


--
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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/10765862-e99b-44bf-bdb8-0db36c1fc49b%40googlegroups.com.


--
Mark Whaite, Systems Administrator
Adelard LLP
Adelard LLP, Units 24 Waterside, 44-48 Wharf Road, London N1 7UX
Tel: 020 7832 5850
Web: http://www.adelard.com

This e-mail, and any attachments, is confidential and for the use of the addressee only. If you are not the intended recipient, please telephone 020 7832 5850. We do not accept legal responsibility for this e-mail or any viruses.

Registered office: Stourside Place, Station Road, Ashford, Kent TN12 1PP
Registered in England & Wales no. OC 304551. VAT no. 454 489808

Stefan Hornburg (Racke)

unread,
Mar 6, 2020, 4:02:07 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 9:04 AM, Shifa Shaikh wrote:
> 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
Racke

> I run this with the below playbook:
>
> |
> ---
>   -name:Demonstratingvariables inJinja2Loops
>     hosts:localhost
>     connection:local
>     vars_files:
>       -vars.yml
>     gather_facts:no
>     tasks:
>       -name:Createthe Jinja2based templateone
>
>         template:src=./varloop_new.j2 dest=./output.txt
> |
>
>
>
> The output recieved has a problem. 
>
> Output.txt:
>
> |
> -bash-4.2$ cat output.txt
> <table bgcolor="[u'sky blue']">
> <tr>
> <th></th>
> <th align="center">cactus</th>
> <th align="center">lotus</th>
> <th align="center">jasmine</th>
> <th align="center">rose</th>
> </tr>
> </table>
> <table bgcolor="[u'yellow']">
> <tr>
> <th> </th>
> <th align="center">mango</th>
> <th align="center">apple</th>
> </tr>
> </table>
> |
>
>
> It displays bgcolor="[u'sky blue']" instead of bgcolor="sky blue"
>
> How can I convert the list to a string in the jinja template.
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> <https://groups.google.com/d/msgid/ansible-project/10765862-e99b-44bf-bdb8-0db36c1fc49b%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Shifa Shaikh

unread,
Mar 6, 2020, 4:08:12 AM3/6/20
to Ansible Project
Thank you @Stefan, 

That solves the issue !!
Reply all
Reply to author
Forward
0 new messages