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

后端 未结 8 444
伪装坚强ぢ
伪装坚强ぢ 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条回答
  • 2021-01-11 11:32

    try:

    - hosts: localhost
      tags: s20
      gather_facts: no
      vars:
        ip:
          - 1.1.1.1
          - 2.2.2.2
          - 3.3.3.3
        joined_ip: "'{{ \"', '\".join(ip)}}'"
      tasks:
      - debug: msg="(ip={{joined_ip}})"
    

    PS: ansible supports a bit of python code execution within {{}}, which is what i'm misusing here.

    0 讨论(0)
  • 2021-01-11 11:42

    Following worked for me

    ('{{ iplist | join('\',\'') }}')
    

    Ex:

    Inventory
    
    [ips]
    1.1.1.1
    2.2.2.2
    3.3.3.3
    
    #cat temp.sh.j2 
    
     "ips": (ip='{{ groups['zoo'] | join('\',\'') }}') 
    
    result:
    
    #cat temp.sh
    
     "ips": (ip='1.1.1.1','2.2.2.2','3.3.3.3')
    

    Hope it would help someone.

    0 讨论(0)
提交回复
热议问题