--- - name: build ping_map hosts: localhost become: no gather_facts: false tasks: - name: build ping_map ansible.builtin.set_fact: _ping_map: "{{ _ping_map | default({}) | combine ({entry: []}, recursive=True) }}" loop: "{{ inventory_hosts }}" loop_control: loop_var: entry vars: inventory_hosts: "{{ hostvars[inventory_hostname]['groups']['all'] }}" # - ansible.builtin.debug: # msg: # - "{{ _ping_map }}" - name: populate ping_map ansible.builtin.set_fact: _ping_map: "{{ _ping_map | default({}) | combine ({source: destination_list_append}, recursive=True) }}" loop: "{{ target_hosts|product(target_hosts) }}" loop_control: loop_var: entry vars: target_hosts: "{{ hostvars[inventory_hostname]['groups']['all'] }}" source: "{{ entry[0] }}" destination: "{{ entry[1] }}" destination_list: "{{ _ping_map[source] }}" destination_list_append: "{{ destination_list + [destination] }}" when: not entry[0] == entry[1] # - ansible.builtin.debug: # msg: # - "{{ _ping_map }}" - name: write global ping_map ansible.builtin.set_fact: _global_ping_map: "{{ _ping_map }}" delegate_to: localhost delegate_facts: true - name: ping test hosts: all become: yes gather_facts: True tasks: - name: load global ping_map ansible.builtin.set_fact: _ping_map: "{{ hostvars['localhost']['_global_ping_map'] }}" when: - hostvars['localhost']['_global_ping_map'] is defined # - debug: # msg: # - "{{ _ping_map }}" - name: ping neighbours ansible.builtin.shell: | echo SOURCE {{ inventory_hostname }} echo DESTINATION {{ destination_target }} echo ping -Rn -c 1 {{ destination_ip }} loop: "{{ destination_targets }}" loop_control: loop_var: entry vars: destination_targets: "{{ _ping_map[inventory_hostname] }}" destination_target: "{{ entry }}" destination_ip: "{{ hostvars[destination_target]['ansible_default_ipv4']['address'] }}" source: "{{ inventory_hostname }}" register: _ping_results - name: print results ansible.builtin.debug: msg: - "{{ output }}" loop: "{{ _ping_results['results'] }}" loop_control: loop_var: idx label: "{{ destination }}" vars: destination: "{{ idx['entry'] }}" output: "{{ idx['stdout_lines'] }}"