Re: [ansible-project] Convert list to string in rendered Jinja template

2020-03-06 Thread Shifa Shaikh
Thank you @Stefan, 

That solves the issue !!

On Friday, March 6, 2020 at 2:32:07 PM UTC+5:30, Stefan Hornburg (Racke) 
wrote:
>
> 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 %} 
> >  
> >  
> >  
> > {% for item in layer.things %} 
> > {{ item }} 
> > {%endfor %} 
> >  
> >  
> > {%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 
> >  
> >  
> >  
> > cactus 
> > lotus 
> > jasmine 
> > rose 
> >  
> >  
> >  
> >  
> >   
> > mango 
> > apple 
> >  
> >  
> > | 
> > 
> > 
> > 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...@googlegroups.com   ansible-project+unsubscr...@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
>  
> > <
> https://groups.google.com/d/msgid/ansible-project/10765862-e99b-44bf-bdb8-0db36c1fc49b%40googlegroups.com?utm_medium=email_source=footer>.
>  
>
>
>
> -- 
> Ecommerce and Linux consulting + Perl and web application programming. 
> Debian and Sympa administration. Provisioning with Ansible. 
>
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/59bb4061-7544-4846-9855-fb291065a0e1%40googlegroups.com.


Re: [ansible-project] Convert list to string in rendered Jinja template

2020-03-06 Thread Stefan Hornburg (Racke)
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 %}
> 
> 
> 
> {% for item in layer.things %}
> {{ item }}
> {%endfor %}
> 
> 
> {%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
> 
> 
> 
> cactus
> lotus
> jasmine
> rose
> 
> 
> 
> 
>  
> mango
> apple
> 
> 
> |
> 
> 
> 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-project+unsubscr...@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
> .


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

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/51c78d34-8549-3a2e-2f79-cf7ffeaedc1d%40linuxia.de.


signature.asc
Description: OpenPGP digital signature


Re: [ansible-project] Convert list to string in rendered Jinja template

2020-03-06 Thread Mark Whaite
Hi

You can find all about Jinja filters here
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html

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.



On Fri, 6 Mar 2020 at 08:04, 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:
> {% for layer in layers %}
> 
> 
>  
> {% for item in layer.things %}
> {{ item }}
> {% endfor %}
> 
> 
> {% 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
> 
> 
>  
> cactus
> lotus
> jasmine
> rose
> 
> 
> 
> 
>  
> mango
> apple
> 
> 
>
>
> 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-project+unsubscr...@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

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAHtMyqgUtd%2BYD1%3DKuw5p89vUW_-EFdgRr%2BUPg%3D-pA7bF7N8xxw%40mail.gmail.com.


[ansible-project] Convert list to string in rendered Jinja template

2020-03-06 Thread Shifa Shaikh
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 %}
{{ item }}
{% endfor %}


{% 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


 
cactus
lotus
jasmine
rose




 
mango
apple




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-project+unsubscr...@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.