Lesson 3 - Creating plays
Documentation on creating Plays with ansible can be found here. We will be using the Cisco IOS Collection and templates with Jinja2 to create the configurations that will be sent to each device via an SSH session from our Ansible control node. So with all of this information lets create a play to reach out to one of our switches and pull back the configured vlan database.
In your main folder (Ansible_Workshop) create a new file pb.get.vlans.yaml. Every play needs the below structure. At the top of the play we list what and how we are connecting to with
- hosts: we will connect to podxsw3.
- Gather_facts in our use case will always be false.
- Connection will be network_cli.
Below these details we will list out the tasks to be performed in this play. Notice the structure of the file below. indentation is key to ensure that ansible can read in this file. Our first task is using the cisco ios collection to run the command on podxsw3 “show vlan”. The register will store the output of “show vlan”. Our next tasks is to take that stored result and display it on our terminal window. Ansible has a debug that will handle this and is a useful way to validate the results Ansible is getting from the device. We could also print it to a file if you desired. With the help of clay584s parse_genie this “show vlan” output will be displayed in a structured yaml format.
To run the play type into the terminal ansible-playbook -i inventory/inventory.yaml pb.get.vlans.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
---
############################################################
# Pulls down the existing vlan database from a cisco switch
############################################################
- name: Connect to access switches
hosts: pod1sw3
gather_facts: false
connection: network_cli
tasks:
- name: show vlan
ios_command:
commands:
- "show vlan"
register: ios_output
- name: Print Structured Data
debug:
msg: "{{ ios_output['stdout'][0] | clay584.genie.parse_genie(command='show vlan', os='ios') }}"
delegate_to: localhost
The results of (show vlan) from the cli would look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Gi0/0, Gi0/1, Gi0/2, Gi0/3
Gi1/0, Gi1/1, Gi1/2
300 USERS active
350 SERVERS active
400 GUEST active
666 NATIVE_VLAN active
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
1 enet 100001 1500 - - - - - 0 0
300 enet 100300 1500 - - - - - 0 0
350 enet 100350 1500 - - - - - 0 0
400 enet 100400 1500 - - - - - 0 0
666 enet 100666 1500 - - - - - 0 0
1002 fddi 101002 1500 - - - - - 0 0
1003 tr 101003 1500 - - - - - 0 0
VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
1004 fdnet 101004 1500 - - - ieee - 0 0
1005 trnet 101005 1500 - - - ibm - 0 0
Primary Secondary Type Ports
------- --------- ----------------- ------------------------------------------
Parse_genie parses results of the show vlan command and prints the result in our terminal window
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
msg:
vlans:
'1':
interfaces:
- GigabitEthernet0/0
- GigabitEthernet0/1
- GigabitEthernet0/2
- GigabitEthernet0/3
- GigabitEthernet1/0
- GigabitEthernet1/1
- GigabitEthernet1/2
mtu: 1500
name: default
said: 100001
shutdown: false
state: active
trans1: 0
trans2: 0
type: enet
vlan_id: '1'
'1002':
mtu: 1500
name: fddi-default
said: 101002
shutdown: false
state: unsupport
trans1: 0
trans2: 0
type: fddi
vlan_id: '1002'
'1003':
mtu: 1500
name: token-ring-default
said: 101003
shutdown: false
state: unsupport
trans1: 0
trans2: 0
type: tr
vlan_id: '1003'
'1004':
mtu: 1500
name: fddinet-default
said: 101004
shutdown: false
state: unsupport
stp: ieee
trans1: 0
trans2: 0
type: fdnet
vlan_id: '1004'
'1005':
mtu: 1500
name: trnet-default
said: 101005
shutdown: false
state: unsupport
stp: ibm
trans1: 0
trans2: 0
type: trnet
vlan_id: '1005'
'300':
mtu: 1500
name: USERS
said: 100300
shutdown: false
state: active
trans1: 0
trans2: 0
type: enet
vlan_id: '300'
'350':
mtu: 1500
name: SERVERS
said: 100350
shutdown: false
state: active
trans1: 0
trans2: 0
type: enet
vlan_id: '350'
'400':
mtu: 1500
name: GUEST
said: 100400
shutdown: false
state: active
trans1: 0
trans2: 0
type: enet
vlan_id: '400'
'666':
mtu: 1500
name: NATIVE_VLAN
said: 100666
shutdown: false
state: active
trans1: 0
trans2: 0
type: enet
vlan_id: '666'
The results are now in a format that we can store and reuse for validation of changes. This is something that is currently out scope, but will be something added to this workshop eventually.