我正试图解析返回如下一行的命令的输出(还有更多的输出,但这是我想要的行):
剩余时间:3分12秒
当没有时间时,它返回如下一行:
剩余时间:0秒
我想提取出分钟和秒的数量,这样我就可以将它提供给GNU date -d。首先我尝试了这个:
- name: determine how much time we have left
set_fact:
time_left: "{{ cmd_output.stdout | regex_search(time_left_regex, '\\1', '\\2') | join(' ') }}"
vars:
time_left_regex: 'Remaining Time: ([0-9]+ Minutes) and ([0-9]+ Seconds)'但在没有时间的情况下,这并不能处理这件事。于是我尝试了这样的方法:
- name: determine how much time we have left
set_fact:
time_left: "{{ cmd_output.stdout | regex_findall(time_left_regex, '\\1') }}"
vars:
time_left_regex: 'Next Execution:.*([0-9]{1,2} (Minutes|Seconds))'但这只会返回如下内容:
ok: localhost => { "msg":“时间离开:[U‘2秒’,u‘秒’]”}
我想我是在正确的轨道上,但我需要一个更好的regex,所以也许有人可以帮助我在这里?提前谢谢你。
发布于 2020-03-21 07:36:20
发布于 2020-03-21 10:27:26
拆分字符串(行)和合并字典是可能的。例如
- set_fact:
time_left: "{{ time_left|default({})|
combine({myline[item]: myline[item+1]}) }}"
loop: "{{ range(0, myline|length + 1, 3)|list }}"
vars:
myline: "{{ cmd_output.stdout.split(':').1.split()|reverse|list }}"
- debug:
var: time_left用于各种命令输出
cmd_output.stdout: 'Remaining Time: 3 Minutes and 12 Seconds'
cmd_output.stdout: 'Remaining Time: 0 Seconds'
cmd_output.stdout: 'Remaining Time: 2 Days and 7 Hours and 3 Minutes and 12 Seconds'给予(分别)
"time_left": {
"Minutes": "3",
"Seconds": "12"
} "time_left": {
"Seconds": "0"
} "time_left": {
"Days": "2",
"Hours": "7",
"Minutes": "3",
"Seconds": "12"
}https://stackoverflow.com/questions/60785464
复制相似问题