I'm trying to generate an Ansible template that increments on letters alphabetically rather than numbers. Is there a function similar to range(x)
that could help me?
pseudo code example
{% for letter in range(a, d) %}
{{ letter }}
{% endfor %}
expected output
a
b
c
d
Alternatively is there a way to convert a number into it's alphabetical equivalent in Ansible?
{% for i in range(6) %}
{{ convert(i) }}
{% endfor %}
UPDATE
For those who are curious, here's how I ended up applying @zigam's solution. The goal was to create xml tags with every host from a hostgroup.
In my role defaults:
ids: "ABCDEFGHIGJKLMNPQRSTUVWXYZ"
In my template:
{% for host in groups['some_group'] %}
<host-id="{{ ids[loop.index] }}" hostName="{{ host }}" port="8888" />
{% endfor %}
you can use a custom filter plugin to do what you want
in filter_plugins/scratch_filter.py:
in scratch-template.j2:
in scratch_playbook.yml
You can iterate over a string:
If you want to iterate over a range of the alphabet: