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

**URL:** <https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557>\
**Category:** Ansible Project\
**Created:** [June 26, 2014, 1:06pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557 "2014-06-26T13:06:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [June 26, 2014, 1:06pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/1 "2014-06-26T13:06:01Z")

</div>

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, )

---

<div class="post-metadata">

**Author:** ![Enrique\_Paredes](https://avatars.discourse-cdn.com/v4/letter/e/3bc359/32.png) [@Enrique\_Paredes](https://forum.ansible.com/u/Enrique_Paredes)\
**Post date:** [June 26, 2014, 1:11pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/2 "2014-06-26T13:11:16Z")

</div>

Try the join filter in Jinja:

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

---

<div class="post-metadata">

**Author:** ![jepper](https://avatars.discourse-cdn.com/v4/letter/j/49beb7/32.png) [@jepper](https://forum.ansible.com/u/jepper)\
**Post date:** [June 26, 2014, 3:43pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/3 "2014-06-26T15:43:58Z")

</div>

Will that not generate a list as follows

fusemq01, fusemq02

it does not take into account “tcp://”

---

<div class="post-metadata">

**Author:** ![Scott\_Sturdivant](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Scott\_Sturdivant](https://forum.ansible.com/u/Scott_Sturdivant)\
**Post date:** [June 26, 2014, 4:37pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/4 "2014-06-26T16:37:55Z")

</div>

You may want something like this (untested):

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

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 26, 2014, 4:38pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/5 "2014-06-26T16:38:41Z")

</div>

You can do this:

---

<div class="post-metadata">

**Author:** ![Timothy\_Appnel](https://avatars.discourse-cdn.com/v4/letter/t/e5b9ba/32.png) [@Timothy\_Appnel](https://forum.ansible.com/u/Timothy_Appnel)\
**Post date:** [June 26, 2014, 11:39pm UTC](https://forum.ansible.com/t/for-loop-in-template-how-do-i-avoid-trailing-comma-on-last-iteration/15557/6 "2014-06-26T23:39:45Z")

</div>

OK try this...

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

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

\<tim/\>
