Use Ansible to Test Clos Ping Connectivity

  • 4
  • Article
  • Updated 2 years ago
  • (Edited)
I wanted to share something I have been using for a while to test ping connectivity using Ansible. I use this super short and super simple playbook to do an eye check on my switch connectivity after making a change. Ideally you would integrate something like Behave or ServerSpec into your Continuous Integration and Development process, so that any changes are automatically tested and given a pass or fail. That is not always something you can create on the fly when starting a new deployment or project or just simply testing out a new feature.

How is this working?
Ansible has a variable called 'hostvars' that allows for accessing information about other hosts. Here I use hostvars[item] to pull out variables for each host during the play and specifically grab the ipv4 addresses local to that host. Those addresses are appended to a list and the ping command is run on each host for all ipv4 address collected for each and every host.

- hosts: network
remote_user: root

- name: set an empty list named ips
ips: [] - name: append all known IPv4 addresses from the group network set_fact: ips: "{{ ips + hostvars[item].ansible_all_ipv4_addresses }}" with_inventory_hostnames: network - name: ping all IP addresses in ips shell: "ping {{ item }} -c 1" with_items: - "{{ ips }}" register: result changed_when: "result.rc != 0" 
Photo of David Marshall

David Marshall, Employee

  • 498 Points 250 badge 2x thumb

Posted 2 years ago

  • 4

Be the first to post a reply!