Book Image

Network Automation Cookbook

By : Karim Okasha
Book Image

Network Automation Cookbook

By: Karim Okasha

Overview of this book

Network Automation Cookbook is designed to help system administrators, network engineers, and infrastructure automation engineers to centrally manage switches, routers, and other devices in their organization's network. This book will help you gain hands-on experience in automating enterprise networks and take you through core network automation techniques using the latest version of Ansible and Python. With the help of practical recipes, you'll learn how to build a network infrastructure that can be easily managed and updated as it scales through a large number of devices. You'll also cover topics related to security automation and get to grips with essential techniques to maintain network robustness. As you make progress, the book will show you how to automate networks on public cloud providers such as AWS, Google Cloud Platform, and Azure. Finally, you will get up and running with Ansible 2.9 and discover troubleshooting techniques and network automation best practices. By the end of this book, you'll be able to use Ansible to automate modern network devices and integrate third-party tools such as NAPALM, NetBox, and Batfish easily to build robust network automation solutions.
Table of Contents (15 chapters)

Configuring trunk and access interfaces

In this recipe, we will show how to configure access and trunk interfaces on Cisco IOS-based devices, and how to map interfaces to an access VLAN, as well as how to allow specific VLANs on the trunks.

Getting ready

Following our sample topology, we will configure the interfaces on the devices. As shown in this table, we are only showing the VLANs for access01 and core01— the other devices are exact replicas:

Device

Interface

Mode

VLANs

Core01

Ethernet0/1

Trunk

10,20,100

Core01

Ethernet0/2

Trunk

10,20,100

Core01

Ethernet0/3

Trunk

10,20,100,200

Access01

Ethernet0/1

Trunk

10,20,100

Access01

Ethernet0/2

Trunk

10,20,100

Access01

Ethernet0/3

Access

10

How to do it...

  1. Create a new core.yml file under group_vars and include the following core_vlans definition:
core_vlans:
- name: l3_core_vlan
vlan_id: 200
interface: Ethernet0/3
  1. Update the pb_build_network.yml playbook with the following tasks to configure all trunk ports:
  - name: "Configure L2 Trunks"
ios_l2_interface:
name: "{{ item.name }}"
mode: "{{ item.mode }}"
trunk_allowed_vlans: "{{ vlans | map(attribute='vlan_id') | join(',') }}"
state: present
loop: "{{ interfaces[inventory_hostname] |
selectattr('mode','equalto','trunk') | list }}"
- name: "Enable dot1q Trunks"
ios_config:
lines:
- switchport trunk encapsulation dot1q
parents: interface {{item.name}}
loop: "{{ interfaces[inventory_hostname] |
selectattr('mode','equalto','trunk') | list }}"
tags: dot1q
  1. Update the playbook with the following task to configure all access ports:
  - name: "Configure Access Ports"
ios_l2_interface:
name: "{{ item.name }}"
mode: "{{ item.mode}}"
access_vlan: "{{ item.vlan }}"
state: present
loop: "{{ interfaces[inventory_hostname] |
selectattr('mode','equalto','access') | list }}"

How it works...

We are using the same data structure in the lan.yml file that defines all the interfaces within the LAN network and describes their type (access/trunk). In the case of access ports, we define which access interface is part of which VLAN. We will reference this list data structure to configure the access and trunk ports on all the devices within the lan group.
The interfaces within our layer2 network are one of the following two options:

Access:

  • We use ios_l2_interface with the access_vlan parameter to configure the correct access VLAN on the interface.
  • We select only the access interfaces for each device using the selectattr jinja2 filter, and we match only one interface with a mode equal to access, and we loop over this list for each device.

Trunk:

  • We use ios_l2_interface with the trunk_allowed_vlans parameter to add all the VLANs to the trunk ports, on both access and core switches.
  • We create the permitted VLAN list using the Jinja2 map and join filters and we apply this filter to the vlans list data structure. This outputs a string similar to the following: 10,20,100.
  • We select only the trunk ports using the selectattr Jinja2 filter from the interface's data structure per node.
  • We need to configure these trunks as dot1q ports; however, this attribute is still not enabled on ios_l2_interface. Hence, we use another module, ios_config, to send the required Cisco IOS command to set up the dot1q trunks.

The following output outlines the configuration applied to the access01 device as an example for both access and trunk ports:

!
interface Ethernet0/3 >> Access Port
description Data_vlan
switchport access vlan 10
switchport mode access

!
interface Ethernet0/1 >> Trunk Port
description core01_e0/1
switchport trunk encapsulation dot1q
switchport trunk allowed vlan 10,20,100
switchport mode trunk

See also...