Pre-checks, post-checks, verification & automatic rollback
Check before and after, roll back on failure
Lesson 19 of 20 · 18 min · Module 6: Safe changes and the capstone
Careful engineers follow a plan for every change: check the state before, make the change, check again, and undo the change if the check fails. A script can follow that plan every single time.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Record the state before a change
- Prove the change worked with a post-check
- Undo the change automatically when it did not
The idea
A pre-check records the state before the change: which VLAN the port is in and which MAC address the device behind it uses. The post-check proves the change worked: the same MAC address must appear again, now in the new VLAN. If it does not, the device behind the port cannot work in the new VLAN, so the script rolls back to the VLAN it recorded. show interfaces <port> switchport shows the access VLAN, and show mac address-table interface <port> shows what the port has learned.
line = " 10 3cd9.2b00.0203 DYNAMIC Gi0/3"
vlan, mac, kind, port = line.split()
print(int(vlan), mac, port)
01What the port has learned
The MAC address table shows the printer behind Gi0/3, in VLAN 10.
import os
from netmiko import ConnectHandler
USERNAME = os.environ["NET_USERNAME"]
PASSWORD = os.environ["NET_PASSWORD"]
conn = ConnectHandler(device_type="cisco_ios", host="10.0.0.12",
username=USERNAME, password=PASSWORD)
print(conn.send_command("show mac address-table interface Gi0/3")) Mac Address Table
-------------------------------------------
Vlan Mac Address Type Ports
---- ----------- -------- -----
10 3cd9.2b00.0203 DYNAMIC Gi0/3
Total Mac Addresses for this criterion: 1 02The VLAN the port is in
show interfaces ... switchport describes the port's switching settings. One regular expression reads the access VLAN.
import re
text = conn.send_command("show interfaces Gi0/3 switchport")
print("\n".join(text.splitlines()[:8]))
print(int(re.search(r"Access Mode VLAN: (\d+)", text)[1]))Name: Gi0/3
Switchport: Enabled
Administrative Mode: static access
Operational Mode: static access
Administrative Trunking Encapsulation: dot1q
Operational Trunking Encapsulation: native
Negotiation of Trunking: Off
Access Mode VLAN: 10 (USERS)
1003When the post-check fails
This printer is set up for VLAN 10 only. After a move to VLAN 40 the table is empty for the port, which is exactly what the post-check must catch.
conn.send_config_set(["interface Gi0/3", "switchport access vlan 40"])
print(conn.send_command("show mac address-table interface Gi0/3"))
conn.send_config_set(["interface Gi0/3", "switchport access vlan 10"])
conn.disconnect() Mac Address Table
-------------------------------------------
Vlan Mac Address Type Ports
---- ----------- -------- -----
Total Mac Addresses for this criterion: 0 Your turn
move_printer(conn, port, vlan): record the port's current access VLAN and the MAC addresses learned on it, move the port to vlan, then check that every recorded MAC address is learned again in the new VLAN. If not, put the old VLAN back. Return "moved" or "rolled back". Move Gi0/3 to VLAN 40 on sw1 and sw2, store the results in results, and save both switches.What the checks look for
- The sw1 printer moved to VLAN 40
- The sw2 printer was rolled back to VLAN 10
- The sw2 printer works again
- Checked the port before and after the change
- Both switches are saved
Take this with you
Check before, change, check after, and roll back automatically when the check fails.
Go a little further
Keep a log: append (time, device, step, result) for the pre-check, the change, the post-check and any rollback, and print it at the end.