【发布时间】:2018-09-23 21:28:51
【问题描述】:
我想获得我在剧本中 ping 的所有 IP 地址的结果。例如,我正在尝试 ping 三个不同的 IP 地址,我不想手动计算必须设置多少 output.results[]。这是我编辑的剧本:
- name: Testing Napalam Ping Module
hosts: arista
gather_facts: no
vars:
output_results: []
tasks:
- name: Napalm ping
napalm_ping:
provider: "{{creds}}"
destination: "{{item}}"
with_items:
- 1.1.1.1
- 8.8.8.8
- 4.2.2.2
register: output
- name: I am gonna set the output as a fact
set_fact:
ping_results: "{{output.results[1].item}}"
- name: I am gonna print out the ping_results fact
debug:
var: ping_results
- name: I am gonna set the output as a fact
set_fact:
packet_loss: "{{output.results[1].results.success}}"
- name: I am gonna print out the packet_loss fact
debug:
var: packet_loss
- debug:
msg: "{{ping_results}} is pingable from {{ansible_host}} with {{item.key}} = {{item.value}} out of 5 packets"
with_dict: "{{packet_loss}}"
when: "item.key == 'packet_loss'"
这是输出:
TASK [debug] *****************************************************************************************************************************
ok: [pynet-sw5] => (item={'value': 0, 'key': u'packet_loss'}) => {
"msg": "1.1.1.1 is pingable from arista5.twb-tech.com with packet_loss = 0 out of 5 packets"
}
ok: [pynet-sw6] => (item={'value': 0, 'key': u'packet_loss'}) => {
"msg": "1.1.1.1 is pingable from arista6.twb-tech.com with packet_loss = 0 out of 5 packets"
}
ok: [pynet-sw7] => (item={'value': 4, 'key': u'packet_loss'}) => {
"msg": "1.1.1.1 is pingable from arista7.twb-tech.com with packet_loss = 4 out of 5 packets"
}
ok: [pynet-sw8] => (item={'value': 1, 'key': u'packet_loss'}) => {
"msg": "1.1.1.1 is pingable from arista8.twb-tech.com with packet_loss = 1 out of 5 packets"
因为我手动设置了 output.results[1] 的事实,所以我只能看到 1.1.1.1 结果,但我想查看所有三个 IP 地址的结果?有办法吗?
【问题讨论】: