Thanks - that’s a good option. Do you know if there is a way to get the zero-padding? That’s less important, but I thought it should be doable with Jinja. It’s a shame the format function can’t be used for this.
inspired by this answer. It lacks elegance because it requires you to keep the number of format strings and the length of the range the same, and it goes via strings, so you have to be careful that your separator doesn’t appear in the string, but it does the job in a single line without having to make a custom filter. Is there a way to improve on this?
What you need to be able to use is the map filter, but unfortunately the built in jinja2 format filter wasn’t really written with usability from the map filter in mind, and thus the arguments it takes are reversed from what you need.
Instead of making some overly complicated jinja template in your playbook, I’d recommend making a new custom filter for your needs. This could be as simple as making your own version of format that works the way you need such as:
# saved as filter_plugins/ianhinder.py
def ianhinder_format(value, fmt):
return fmt % value
class FilterModule:
def filters(self):
return {
'ianhinder_format': ianhinder_format,
}
Note: Needed to use %03d instead of %3d since the latter does space padding and not zero padding. And to include 100 in the range, you have to set the stop to 101 as it’s not inclusive of the stop.
Thanks. That certainly is the most ergonomic in the playbook, though it has the annoyance of having to implement it yourself. Yes, most of the examples I found for similar tasks when searching said you need to write your own filter in Python. It seems that Jinja2 is not quite powerful enough to express all that is generally needed, and it needs to be augmented with Python code even for quite simple use cases.