【问题标题】:How to loop through an ansible playbook output? [duplicate]如何遍历 ansible playbook 输出? [复制]
【发布时间】: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 地址的结果?有办法吗?

【问题讨论】:

    标签: loops ansible


    【解决方案1】:

    您可以在 playbook 中使用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'"
    

    【讨论】:

    • 抱歉,我不明白这将如何帮助获得所有三个 IP 地址的相同结果?请详细说明一下好吗?
    • 我更新并包含了更多细节。
    猜你喜欢
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多