【问题标题】:Add quotes to elemens of the list in jinja2 (ansible)为 jinja2 中的列表元素添加引号(ansible)
【发布时间】:2015-04-09 11:48:02
【问题描述】:

我在模板中有很简单的一行:

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

我有 ip 列表:

ip:
 - 1.1.1.1
 - 2.2.2.2
 - 3.3.3.3

但应用程序需要带引号的 IP (ip='1.1.1.1', '2.2.2.2')。

我可以这样做:

ip:
 - "'1.1.1.1'"
 - "'2.2.2.2'"
 - "'3.3.3.3'"

但它非常难看。有什么好方法可以在 ansible 列表的每个元素上添加引号?

谢谢!

【问题讨论】:

    标签: jinja2 ansible


    【解决方案1】:

    这将起作用:

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

    自定义过滤器插件也可以使用。在 ansible.cfg 中取消注释 filter_plugins 并给它一个路径,我们把它放在哪里

    def wrap(list):
        return [ '"' + x + '"' for x in list]
    
    class FilterModule(object):
        def filters(self):
            return {
                'wrap': wrap
            }
    

    在一个名为 core.py 的文件中。 Like this。然后你可以简单地使用

    ip|wrap|join(', ')
    

    它应该生成逗号分隔的列表,每个 ip 都用引号括起来。

    【讨论】:

    • 这就像一个魅力。如果您只想为一个项目/剧本提供此功能,只需在您的项目中放置一个 filter_plugins 文件夹并调用该文件,如前所述,core.py
    • 不应该是ip={{ '\"' + ip|join('\", \"') + '\"'}}吗?
    【解决方案2】:

    其实有一个很简单的方法可以做到:

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

    过滤器map 遍历每个项目并让quote 处理它。之后你可以很容易地join他们在一起。

    【讨论】:

    • quote 过滤器似乎是 ansible 的一部分,而不是 jinja2 的核心。
    • 由于某种原因 map('quote') 在 ansible 中对我不起作用,它什么也没做
    • Ansible 的 map('quote') 仅用于 shell 的引号
    • map('quote') shell 变量的引号(不适用于 j2 模板),它决定是否需要引用(基于字符串的内容)。要点:改用 for 循环。
    • 作为记录,引用适用于command 模块。
    【解决方案3】:

    尝试:

    - 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 支持在{{}} 中执行一些 python 代码,这是我在这里误用的。

    【讨论】:

    • 这个对我有用,另一个对我有用。非常感谢分享这个。
    【解决方案4】:

    以下对我有用

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

    例如:

    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')
    

    希望对某人有所帮助。

    【讨论】:

    • 适用于非空列表。但对于空数组,它将导致 ['']
    【解决方案5】:

    注意 这类似于 Kashyap 的答案,但我需要一个稍微不同的版本:使用它来双引号 bash 数组中的每个项目),例如。结果应该是:

    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) }}" )
    

    【讨论】:

      【解决方案6】:

      我开发了一个自定义的wrap 过滤器

      def wrap(value, wrapper = '"'):
        return wrapper + value + wrapper
      
      class FilterModule(object):
        def filters(self):
          return {
            'wrap': wrap
          }
      

      如您所见,包装器是可自定义的,默认为 "

      你可以这样使用它

      ip={{ ip | map('wrap') | join(', ') }}
      

      免责声明:我是 python 和 ansible 新手

      【讨论】:

        【解决方案7】:

        您可以使用regex_replace, f.e.在 j2 模板文件中:

        (ip={{ip | map('regex_replace', '(.*)', "'\\1'") | join(',')}})
        

        如果您在剧本中内联执行此操作,请不要忘记转义双引号。这是一个完整的例子:

        - hosts: localhost
          gather_facts: no
          vars:
            ip:
            - 1.1.1.1
            - 2.2.2.2
            - 3.3.3.3
            ip_result: "{{ip | map('regex_replace', '(.*)', \"'\\1'\") | join(',')}}"
          tasks:
          - debug: msg="(ip={{ip_result}})"
          - copy: content="(ip={{ip_result}})" dest=./ip_result.txt
        

        ip_result.txt 的内容:

        $ cat ip_result.txt
        (ip='1.1.1.1','2.2.2.2','3.3.3.3')
        

        【讨论】:

          【解决方案8】:

          我发现使用现有 Ansible 过滤器执行此操作的最简单方法是使用 regex_replace

          {{ ip | map("regex_replace","(.+)","\'\\1\'") | join(',')}}
          

          【讨论】:

            猜你喜欢
            • 2011-10-08
            • 2013-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多