gpt4 book ai didi

ansible - 如何在ansible中使用regex_replace转换这种数据格式

转载 作者:行者123 更新时间:2023-12-04 08:40:27 28 4
gpt4 key购买 nike

- hosts: localhost
connection: local
gather_facts: no
vars:
input: |-
404135 ARP requests sent
6762 ARP replies sent
6762 ARP requests received
0 ARP requests ignored
tasks:
- debug: var=input

- set_fact:
var2: "{{ '404135 ARP requests sent' | map('regex_replace','(\\d+)ARP requests sent','ARPRequestsSent: \\1') }}"
我希望使用正则表达式替换插件来操作和转换为 JSON 格式,我们可以操作数据以及如何更改字符串位置并获得所需的输出格式
{
"ARP_Requests_Sent": "404135",
"ARP_replies_sent": "6762",
"ARP_requests_received": "6762",
"ARP_requests_ignored": "0"
}
任何线索都有帮助

最佳答案

下面的任务完成这项工作

    - set_fact:
var2: "{{ dict(my_keys|zip(my_vals)) }}"
vars:
my_lines: "{{ input.splitlines() }}"
my_regex: '^(.*?) (.*)$'
replace_val: '\1'
replace_key: '\2'
my_keys: "{{ my_lines|
map('regex_replace', my_regex, replace_key)|
list }}"
my_vals: "{{ my_lines|
map('regex_replace', my_regex, replace_val)|
list }}"
- debug:
var: var2|to_nice_json

  var2|to_nice_json: |-
{
"ARP replies sent": "6762",
"ARP requests ignored": "0",
"ARP requests received": "6762",
"ARP requests sent": "404135"
}

关于ansible - 如何在ansible中使用regex_replace转换这种数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64595669/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com