【问题标题】:ansible join listansible 加入列表
【发布时间】:2020-03-22 08:22:34
【问题描述】:

我在 ansible 中加入列表时遇到问题。请看下面的剧本摘录:

    - name: determine how much time we have left
      set_fact:
        time_left1: "{{ cmd_output.stdout | regex_search(time_left_regex1, '\\1', '\\2') }}"
        time_left2: "{{ cmd_output.stdout | regex_findall(time_left_regex2, '\\1') }}"
      vars:
        time_left_regex1: 'Remaining Time: ([0-9]+ Minutes) and ([0-9]+ Seconds)'
        time_left_regex2: 'Remaining Time: (?:([0-9]+ Minutes) and )?([0-9]+ Seconds)'

    - debug:
        msg: "{{ time_left1 }}"

    - debug:
        msg: "{{ time_left1 | join(' ') }}"

    - debug:
        msg: "{{ time_left2 }}"

    - debug:
        msg: "{{ time_left2 | join(' ') }}"

当我运行这个剧本时,我得到:

好的:[本地主机] => { “味精”:[ “11分钟”, 《48 秒》 ] }

好的:[本地主机] => { "msg": "11 分 48 秒" }

好的:[本地主机] => { “味精”:[ [ “11分钟”, 《48 秒》 ] ] }

好的:[本地主机] => { “味精”:[ “11分钟”, 《48 秒》 ] }

在我看来,regex_search 返回的 I 列表我可以加入一个简单的字符串,而 regex_findall 创建了一个嵌套列表。它是否正确?如果是这样,如何将输出转换为类似于 regex_search 工作方式的字符串?

PS:更多cmd_output的内容请看我的previous question

【问题讨论】:

    标签: regex list join ansible


    【解决方案1】:

    flatten 嵌套列表。例如

        - debug:
            msg: "{{ time_left2|flatten|join(' ') }}"
    

    【讨论】:

    • 非常感谢弗拉基米尔,我完全忽略了这个过滤选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2021-11-05
    • 2019-04-21
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多