Building an Inventory as Code: One Device, One YAML
Turning network devices into structured data is the foundation for automation.
I have managed networks long enough to know the pain of scattered documentation. IP addresses in spreadsheets, hostnames in a wiki, firewall rules in ticket comments. When you need to act quickly, that mess slows you down.
Automation only works if the source of truth is clean. That is why I am moving toward inventory as code. The idea is simple: every device has a file, written in YAML, stored in Git. That inventory then feeds Python scripts, Ansible playbooks, and eventually CI/CD pipelines.
Why “one device, one YAML”
Keeping one file per device makes it:
Easy to read
Easy to review in Git (a small diff shows the change)
Easy to validate with a Python script or CI job
It also lines up with how I already think about production changes: controlled, traceable, peer-reviewable.
A basic example
Here is a YAML file for a lab leaf switch:
hostname: leaf1-lab
role: leaf
mgmt:
ip: 10.0.0.21/24
interfaces:
- { name: Eth1, peer: spine1, ip: 172.16.0.1/31 }
- { name: Eth2, peer: spine2, ip: 172.16.0.3/31 }
And here is a short Python script that reads all device files and prints a summary:
import glob, yaml
for f in glob.glob("inventories/lab/devices/*.yml"):
data = yaml.safe_load(open(f))
print(f"{data['hostname']:12} {data['mgmt']['ip']:15} {data['role']}")
Output:
leaf1-lab 10.0.0.21/24 leaf
That is already useful. Instead of flipping through spreadsheets, I can generate a quick summary. Later I can build checks to catch duplicate IPs or missing fields.
Where this goes next…
Python can use this data to generate configs or back up running configs.
Ansible can consume the same inventory to drive playbooks.
GitLab CI can lint these files and refuse a merge if something is invalid.
Terraform can connect the same mindset to cloud networking.
This is how I see inventory as the first building block. Get it right, and everything else becomes easier.
Automation is not about replacing engineers. It is about making the data we already know repeatable and reliable. For me that starts with a simple rule: one device, one YAML.
Next I will show how I use Python to validate that inventory and stop bad data before it ever reaches production.

