我想得到所有ip地址的结果,我在我的剧本中点击。例如,我试图平三个不同的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.results1的事实,所以我只能看到1.1.1.1的结果,但是我希望看到所有三个ip地址的结果?有办法吗?
发布于 2018-09-24 01:09:54
您可以在游戏手册中使用任务模块,如下所示
- name: Testing Napalam Ping Module
hosts: arista
gather_facts: no
tasks:
- name: Napalm ping and debug
include_tasks: napalm-tasks.yml
with_items:
- 1.1.1.1
- 8.8.8.8
- 4.2.2.2
loop_control:
loop_var: destination_host并将所有其他相关任务放置在napalm-tasks.yml中,以循环处理这些任务,以对输出进行ping和调试:
- name: Napalm ping
napalm_ping:
provider: "{{ creds }}"
destination: "{{ destination_host }}"
register: output
- name: I am gonna set the output as a fact
set_fact:
ping_results: "{{ output }}"
- 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.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'"https://stackoverflow.com/questions/52470416
复制相似问题