我有以下脚本,它执行ping操作。
- name: Running WAN connectivty checks on WAN-01A
cisco.ios.ios_ping:
vrf: '{{item.vrf}}'
dest: '{{item.destination}}'
source: '{{item.source}}'
count: '{{item.count}}'
size: '{{item.size}}'
loop: "{{ wan01avar }}"
when: inventory_hostname == 'WAN-01A'
register: wan01a
tags:
- never
- WAN
- name: WAN connectivty results on WAN-01A
debug: msg="{{ wan01a.results | json_query(jmesquery)}}"
vars:
jmesquery: "[*].{Source: item.source, Destination: item.destination,packetloss: packet_loss}"
when: inventory_hostname == 'WAN-01A'
tags:
- never
- WAN该脚本使用以下变量:
wan01avar:
- vrf: 'default'
source: 'Bundle-Ether400.682'
destination: '10.27.251.194'
count: '10'
size: '1500'
- vrf: 'CLOUD_DCI'
source: 'Bundle-Ether400.682'
destination: '10.27.251.194'
count: '9'
size: '1400'我正在使用json_query (jmesquery)并打印以下输出:
TASK [wan : Output from WAN-01A] *******************************************
ok: [WAN-01A] => {
"msg": [
{
"Destination": "10.27.251.194",
"Source": "Bundle-Ether400.682",
"packetloss": "0%"
},
{
"Destination": "10.27.251.194",
"Source": "Bundle-Ether400.682",
"packetloss": "0%"
}
]
}我想要实现的是以下输出:
TASK [wan : Output from WAN-01A] *******************************************
ok: [WAN-01A] => {
"msg": [
{
"Destination": "10.27.251.194", "Source": "Bundle-Ether400.682", "packetloss": "0%"
"Destination": "10.27.251.194","Source": "Bundle-Ether400.682","packetloss": "0%"
}
]
}如何实现上述输出?每一节都有一行
发布于 2020-12-05 17:11:10
我已经创建了这个简单的攻略,它可能会帮助你获得你需要的东西:
---
- name: Format message
hosts: localhost
vars:
wan01a:
- vrf: 'default'
source: 'Bundle-Ether400.682'
destination: '10.27.251.194'
count: '10'
size: '1500'
- vrf: 'CLOUD_DCI'
source: 'Bundle-Ether400.682'
destination: '10.27.251.194'
count: '9'
size: '1400'
tasks:
- debug:
msg: "{{ wan01a }}"
- debug:
msg: "Destination: {{ item['destination'] }}, Source: {{ item['source'] }}, count: {{ item['count'] }}"
with_items: "{{ wan01a }}"
- debug:
msg: >
{%- for item in wan01a %}
Destination: {{ item['destination'] }}, Source: {{ item['source'] }}, count: {{ item['count'] }}
{%- endfor %}我用三种不同的方式打印了这条消息。最后一个最接近你想要实现的目标。
https://stackoverflow.com/questions/65153263
复制相似问题