advanced Jinja config

65 views
Skip to first unread message

Nicolas G

unread,
May 27, 2015, 2:33:31 PM5/27/15
to ansible...@googlegroups.com
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]

# config.conf.j2 :
slaves_hosts={% for host in groups['server-slaves'] %}
{{ host }}{% if not loop.last %},{% endif %}
{% endfor %}

# output :

# desired output 

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

Thanks in advance.

Matt Martz

unread,
May 27, 2015, 3:04:55 PM5/27/15
to ansible...@googlegroups.com
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.

--
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/25aef324-4ccc-49f1-aac5-d7b96a3db87f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Nicolas G

unread,
May 27, 2015, 3:38:42 PM5/27/15
to ansible...@googlegroups.com
You save my day Matt, thank you so much .
Reply all
Reply to author
Forward
0 new messages