Search icon CANCEL
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

Configuring interface IP addresses

In this recipe, we will explore how to configure the interface IP address on Cisco IOS devices. We will use the sample topology to configure the VLAN interfaces on both the core switches. We will outline how to configure VRRP between the core switches for all the VLAN interfaces. We will configure the following IP addresses:

Interface

Prefix

VRRP IP address

VLAN10

10.1.10.0/24

10.1.10.254

VLAN20

10.1.20.0/24

10.1.20.254

VLAN100

10.1.100.0/24

10.1.100.254

Getting ready

This recipe assumes that the interface and VLANs are configured as per the previous recipes in this chapter.

How to do it...

  1. Update the group_vars/core.yml file with following data to define the SVI interfaces:
$ cat group_vars/core.yml
<-- Output Trimmed for brevity ------>
svi_interfaces:
- name: Vlan10
ipv4: 10.1.10.0/24
vrrp: yes
ospf: passive
- name: Vlan20
ipv4: 10.1.20.0/24
vrrp: yes
ospf: passive
- name: Vlan100
ipv4: 10.1.100.0/24
vrrp: yes
ospf: passive
  1. Create core01.yml and core02.yml files under the host_vars folder and add the following content:
$ cat host_vars/core01.yml
hst_svi_id: 1
hst_vrrp_priority: 100
$ cat host_vars/core02.yml
hst_svi_id: 2
hst_vrrp_priority: 50
  1. Update the pb_build_network.yml playbook with the following tasks to create and enable the L3 SVI interfaces:
- name: "PLAY 2: Configure Core Switches"
hosts: core
tags: l3_core
tasks:
<-- Output Trimmed for brevity ------>
- name: "Create L3 VLAN Interfaces"
ios_l3_interface:
name: "{{item.name }}"
ipv4: "{{item.ipv4 | ipv4(hst_svi_id)}}"
loop: "{{svi_interfaces}}"
tags: l3_svi
- name: "Enable the VLAN Interfaces"
ios_interface:
name: "{{ item.name }}"
state: up
loop: "{{ svi_interfaces }}"
  1. Update the playbook with the following task to set up VRRP configuration on the SVI interfaces:
    - name: "Create VRRP Configs"
ios_config:
parents: interface {{ item.name }}
lines:
- vrrp {{item.name.split('Vlan')[1]}} priority {{ hst_vrrp_priority }}
- vrrp {{item.name.split('Vlan')[1]}} ip {{item.ipv4 | ipv4(254)|ipaddr('address')}}
loop: "{{svi_interfaces | selectattr('vrrp','equalto',true) | list }}"

How it works...

In this section, we are configuring the IP addresses for the L3 VLAN interfaces on the core switches, as well as configuring VRRP on all the L3 VLAN interfaces to provide L3 redundancy.

We are using a new list data structure called svi_interfaces, which describes all the SVI interfaces with L3 IP addresses, and also some added parameters to control both the VRRP and OSPF configured on these interfaces. We also set up two new variables on each core router, hst_svi_id and hst_vrrp_priority, which we will use in the playbook to control the IP address on each core switch, as well as the VRPP priority.

We use the ios_l3_interface Ansible module to set the IPv4 addresses on the VLAN interfaces. On each core switch, we loop over the svi_interfaces data structure, and for each VLAN we configure the IPv4 address on the corresponding VLAN interface. We determine which IP address is configured on each router using the Ansible ipaddr filter, along with the hst_svi_id parameter {{item.ipv4 | ipv4(hst_svi_id)}} . So, for example, for VLAN10, we will assign 10.1.10.1/24 for core01 and 10.1.10.2/24 for core02.


When first creating the VLAN interface on Cisco IOS devices, they are in a state of shutdown, so we need to enable them. We use the ios_interface module to enable the interfaces.

For the VRRP part, we return to using the ios_config module to set up the VRRP configuration on all the VLAN interfaces, and we use hst_vrrp_priority to correctly set up core01 as the master VRRP for all VLANs.

The following is a sample of the configuration that is pushed on the devices after running the playbook:

Core01
========
!
interface Vlan10
ip address 10.1.10.1 255.255.255.0
vrrp 10 ip 10.1.10.254
!
Core02
=======
!
interface Vlan10
ip address 10.1.10.2 255.255.255.0
vrrp 10 ip 10.1.10.254
vrrp 10 priority 50

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