# Changing Last item in Host Group for Template

**URL:** <https://forum.ansible.com/t/changing-last-item-in-host-group-for-template/27767>\
**Category:** Ansible Project\
**Created:** [June 11, 2018, 1:23pm UTC](https://forum.ansible.com/t/changing-last-item-in-host-group-for-template/27767 "2018-06-11T13:23:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Janis\_Orlovs](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@Janis\_Orlovs](https://forum.ansible.com/u/Janis_Orlovs)\
**Post date:** [June 11, 2018, 1:23pm UTC](https://forum.ansible.com/t/changing-last-item-in-host-group-for-template/27767/1 "2018-06-11T13:23:40Z")

</div>

Hello,

I have got situation, where I needed to change last item in the host group for template.  
{% for host in groups[‘application\_srv’] %}

```auto
{% if groups[‘application_srv’].index(inventory_hostname) == -1 %}
          'app@{{ hostvars[host]['ansible_fqdn'] }}'
{% else %}
          'app@{{ hostvars[host]['ansible_fqdn'] }}',
{% endif %}
{% endfor %}

```

```auto

```

```auto

```

```auto

But this does not working. Maybe someone can, to use host_group item and then loop thru the index as last one

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 11, 2018, 2:07pm UTC](https://forum.ansible.com/t/changing-last-item-in-host-group-for-template/27767/2 "2018-06-11T14:07:15Z")

</div>

You should explain what you are trying to do so we don't have to guess.  
So what you are trying to do is to not have a comma after the last item?

If so Jinga has many loop variables[1] you can use inside a for loop.  
The one you are looking for is loop.last.

{% if not loop.last %},{% endif %}

[1] [http://jinja.pocoo.org/docs/2.10/templates/#for](http://jinja.pocoo.org/docs/2.10/templates/#for)
