【问题标题】:Loop over variable and concatenate with string循环变量并与字符串连接
【发布时间】:2018-01-02 00:48:19
【问题描述】:

假设我在 ansible 中有一个数组:

vars:
  loopme: ['somesing', 'someothersing']
  concatenateme: 'constant'

如何使用变量 concatenateme 遍历列表并从列表中连接值?

所以我得到somesingconstantsomeothersingconstant 并将结果放入任务的字段中?也许是神社?

【问题讨论】:

    标签: variables ansible yaml jinja2


    【解决方案1】:

    您可以使用mapregex_replace 过滤器应用于列表的每个元素,并用您的常量替换“字符串结尾”($):


    - hosts: localhost
      gather_facts: no
      vars:
        loopme: ['somesing', 'someothersing']
        concatenateme: 'constant'
      tasks:
        - debug:
            msg: "{{ loopme | map('regex_replace','$',concatenateme) | list }}"
    

    【讨论】:

    • 这会是一个数组吗?
    • 是的,这将是一个列表:[ "somesingconstant", "someothersingconstant" ]
    • 我在尝试这个时遇到一个错误:unhashable type: 'list' 这是我要分配的值:backup_directories: "{{ backup_directories | map('regex_replace',' ^',backup_snapshot_prefix) | list }}" 有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2016-12-15
    • 2017-12-18
    • 2019-03-09
    • 2021-06-20
    • 2014-07-20
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多