从 Ansible 中的 Json 嵌套对象输出中解析键和值
Parsed the key and values from Json nested object output in Ansible
我正在从设备中提取信息并保存为 json 和 yaml 格式,然后浏览数据然后解析并生成特定键和值的 csv 文件。
你能帮我得到这些具体的key/values(有些key/values可能是未知的,所以我们不能叫他们的名字)
这是我需要的 CVS 文件
OPER_ID, TYPE, TAG, DATA SIZE REQ, SRC ADDR, DEST ADDR, DEST PORT, FREQUENCY, PACKET COUNT, PACKET INTERVAL
200, udp jitter, ABC, 500, 1.1.1.1, 2.2.2.2, 17000, 60, 1000, 20
200, icmp path-jitter, DEF, null, 3.3.3.3, 4.4.4.4, null, 30, 20, null
这是在我的剧本运行
之后保存在json文件中的json文件数据
"{
"100": {
"type": {
"udp jitter": {
"data_size_req": 500,
"dest_addr": "2.2.2.2",
"dest_port": 17000,
"frequency": 60,
"packet": {
"count": 1000,
"interval": 20
},
"src_addr": "1.1.1.1",
"tag": "ABC"
}
}
},
"200": {
"type": {
"icmp path-jitter": {
"dest_addr": "4.4.4.4",
"frequency": 30,
"packet": {
"count": 50
},
"src_addr": "3.3.3.3",
"tag": "DEF"
}
}
}"
这是我的剧本
---
- name: COLLECT SHOW RUN IPSLA OPERATION
hosts: router1.example.com
gather_facts: no
connection: network_cli
roles:
- clay584.parse_genie
tasks:
- name: show running ipsla config
iosxr_command:
commands: show run ipsla operation
register: ipsla_output
- name: Set fact with Genie parser
set_fact:
ipsla_config: "{{ ipsla_output['stdout'][0] | parse_genie(command='show run ipsla operation', os='iosxr') }}"
- name: Copy the output into a json file
copy:
content: |
"{{ ipsla_config.ipsla.operations | to_nice_json }}"
dest: "{{ inventory_hostname }}_ipsla.json"
# Here something is wrong
- name: Create a CSV file from the output
lineinfile:
path: "{{ inventory_hostname }}_ipsla.csv"
line: "{{ item.key }}, {{ item.value.type.key }} " ### this is where i am wrong
create: yes
loop: "{{ ipsla_config.ipsla.operations | dict2items }}"
- name: Add a header row on the above CSV file
lineinfile:
path: "{{ inventory_hostname }}_ipsla.csv"
insertbefore: BOF
line: OPER_ID,TYPE
这是我尝试拉取“类型”值键时的错误
TASK [Create a CSV file from the output] ********************************************************************************
fatal: [fra01-wxbb-crt01.webex.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'key'\n\nThe error appears to be in '/Users/murafi/webexansible/ipsla_plays.yml': line 45, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create a CSV file from the output\n ^ here\n"}
如果您需要更多信息,请告诉我,提前感谢您的帮助:)
使用模板模块创建最终的 csv:
在 templates
文件夹
中创建模板文件 csv.j2
OPER_ID, TYPE, TAG, DATA SIZE REQ, SRC ADDR, DEST ADDR, DEST PORT, FREQUENCY, PACKET COUNT, PACKET INTERVAL
{% for k0 in json %}
{% set oper_id = k0 %}
{% set type = json[k0]['type']|first %}
{% set data_size_req = json[k0]['type'][type]['data_size_req']|d("null") %}
{% set tag = json[k0]['type'][type]['tag']|d("null") %}
{% set src_addr = json[k0]['type'][type]['src_addr']|d("null") %}
{% set dest_addr = json[k0]['type'][type]['dest_addr']|d("null") %}
{% set dest_port = json[k0]['type'][type]['dest_port']|d("null") %}
{% set frequency = json[k0]['type'][type]['frequency']|d("null") %}
{% set packet_count = json[k0]['type'][type]['packet']['count']|d("null") %}
{% set packet_interval = json[k0]['type'][type]['packet']['interval']|d("null") %}
{{ oper_id }}, {{ type }}, {{ tag }}, {{ data_size_req }}, {{ src_addr }}, {{ dest_addr }}, {{ dest_port }}, {{ frequency }}, {{ packet_count }}, {{ packet_interval }}
{% endfor %}
创建最终 csv 文件的剧本:
- name: testplaybook jinja2
hosts: router1.example.com
tasks:
- name: Create a CSV file from the output
template:
src: csv.j2
dest: "{{ inventory_hostname }}_ipsla.csv"
vars:
fil: "{{ inventory_hostname }}_ipsla.json"
json: "{{ lookup('file', fil) | from_json }}"
如果你想 select 一些标签使用 if:
{% if tag == "ABC" %}
{{ oper_id }}, {{ type }}, {{ tag }}, {{ data_size_req }}, {{ src_addr }}, {{ dest_addr }}, {{ dest_port }}, {{ frequency }}, {{ packet_count }}, {{ packet_interval }}
{% endif %}
我正在从设备中提取信息并保存为 json 和 yaml 格式,然后浏览数据然后解析并生成特定键和值的 csv 文件。
你能帮我得到这些具体的key/values(有些key/values可能是未知的,所以我们不能叫他们的名字)
这是我需要的 CVS 文件
OPER_ID, TYPE, TAG, DATA SIZE REQ, SRC ADDR, DEST ADDR, DEST PORT, FREQUENCY, PACKET COUNT, PACKET INTERVAL
200, udp jitter, ABC, 500, 1.1.1.1, 2.2.2.2, 17000, 60, 1000, 20
200, icmp path-jitter, DEF, null, 3.3.3.3, 4.4.4.4, null, 30, 20, null
这是在我的剧本运行
之后保存在json文件中的json文件数据"{
"100": {
"type": {
"udp jitter": {
"data_size_req": 500,
"dest_addr": "2.2.2.2",
"dest_port": 17000,
"frequency": 60,
"packet": {
"count": 1000,
"interval": 20
},
"src_addr": "1.1.1.1",
"tag": "ABC"
}
}
},
"200": {
"type": {
"icmp path-jitter": {
"dest_addr": "4.4.4.4",
"frequency": 30,
"packet": {
"count": 50
},
"src_addr": "3.3.3.3",
"tag": "DEF"
}
}
}"
这是我的剧本
---
- name: COLLECT SHOW RUN IPSLA OPERATION
hosts: router1.example.com
gather_facts: no
connection: network_cli
roles:
- clay584.parse_genie
tasks:
- name: show running ipsla config
iosxr_command:
commands: show run ipsla operation
register: ipsla_output
- name: Set fact with Genie parser
set_fact:
ipsla_config: "{{ ipsla_output['stdout'][0] | parse_genie(command='show run ipsla operation', os='iosxr') }}"
- name: Copy the output into a json file
copy:
content: |
"{{ ipsla_config.ipsla.operations | to_nice_json }}"
dest: "{{ inventory_hostname }}_ipsla.json"
# Here something is wrong
- name: Create a CSV file from the output
lineinfile:
path: "{{ inventory_hostname }}_ipsla.csv"
line: "{{ item.key }}, {{ item.value.type.key }} " ### this is where i am wrong
create: yes
loop: "{{ ipsla_config.ipsla.operations | dict2items }}"
- name: Add a header row on the above CSV file
lineinfile:
path: "{{ inventory_hostname }}_ipsla.csv"
insertbefore: BOF
line: OPER_ID,TYPE
这是我尝试拉取“类型”值键时的错误
TASK [Create a CSV file from the output] ********************************************************************************
fatal: [fra01-wxbb-crt01.webex.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'key'\n\nThe error appears to be in '/Users/murafi/webexansible/ipsla_plays.yml': line 45, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Create a CSV file from the output\n ^ here\n"}
如果您需要更多信息,请告诉我,提前感谢您的帮助:)
使用模板模块创建最终的 csv:
在 templates
文件夹
csv.j2
OPER_ID, TYPE, TAG, DATA SIZE REQ, SRC ADDR, DEST ADDR, DEST PORT, FREQUENCY, PACKET COUNT, PACKET INTERVAL
{% for k0 in json %}
{% set oper_id = k0 %}
{% set type = json[k0]['type']|first %}
{% set data_size_req = json[k0]['type'][type]['data_size_req']|d("null") %}
{% set tag = json[k0]['type'][type]['tag']|d("null") %}
{% set src_addr = json[k0]['type'][type]['src_addr']|d("null") %}
{% set dest_addr = json[k0]['type'][type]['dest_addr']|d("null") %}
{% set dest_port = json[k0]['type'][type]['dest_port']|d("null") %}
{% set frequency = json[k0]['type'][type]['frequency']|d("null") %}
{% set packet_count = json[k0]['type'][type]['packet']['count']|d("null") %}
{% set packet_interval = json[k0]['type'][type]['packet']['interval']|d("null") %}
{{ oper_id }}, {{ type }}, {{ tag }}, {{ data_size_req }}, {{ src_addr }}, {{ dest_addr }}, {{ dest_port }}, {{ frequency }}, {{ packet_count }}, {{ packet_interval }}
{% endfor %}
创建最终 csv 文件的剧本:
- name: testplaybook jinja2
hosts: router1.example.com
tasks:
- name: Create a CSV file from the output
template:
src: csv.j2
dest: "{{ inventory_hostname }}_ipsla.csv"
vars:
fil: "{{ inventory_hostname }}_ipsla.json"
json: "{{ lookup('file', fil) | from_json }}"
如果你想 select 一些标签使用 if:
{% if tag == "ABC" %}
{{ oper_id }}, {{ type }}, {{ tag }}, {{ data_size_req }}, {{ src_addr }}, {{ dest_addr }}, {{ dest_port }}, {{ frequency }}, {{ packet_count }}, {{ packet_interval }}
{% endif %}