我使用下面的代码来获取 YML 形式的设备信息,需要过滤设备的值并将输出打印到 CSV 文件。
- name: get system log
connection: local
check_mode: no
uri:
validate_certs: no
url: "https://{{ provider.ip_address }}/api/?type=op&cmd=<show><monitoring><health><device><all></all></device></health></monitoring></show>&key={{ auth.api_key }}"
return_content: yes
method: GET
register: output
- name: translate returned content from XML output into YML
set_fact:
YML: "{{ output['content'] | ansible.utils.from_xml }}"
- name: Output-TO-FILE
debug:
msg: "{{ YML.response.result }}"
我分享了我使用过的代码和我需要过滤的输出。
此输出有来自 2 个设备的信息,但实际输出可能有多个设备信息
"msg": {
"response": {
"@status": "success",
"result": {
"entry": [
{
"cps": "26",
"deviating_fields": {
"dp-cpu": null,
"logging-rate": null
},
"device-id": "1",
"dp-cpu": "4",
"fan-total": "0",
"fan-up": "0",
"hostname": "FW1",
"logging-rate": "74",
"model": "PA-VM",
"mp-cpu": "12",
"mp-mem": "38",
"port-total": "7",
"port-up": "2",
"power-total": "0",
"power-up": "0",
"sessions": "3070",
"throughput": "42190"
},
{
"cps": "44",
"deviating_fields": {
"logging-rate": null
},
"device-id": "2",
"dp-cpu": "2",
"fan-total": "0",
"fan-up": "0",
"hostname": "FW2",
"logging-rate": "64",
"model": "PA-VM",
"mp-cpu": "12",
"mp-mem": "38",
"port-total": "7",
"port-up": "2",
"power-total": "0",
"power-up": "0",
"sessions": "2836",
"throughput": "7195"
}
我正在寻找过滤
hostname
、device-id
、dp-cpu
等值并将其打印到 CSV 文件的步骤。
...
"device-id": "1",
"dp-cpu": "4",
...
"device-id": "2",
"dp-cpu": "2",
鉴于mre简化数据
YML:
response:
result:
response:
'@status': success
result:
entry:
- cps: '26'
deviating_fields:
dp-cpu: null
logging-rate: null
device-id: '1'
dp-cpu: '4'
fan-total: '0'
fan-up: '0'
hostname: FW1
- cps: '44'
deviating_fields:
logging-rate: null
device-id: '2'
dp-cpu: '2'
fan-total: '0'
fan-up: '0'
hostname: FW2
使用 Jinja 并创建内容
- debug:
msg: |
{{ fnames | join(',') }}
{% for i in YML.response.result.response.result.entry %}
{{ fnames | map('extract', i) | join(',') }}
{% endfor %}
给予
msg: |-
hostname,device-id,dp-cpu
FW1,1,4
FW2,2,2
如果这是您想要的,请将其写入文件
- copy:
dest: /tmp/monitor-dev-health.csv
content: |
{{ fnames | join(',') }}
{% for i in YML.response.result.response.result.entry %}
{{ fnames | map('extract', i) | join(',') }}
{% endfor %}
给予
shell> cat /tmp/monitor-dev-health.csv
hostname,device-id,dp-cpu
FW1,1,4
FW2,2,2
用于测试的完整剧本示例
- hosts: localhost
vars:
YML:
response:
result:
response:
'@status': success
result:
entry:
- cps: '26'
deviating_fields:
dp-cpu: null
logging-rate: null
device-id: '1'
dp-cpu: '4'
fan-total: '0'
fan-up: '0'
hostname: FW1
- cps: '44'
deviating_fields:
logging-rate: null
device-id: '2'
dp-cpu: '2'
fan-total: '0'
fan-up: '0'
hostname: FW2
fnames: [hostname, device-id, dp-cpu]
tasks:
- debug:
var: YML
- debug:
msg: |
{{ fnames | join(',') }}
{% for i in YML.response.result.response.result.entry %}
{{ fnames | map('extract', i) | join(',') }}
{% endfor %}
- copy:
dest: /tmp/monitor-dev-health.csv
content: |
{{ fnames | join(',') }}
{% for i in YML.response.result.response.result.entry %}
{{ fnames | map('extract', i) | join(',') }}
{% endfor %}