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

Retrieving operational data from IOS devices

In this recipe, we will outline how to execute operational commands on IOS devices and store these outputs to text files for further processing. This allows us to capture any operational commands from IOS devices during pre- or post-validation after we perform any deployment so that we can compare the results.

Getting ready

In order to follow along with this recipe, an Ansible inventory file should be in place and the network should already be set up as per the previous recipes.

How to do it...

  1. Create a new playbook called pb_op_cmds.yml and populate it with the following tasks to create the directory structure to save the output from the devices:
---
- name: "Play 1: Execute Operational Commands"
hosts: network
vars:
config_folder: "configs"
op_folder: "op_data"
op_cmds:
- show ip ospf neighbor
- show ip route
tasks:
- name: "P1T1: Build Directories to Store Data"
block:
- name: "Create folder to store Device config"
file:
path: "{{ config_folder }}"
state: directory
- name: "Create Folder to store operational commands"
file:
path: "{{ op_folder }}"
state: directory
run_once: yes
delegate_to: localhost
  1. Update the pb_op_cmds.yml playbook and populate it with the following tasks to retrieve the running configuration from the devices:
      - name: "P1T2: Get Running configs from Devices"
ios_command:
commands: show running-config
register: show_run
- name: "P1T3: Save Running Config per Device"
copy:
content: "{{ show_run.stdout[0] }}"
dest: "{{ config_folder }}/{{ inventory_hostname }}.cfg"
  1. Update the playbook and populate it with the following tasks to retrieve the operational commands from the devices and save it:
      - name: "P1T4: Create Folder per Device"
file:
path: "{{ op_folder}}/{{ inventory_hostname }}"
state: directory
delegate_to: localhost
- name: "P1T5: Get Operational Data from Devices"
ios_command:
commands: "{{ item }}"
register: op_output
loop: "{{ op_cmds }}"
- name: "P1T6: Save output per each node"
copy:
content: "{{ item.stdout[0] }}"
dest: "{{ op_folder}}/{{ inventory_hostname }}/{{item.item | replace(' ', '_')}}.txt"
loop: "{{ op_output.results }}"

How it works...

In this recipe, we are using the ios_command module in order to execute operational commands on the IOS devices, and saving them to text files. In order to achieve this goal, we perform the following steps:

  • We create the folders that we will store the output to, and we create a folder called configs to store the running config of all the devices. We also create an op_data file to store the output of the operational commands that we will get from the devices.
  • We then execute the show running command on all the IOS devices in our inventory and we register the output in a new variable called show_run.
  • We use the copy module to save the output from the previous task to a file for each device. The output from the command run is saved in the stdout variable. As we executed a single command, the stdout variable only has a single item (stdout[0]).

Once we execute this task, we can see that the configs folder is populated as shown in the following output:

$ tree configs/
configs/
├── access01.cfg
├── access02.cfg
├── core01.cfg
├── core02.cfg
├── isp01.cfg
├── wan01.cfg
└── wan02.cfg

For the next part, we create a folder for each node to store the output from the multiple show commands that we will execute on the IOS devices.

We use the ios_command module to execute the show commands on the devices, and save all the output in a new variable called op_output. We use the copy execute command, show ip route, and we create a file for the output of this command with the name show_ip_route.txt.

After running this task, we can see that this is the current structure of the op_data folder:

$ tree op_data/
op_data/
├── access01
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
├── access02
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
├── core01
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
├── core02
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
├── isp01
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
├── wan01
│ ├── show_ip_ospf_neighbor.txt
│ └── show_ip_route.txt
└── wan02
├── show_ip_ospf_neighbor.txt
└── show_ip_route.txt

We can check the content of one of the files to confirm that all the data has been stored:

$ head op_data/core01/show_ip_ospf_neighbor.txt

Neighbor ID Pri State Dead Time Address Interface
10.100.1.3 0 FULL/ - 00:00:37 10.3.1.2 Ethernet1/0
10.100.1.2 0 FULL/ - 00:00:36 10.1.200.2 Vlan200
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