# advanced Jinja config

**URL:** <https://forum.ansible.com/t/advanced-jinja-config/18322>\
**Category:** Ansible Project\
**Created:** [May 27, 2015, 6:33pm UTC](https://forum.ansible.com/t/advanced-jinja-config/18322 "2015-05-27T18:33:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 27, 2015, 6:33pm UTC](https://forum.ansible.com/t/advanced-jinja-config/18322/1 "2015-05-27T18:33:31Z")

</div>

Hi,

I need help with the bellow configuration in Jinja, I’m using a for loop to populate the hosts from a group defined in my inventory and that’s working good but what my application requires is a letter in front of every host in alphabetical order depending how many host numbers I have :

# servers.ini :

[server-slaves]  
[server1.example.com](http://server1.example.com)  
[server2.example.com](http://server2.example.com)

[server3.example.com](http://server3.example.com)

# config.conf.j2 :

slaves\_hosts={% for host in groups[‘server-slaves’] %}

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

# output :

slaves\_hosts=[server1.example.com](http://server1.example.com),[server2.example.com](http://server2.example.com),[server3.example.com](http://server3.example.com)

# desired output

slaves\_hosts= **a** :server1.example.com, **b** :server2.example.com, **c** :server3.example.com

This is possible in Chef, any idea if I can do the same in Ansible and how ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 27, 2015, 7:04pm UTC](https://forum.ansible.com/t/advanced-jinja-config/18322/2 "2015-05-27T19:04:11Z")

</div>

There might be a different way, but here is one that I can think of:

{% set letters = cycler(\*‘abcdefghijklmnopqrstuvwxyz’[:]) %}  
slave\_hosts={% for host in slaves %}{{ letters.next() }}:{{ host }}{% if not loop.last %},{% endif %}{% endfor %}

This creates a ‘cycler’ with the lowercase letters of the english alphabet, and cycles through them on each iteration of the for loop.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 27, 2015, 7:38pm UTC](https://forum.ansible.com/t/advanced-jinja-config/18322/3 "2015-05-27T19:38:42Z")

</div>

You save my day Matt, thank you so much .
