首页 > 解决方案 > ansible 加入列表

问题描述

我在加入 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秒”]}

好的:[localhost] => {“msg”:“11 分 48 秒”}

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

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

对我来说,似乎regex_search返回了我可以加入一个简单字符串的列表,并且regex_findall创建了一个嵌套列表。这个对吗?如果是这样,如何将输出转换为类似于regex_search工作方式的字符串?

PS:更多关于cmd_output的内容请看我之前的问题

标签: regexlistjoinansible

解决方案


展平嵌套列表。例如

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

推荐阅读