regex - 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的内容请看我之前的问题
解决方案
展平嵌套列表。例如
- debug:
msg: "{{ time_left2|flatten|join(' ') }}"
推荐阅读
- corda - Corda:我们可以开发由 IIS 网络服务器运行的 Dapp 来与 Corda 平台对话吗?
- python-3.x - 向数据框添加零列
- openlayers - 几何函数内的 OpenLayers 特征样式 zIndex
- amazon-web-services - 如何从外部软件触发 Alexa?
- java - 我如何将其转换为 for 循环?
- typescript - 为字段中的属性赋值,但不在打字稿中的构造函数中赋值
- java - 在.xsl 中生成二维码?
- xcode - React Native 项目在分发到 testflight 后崩溃
- java - 我正在使用 Kotlin 修改 Java,但我正在努力修复错误的部分(编辑)
- java - 使用 itext 将不同的文件格式合并为单个 pdf 会导致 pdf 损坏