A string interpolation filter for mapping over lists

Hey Guys,

I’m a big fan of using the extract filter to get hostvar values from a set of hosts. What I have found is that I often times need to inject those vars into strings such as URLs or the such. I created a filter to allow myself to map over the list of vars and inject them into a string that I call interpolate. Just sharing in case others find it also useful.

Here is an example use case.

`

telegraf_influxdb_urls: “{{ groups[‘tag_group_influxdb’]|map(‘extract’, hostvars, ‘ansible_fqdn’)|map(‘interpolate’, ‘http://{}:8086’)|list|join(‘,’) }}”

`

The filter is super similar to the existing jinja2 filter called format just with the arguments flipped so that the template is applied to every element being mapped over.

The source for this is just

`

def interpolate(elem, tmpl):
‘’’ Inject elements into a python string ‘’’
return tmpl.format(elem)

`

Anyways just thought I would share. If someone else knows a way to do this without having to add a new filter let me know, otherwise I think it could be a useful addition to the core ansible filters.
Tony