title: Ansible: Remove last comma in nested loop?
body: <p>I have the following in a jinja2 template. Essentially I'm looping through a dictionary, picking out the name and if it doesn't start with "sh", I'm adding it to this config. However, I would like to have the server comma separated.  </p>
  
  <pre class="lang-yaml prettyprint-override"><code>servers = {% for item in list_search_peers.json['entry'] %}{% if not item.name.startswith('sh-') %}{{ item.name }}{% endif %}{% endfor %}
  </code></pre>
  
  <p>Normally i would do something like:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>servers = {% for item in list_search_peers.json['entry'] %}{% if not item.name.startswith('sh-') %}{{ item.name }}{% if not loop.last %},{% endif %}{% endif %}{% endfor %}`
  </code></pre>
  
  <p>However <code>loop.last</code> won't work because I am filtering out one of the values. </p>
  
  <p>My output is:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>servers = x.x.x.x:8089,y.y.y.y:8089,
  </code></pre>
  
  <p>But i want it without the last <code>,</code>:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>servers = x.x.x.x:8089,y.y.y.y:8089
  </code></pre>
  
  <p>I was trying <code>{% set }</code> to set a variable but had no luck there.</p>