for loop in template how do I avoid trailing comma on last iteration?

385 views
Skip to first unread message

jepper

unread,
Jun 26, 2014, 9:06:01 AM6/26/14
to ansible...@googlegroups.com
In a template, I loop over a group of fusemq hosts defined in my invetory file e.g.

fuseMQ_URL=failover:( {% for host in groups['fusemq'] %} tcp://{{ host }}, {% endfor %} )

The result is of course, a trailing comma - how can I avoid this?

fuseMQ_URL=failover:(  tcp://fusemq01, tcp://fusemq02,  )

Iknite

unread,
Jun 26, 2014, 9:20:21 AM6/26/14
to ansible...@googlegroups.com
Try the join filter in Jinja:

{{ groups['fusemq']|join(' ,') }}



--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8718c489-3551-41e8-a1e7-4a73cfa461d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jepper

unread,
Jun 26, 2014, 11:43:58 AM6/26/14
to ansible...@googlegroups.com
Will that not generate a list as follows

fusemq01, fusemq02 

it does not take into account "tcp://" 

Scott Sturdivant

unread,
Jun 26, 2014, 12:38:38 PM6/26/14
to ansible...@googlegroups.com
You may want something like this (untested):

{% for host in groups['fusemq'] %} tcp://{{ host }}{% if not loop.last %}, {% endif %}{% endfor %}


Petros Moisiadis

unread,
Jun 26, 2014, 12:38:54 PM6/26/14
to ansible...@googlegroups.com
--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8718c489-3551-41e8-a1e7-4a73cfa461d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

You can do this:

fuseMQ_URL=failover:{% for host in groups['fusemq'] %} tcp://{{ host }}{% if not loop.last %},{% endif %}{% endfor %}

Timothy Appnel

unread,
Jun 26, 2014, 7:39:45 PM6/26/14
to ansible...@googlegroups.com
OK try this...

"tcp://{{ groups['fusemq']|join(',tcp://') }}"

Not exactly pretty but better than a for loop with conditionals.

<tim/>

Reply all
Reply to author
Forward
0 new messages