Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Network Automation Cookbook

You're reading from   Network Automation Cookbook Proven and actionable recipes to automate and manage network devices using Ansible

Arrow left icon
Product type Paperback
Published in Apr 2020
Publisher Packt
ISBN-13 9781789956481
Length 482 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Karim Okasha Karim Okasha
Author Profile Icon Karim Okasha
Karim Okasha
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Building Blocks of Ansible 2. Managing Cisco IOS Devices Using Ansible FREE CHAPTER 3. Automating Juniper Devices in the Service Providers Using Ansible 4. Building Data Center Networks with Arista and Ansible 5. Automating Application Delivery with F5 LTM and Ansible 6. Administering a Multi-Vendor Network with NAPALM and Ansible 7. Deploying and Operating AWS Networking Resources with Ansible 8. Deploying and Operating Azure Networking Resources with Ansible 9. Deploying and Operating GCP Networking Resources with Ansible 10. Network Validation with Batfish and Ansible 11. Building a Network Inventory with Ansible and NetBox 12. Simplifying Automation with AWX and Ansible 13. Advanced Techniques and Best Practices for Ansible 14. Other Books You May Enjoy

Validating network reachability on IOS devices

In this recipe, we will outline how to validate network reachability via ping using Ansible. ICMP allows us to validate proper forwarding across our network. Using Ansible to perform this task provides us with a robust tool to validate proper traffic forwarding, since we can perform this task from each node simultaneously and collect all the results for further inspection.

Getting ready

This recipe is built based on the network setup that was outlined in the chapter introduction, and I am assuming that the network has already been built in accordance with all the previous recipes in this chapter.

How to do it...

  1. Create a new playbook called pb_net_validate.yml and add the following task to store all SVI IP addresses:
---
- name: "PLay 1: Validate Network Reachability"
hosts: core,wan
vars:
host_id: 10
packet_count: 10
tasks:
- name: "Get all SVI Prefixes"
set_fact:
all_svi_prefixes: "{{ svi_interfaces | selectattr('vrrp') |
map(attribute='ipv4') | list }}"
run_once: yes
delegate_to: localhost
tags: svi
  1. Update the pb_net_validate.yml playbook with the following task to ping all the SVI interfaces:
      - name: "Ping Hosts in all VLANs"
ios_ping:
dest: "{{ item | ipaddr(10) | ipaddr('address') }}"
loop: "{{ all_svi_prefixes }}"
ignore_errors: yes
tags: svi

How it works...

In this playbook, we are using the ios_ping module, which logs into each node defined in our Ansible inventory, and pings the destination specified by the dest attribute. In this sample playbook, we would like to validate network reachability to a single host within the data, voice, and web VLANs, and we choose the tenth host in all these VLANs (just as an example). In order to build all the VLAN prefixes we set in the first task, we add a new variable called all_svi_prefixes and use multiple jinja2 filters to collect only those prefixes that are running VRRP (so as to remove any core VLANs). We get only the IPv4 attributes for these SVI interfaces. The following are the contents of this new variable after running the first task:

ok: [core01 -> localhost] => {
"all_svi_prefixes": [
"10.1.10.0/24",
"10.1.20.0/24",
"10.1.100.0/24"
]
}

We supply this new list data structure to the ios_ping module and we specify that we need to ping the tenth host within each subnet. As long as the ping succeeds, the task will succeed. However, if there is a connectivity problem from the router/switch to this host, the task will fail. We are using the ignore_errors parameter in order to ignore any failure that might occur owing to the fact that the host is unreachable/down, and to run any subsequent tasks. The following code snippet outlines the successful run:

TASK [P1T2: Ping Hosts in all VLANs] *****************************
ok: [core01] => (item=10.1.10.0/24)
ok: [core02] => (item=10.1.10.0/24)
ok: [wan01] => (item=10.1.10.0/24)
ok: [wan02] => (item=10.1.10.0/24)
ok: [core01] => (item=10.1.20.0/24)
ok: [core02] => (item=10.1.20.0/24)
ok: [core01] => (item=10.1.100.0/24)
ok: [wan01] => (item=10.1.20.0/24)
ok: [wan02] => (item=10.1.20.0/24)
ok: [core02] => (item=10.1.100.0/24)
ok: [wan01] => (item=10.1.100.0/24)
ok: [wan02] => (item=10.1.100.0/24)
You have been reading a chapter from
Network Automation Cookbook
Published in: Apr 2020
Publisher: Packt
ISBN-13: 9781789956481
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image