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

后端 未结 8 491
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 10:43

I have very simple line in the template:

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

And I have list for ip:

ip:
 - 1.1.1.1
 - 2.2.2.2
 - 3.3.3.         


        
8条回答
  •  梦毁少年i
    2021-01-11 11:30

    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.

提交回复
热议问题