Capstone project
Capstone: from source of truth to the network
Lesson 20 of 20 · 30 min · Module 6: Safe changes and the capstone
The capstone joins the whole course. The inventory says that VLAN 30, GUESTS, is reserved for Bucharest. Deploy it to every active access switch there, verify it, save, and only then mark the VLAN active in the inventory.
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…
- Read the intent from the source of truth
- Deploy, verify and save idempotently
- Write the result back with PATCH
The idea
This is the loop real network automation runs on. Read the intent from the source of truth: the VLAN from /api/ipam/vlans/?site=bucharest&vid=30 and the switches from /api/dcim/devices/?site=bucharest&role=access&status=active. Deploy idempotently: add the VLAN only where it is missing (name on Cisco, description on Huawei). Verify by reading the VLANs again, then save. Record the result: when every switch has the VLAN, send a PATCH with json={"status": "active"} to the VLAN's URL, and write a report file.
response = requests.patch(vlan["url"], headers=HEADERS,
json={"status": "active"})
response.raise_for_status()
print(response.json()["status"]["value"])
01The VLAN, as the inventory sees it
The VLAN record carries its own url, which is also the address to PATCH later.
import os
import requests
URL = os.environ["INVENTORY_URL"]
HEADERS = {"Authorization": f"Token {os.environ['INVENTORY_TOKEN']}",
"Accept": "application/json"}
params = {"site": "bucharest", "vid": 30}
answer = requests.get(f"{URL}/api/ipam/vlans/", headers=HEADERS, params=params)
vlan = answer.json()["results"][0]
print(vlan["name"], vlan["status"]["value"])
print(vlan["url"])GUESTS reserved
https://inventory.lab/api/ipam/vlans/4/02Ask for exactly the devices you need
Several filters combine: the API returns only devices that match all of them.
params = {"site": "bucharest", "role": "access", "status": "active"}
url = f"{URL}/api/dcim/devices/"
answer = requests.get(url, headers=HEADERS, params=params)
switches = answer.json()["results"]
print([(s["name"], s["platform"]["slug"]) for s in switches])[('sw1', 'cisco_ios'), ('sw2', 'cisco_ios'), ('sw3', 'huawei')]03PATCH changes only the fields you send
This changes the description of one interface record and leaves everything else as it was. json= sends the dictionary as JSON with the right Content-Type.
params = {"device": "sw1", "name": "GigabitEthernet0/6"}
url = f"{URL}/api/dcim/interfaces/"
answer = requests.get(url, headers=HEADERS, params=params)
port = answer.json()["results"][0]
response = requests.patch(port["url"], headers=HEADERS,
json={"description": "Spare"})
print(response.status_code, response.json()["description"])200 SpareYour project brief
report: switch name → "added" or "present". When every switch has the VLAN, PATCH its status to active. Write report to report.json.What the checks look for
- report has the three access switches
- VLAN 30 GUESTS is on sw1, sw2 and sw3
- sw2 and core1 received no configuration
- Verified each changed switch after the change
- Changed switches are saved
- The VLAN is active in the inventory
- report.json matches report
Take this with you
Source of truth, idempotent deploy, verification, and back to the source of truth: that loop is network automation.
Go a little further
Add DRY_RUN = True at the top: build the report and print the planned commands, but send nothing and skip the PATCH.