Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Collecting IOS device facts

In this recipe, we will outline how to collect device facts from Cisco devices with Ansible. This information includes the serial number, IOS version, and all the interfaces on the devices. Ansible executes several commands on managed IOS devices in order to collect this information.

Getting ready

The Ansible controller must have IP connectivity with the managed network devices, and SSH must be enabled on the IOS devices.

How to do it...

  1. Create a new playbook called pb_collect_facts.yml in the same ch2_ios folder with the following information:
---
- name: "PLAY 1: Collect Device Facts"
hosts: core,wan
tasks:
- name: "P1T1: Gather Device Facts"
ios_facts:
register: device_facts
- debug: var=device_facts

How it works...

We run this new playbook against all nodes within the core and wan group, and we use the ios_facts module to collect the information from the managed IOS devices. In this recipe, we use the debug module to print out the information that was collected from the ios_facts module. The following is a subset of the information that was discovered:

ok: [core01 -> localhost] => {
"Ansible_facts": {
"net_all_ipv4_addresses": [
"172.20.1.20",
< ---------- Snippet ------------ >
"10.1.100.1"
],
"net_hostname": "core01",
"net_interfaces": {
< ---------- Snippet ------------ >
"Vlan10": {
"bandwidth": 1000000,
"description": null,
"duplex": null,
"ipv4": [
{
"address": "10.1.10.1",
"subnet": "24"
}
],
"lineprotocol": "up",
"macaddress": "aabb.cc80.e000",
"mediatype": null,
"mtu": 1500,
"operstatus": "up",
"type": "Ethernet SVI"
},

},
"net_iostype": "IOS",
"net_serialnum": "67109088",
"net_system": "ios",
"net_version": "15.1",
}
< ------------ Snippet ------------ >
}

From the preceding output, we can see some of the main facts that the ios_facts module has captured from the devices, including the following:

  • net_all_ipv4_addresses: This list data structure contains all the IPv4 addresses that are configured on all the interfaces on the IOS device.
  • net_interfaces: This dictionary data structure captures the status of all of the interfaces on this device and their operational state, as well as other important information, such as a description and their operational state.
  • net_serialnum: This captures the serial number of the device.
  • net_version: This captures the IOS version running on this device.

There's more...

Using the information that is collected from the ios_facts module, we can generate structured reports for the current state of the network and use these reports in further tasks. In this section, we will outline how to modify our playbook to build this report.

Add a new task to the pb_collect_facts.yml playbook, as shown in the following code:

- name: "P1T2: Write Device Facts"
blockinfile:
path: ./facts.yml
create: yes
block: |
device_facts:
{% for host in play_hosts %}
{% set node = hostvars[host] %}
{{ node.Ansible_net_hostname }}:
serial_number: {{ node.Ansible_net_serialnum }}
ios_version: {{ node.Ansible_net_version }}
{% endfor %}
all_loopbacks:
{% for host in play_hosts %}
{% set node = hostvars[host] %}
{% if node.Ansible_net_interfaces is defined %}
{% if node.Ansible_net_interfaces.Loopback0 is defined %}
- {{ node.Ansible_net_interfaces.Loopback0.ipv4[0].address }}
{% endif %}
{% endif %}
{% endfor %}
run_once: yes
delegate_to: localhost

We use the blockinfile module to build a YAML file called facts.yml. We use Jinja2 expressions within the blockinfile module to customize and select the information we want to capture from the Ansible facts that were captured from the ios_facts task. When we run the pb_collect_facts.yml playbook, we generate the facts.yml file, which has the following data:

device_facts:
wan01:
serial_number: 90L4XVVPL7V
ios_version: 16.06.01
wan02:
serial_number: 9UOFOO7FH19
ios_version: 16.06.01
core01:
serial_number: 67109088
ios_version: 15.1
core02:
serial_number: 67109104
ios_version: 15.1
all_loopbacks:
- 10.100.1.3
- 10.100.1.4
- 10.100.1.1
- 10.100.1.2

See also...

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