Add quotes to elemens of the list in jinja2 (ansible)

This will work :

ip={{ '\"' + ip|join('\", \"') + '\"' }}

A custom filter plugin will also work. In ansible.cfg uncomment filter_plugins and give it a path, where we put this

def wrap(list):
    return [ '"' + x + '"' for x in list]

class FilterModule(object):
    def filters(self):
        return {
            'wrap': wrap
        }

in a file called core.py. Like this. Then you can simply use

ip|wrap|join(', ')

And it should produce comma seperated list with each ip wrapped in quotes.


Actually there is a very simple method to achieve this:

{{ mylist | map('quote') | join(', ') }}

The filter map iterates over every item and let quote process it. Afterwards you can easily join them together.

Tags:

Jinja2

Ansible