In this recipe, we will outline how to configure L2 VLANs on Cisco IOS devices, as per the network topology discussed in the introduction to this chapter. We will outline how to declare VLANs as Ansible variables, and how to use suitable Ansible modules to provision these VLANs on the network.
Configuring L2 VLANs on IOS devices
Getting ready
We will be building on the previous recipes discussed in this chapter to continue to configure the L2 VLANs on all the LAN devices within our sample topology.
How to do it...
- Update the group_vars/lan.yml file with the VLAN definition, as outlined in the following code:
$ cat group_vars/lan.yaml
vlans:
- name: Data
vlan_id: 10
- name: Voice
vlan_id: 20
- name: Web
vlan_id: 100
- Update the pb_build.yml playbook with the following task to provision the VLANs:
- name: "P1T4: Create L2 VLANs"
ios_vlan:
vlan_id: "{{ item.vlan_id }}"
name: "{{ item.name }}"
loop: "{{ vlans }}"
tags: vlan
How it works...
In the group_vars/lan.yml file, we define a vlans list data structure that holds the VLAN definition that we need to apply to all our core and access switches. This variable will be available for all the core and access switches, and Ansible will use this variable in order to provision the required VLANs on the remote devices.
We use another declarative module, ios_vlan, which takes the VLAN definition (its name and the VLAN ID) and configures these VLANs on the remote managed device. It pulls the existing configuration from the device and compares it with the list of devices that need to be present, while only pushing the delta.
We use the loop construct to go through all the items in the vlans list, and configure all the respective VLANs on all the devices.
After running this task on the devices, the following is the output from one of the access switches:
access01#sh vlan
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Et1/0, Et1/1, Et1/2, Et1/3
10 Data active Et0/3
20 Voice active
100 Web active