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

后端 未结 8 445
伪装坚强ぢ
伪装坚强ぢ 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:19

    NOTE This is similar to Kashyap's answer, but i needed a slightly different version: Using it to double quote each items in a bash array), eg. result should be:

    SOME_LIST=( "Johnny" "Joey" "Dee Dee" "Tommy" )

    projects/ansible/expand_list.yml

    ---
    - hosts: localhost
      connection: local
    
      vars:
        some_list:
          - Johnny
          - Joey
          - Dee Dee
          - Tommy
    
      tasks:
        - name: "Expand the ramones band members list."
          template:
            src: "templates/expand_list.conf.j2"
            dest: "/var/tmp/ramones.conf"
    

    projects/ansible/templates/expand_list.conf.j2

    SOME_LIST=( "{{ '" "'.join(some_list) }}" )
    

提交回复
热议问题