Preventing comma from being added at last loop occurance.

408 views
Skip to first unread message

StartledPhoenix

unread,
Feb 24, 2015, 12:41:30 PM2/24/15
to salt-...@googlegroups.com
If I have a jinja loop similar to
{% foreach cat, catdesc in pillar['cats'].iteritems() %}
{% if catdesc['catcolor'] is defined %}
{{ cat }} is {{ catdesc['catcolor'] }},
{% else %}
{{ cat }} has no known color
{% endif %}

how can I make it so that the last entry has no comma?

Regards,
PHoenix

Valentin Bud

unread,
Feb 24, 2015, 1:22:45 PM2/24/15
to salt-...@googlegroups.com
Hello Phoenix,

Maybe the following snippet may help you:

{% set vhosts_list = [] -%}
{% for key in vhosts | sort -%}
{% do vhosts_list.append(key) -%}
{% endfor -%}

vhosts={{vhosts_list|join(',')}}

{% for key in vhosts | sort -%}
[{{key}}]
{% for k in vhosts[key] -%}
{{k}}={{vhosts[key][k]}}
{% endfor -%}

Best,
Valentin




--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elias Probst

unread,
Feb 24, 2015, 4:12:40 PM2/24/15
to salt-...@googlegroups.com
Jinja has builtin support for doing this kind of stuff.
See: http://jinja.pocoo.org/docs/dev/templates/#for
→ loop.first
→ loop.last

- Elias
> send an email to salt-users+unsubscribe@__googlegroups.com
> <mailto:salt-users%2Bunsu...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/__optout
> <https://groups.google.com/d/optout>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to salt-users+...@googlegroups.com
> <mailto:salt-users+...@googlegroups.com>.
signature.asc

Arnold Bechtoldt

unread,
Feb 26, 2015, 2:31:20 PM2/26/15
to salt-...@googlegroups.com
You already got some good replies. In case you're looking for a
practical example you'll find one at
<https://github.com/bechtoldt/saltstack-varnish-formula/blob/master/varnish/files/profiles/common/backends.vcl#L16>.

Have fun.


Arnold

--
Arnold Bechtoldt

Karlsruhe, Germany
0xE2356889.asc
signature.asc

StartledPhoenix

unread,
Feb 28, 2015, 1:05:44 PM2/28/15
to salt-...@googlegroups.com
Thanks for the help :)

Sad that jinja doesn't have a way of doing this natively, but your code
works well.

Regards,
Phoenix
>> send an email to salt-users+...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout [1].
>
> --
> You received this message because you are subscribed to the Google
> Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout [1].
>
>
> Links:
> ------
> [1] https://groups.google.com/d/optout
Reply all
Reply to author
Forward
0 new messages