首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何循环通过一个可操作的剧本输出?

如何循环通过一个可操作的剧本输出?
EN

Stack Overflow用户
提问于 2018-09-23 21:28:51
回答 1查看 2.2K关注 0票数 2

我想得到所有ip地址的结果,我在我的剧本中点击。例如,我试图平三个不同的IP地址,我不想手动计算我必须设置多少output.results[]。这是我编辑的剧本:

代码语言:javascript
复制
- 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'"

这是输出:

代码语言:javascript
复制
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地址的结果?有办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-24 01:09:54

您可以在游戏手册中使用任务模块,如下所示

代码语言:javascript
复制
- 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和调试:

代码语言:javascript
复制
- 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'"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52470416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档