We have a lot of config that wants to fill in templates based on a particular server's "number" within a certain group.
For example, if we have an inventory file with:
[fooservers]
steve
larry
frank
eleanor
...we then want to configure steve's templates with "foo01", larry with "foo02", frank with "foo03", etc.
In most cases I've been using the following pattern inside of templates:
{% for pt in groups.ptservers %}
{% if inventory_hostname == pt %}
special_identifier = foo{{ '%02d' % loop.index }}
{% endif %}
{% endfor %}
This has worked well for limited use, but is there an easier way to store the server's associated loop_index for reuse at either the playbook or template level?
We have reached a point where we have a template that needs to use this value 27 times, and I don't want to have to clutter everything up with 27 loops...
I tried using {% set %} in the template inside of the loop. but set has local scope, so it's unusable afterwards.
Has anyone else come up with a good solution for this pattern or do I just need to make a really really ugly template?