Skip to content

Instantly share code, notes, and snippets.

@jdelaporte
Created February 23, 2022 16:10
Show Gist options
  • Save jdelaporte/6c44fa93ace8115cc7b1434d9234e582 to your computer and use it in GitHub Desktop.
Save jdelaporte/6c44fa93ace8115cc7b1434d9234e582 to your computer and use it in GitHub Desktop.
ICMP Ping sweep with Ansible
---
##################
# changed_when is used here to highlight the ips
# that return greater than X percent packet_loss
# where the X is passed from the GUI
##################
- set_fact:
ping_fail_threshold: '{{ ui_ping_fail_threshold | default(33,True) }}'
ping_count: '{{ ui_ping_packet_count | default(3, True) }}' # ping packets/ attempts per ping ip
ping_start: '{{ ui_ping_start | default(0, True) }}'
ping_x: '{{ ui_ping_x | default("") }}' # Empty for end of list
canary_ping: '{{ canary_ping_list|default([]) }}'
# At least one IP to always check that should always fail. In host_vars/<host>.yml, with the vrf_ping_ips and ping_ips lists
- set_fact:
vrf_ping_packet_loss: []
basic_ping_packet_loss: []
vrf_ping_ips_list: "{{ canary_ping + vrf_ping_ips[(ping_start|int):((ping_x|int + ping_start|int )|default(None, True))] }}" # All or the number specified in ui_ping_x
ping_ips_list: "{{ canary_ping + ping_ips[(ping_start|int):((ping_x|int + ping_start|int)|default(None, True))] }}"
- name: Ping Sweep across VRF
ansible.netcommon.net_ping:
count: '{{ ping_count | default(5) }}'
dest: '{{ item }}'
vrf: '{{ vrf_name }}'
register: vrf_ping_sweep
loop: '{{ vrf_ping_ips_list }}'
changed_when: vrf_ping_sweep.packet_loss is undefined or
(vrf_ping_sweep.packet_loss | replace('%','')| int(default=200) >= ping_fail_threshold|int )
failed_when: False # Don't want to fail right now.
- name: Ping Sweep Local (no VRF)
ansible.netcommon.net_ping:
count: '{{ ping_count | default(5) }}'
dest: '{{ item }}'
register: basic_ping_sweep
loop: '{{ ping_ips_list }}'
changed_when: basic_ping_sweep.packet_loss is undefined or
(basic_ping_sweep.packet_loss | replace('%','')| int(default=200) >= ping_fail_threshold|int )
failed_when: False # Don't want to fail right now.
#- name: Ping Sweep - Long results
# debug:
# msg: "This number is greater than {{ ping_fail_threshold }}: {{ item.packet_loss |default(item.msg|default('N/A', True),True) }}"
# when: item.packet_loss is undefined or
# item.packet_loss | replace('%','')| int(default=200) >= ping_fail_threshold|int
# loop: '{{ basic_ping_sweep.results + vrf_ping_sweep.results }}'
- name: Create the vrf fail list
set_fact:
vrf_ping_packet_loss: "{{ vrf_ping_packet_loss + [item.item +': '+ item.packet_loss|default(item.msg|default('N/A', True),True)] }}"
loop: '{{ vrf_ping_sweep.results }}'
when: item.packet_loss is undefined or
item.packet_loss | replace('%','')| int(default=200) >= ping_fail_threshold|int
#TODO: Use map (?) to sort the worst failures to the top
# This would add valuable visibility to the list
- name: Create the basic fail list
set_fact:
basic_ping_packet_loss: "{{ basic_ping_packet_loss + [item.item +': '+ item.packet_loss|default(item.msg|default('N/A', True),True)] }}"
loop: '{{ basic_ping_sweep.results }}'
when: item.packet_loss is undefined or
item.packet_loss | replace('%','')| int(default=200) >= ping_fail_threshold|int
- name: Print the vrf fail list
debug:
var: vrf_ping_packet_loss
- name: Print the basic fail list
debug:
var: basic_ping_packet_loss
@jdelaporte
Copy link
Author

jdelaporte commented Feb 23, 2022

- hosts: all
  gather_facts: no
  collections:
    - community.network
    - ansible.netcommon
    - paloaltonetworks.panos
    - f5networks.f5_modules
    - cisco.ios
  roles:
    - arubanetworks.aos_wlan_role # Needed for aos MM
  tasks:
    - name: Ping tests for hosts that have them defined
      include_tasks: ../restore/restore_includes/ping_sweep.yml
      when:
      - vrf_ping_ips is defined
      - ping_ips is defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment