我想获取我在剧本中ping的所有IP地址的结果。例如,我尝试对三个不同的IP地址执行ping操作,而我不想手动计算必须设置的output.results []个数。这是我编辑过的剧本:
def generate_array():
while True:
X = np.empty((1, 9008))
y = np.empty((1), dtype=int)
# Some processing
X[0] = row
y[0] = label
yield(X,y)
这是输出:
- 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'"
因为我手动设置了output.results [1]的事实,所以只能看到1.1.1.1的结果,但是我想查看所有三个IP地址的结果?有办法吗?
答案 0 :(得分:0)
您可以按以下方式在剧本中使用include_tasks模块
- 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'"