sn sysnerdunderstand it from the kernel up
← curriculum map
Python for DevOps
Capstone · ~60 min · 4 labs
map / The stack / Python for DevOps
The stack Capstone ~60 min requires: Kubernetes

Python for DevOps

You spent ten modules doing things by hand. Now automate the toil. This capstone converges the whole curriculum: script the primitives, talk to the APIs, and write your own tiny controller.

Before you start. Python 3 ships on Ubuntu. Nothing else required for the core labs.
Q1Shell out and parse structured outputwarm-up

Most ops scripting is: run a command, parse its output, decide. Modern tools speak JSON so you don't regex.

Task

Call ip -j addr from Python and print your interface names.

Verify it yourself
verify
$ python3 ifaces.py

It prints your interface names — parsed from real kernel JSON, no fragile text scraping.

Reveal solution
solution
$ cat > ifaces.py <<EOF
import subprocess, json
out = subprocess.run(["ip","-j","addr"], capture_output=True, text=True).stdout
for i in json.loads(out):
    print(i["ifname"])
EOF
$ python3 ifaces.py
Q2A real CLI with argparsecore

Good tools have a real interface: flags, help, exit codes. argparse gives you all three.

Task

Write a tiny CLI that takes --name and greets.

Verify it yourself
verify
$ python3 tool.py --name world

--help prints usage; --name world greets. This is the skeleton of every internal tool you'll write.

Reveal solution
solution
$ cat > tool.py <<EOF
import argparse
p = argparse.ArgumentParser()
p.add_argument("--name", required=True)
a = p.parse_args()
print(f"hello, {a.name}")
EOF
$ python3 tool.py --help
$ python3 tool.py --name world
Q3Talk to an APIcore

Automation lives on top of APIs. The stdlib can do it with zero installs.

Task

GET a JSON endpoint and print one field.

Verify it yourself
verify
$ python3 api.py

It prints a field from a live JSON response — the pattern behind every integration you'll build.

Reveal solution
solution
$ cat > api.py <<EOF
import urllib.request, json
req = urllib.request.urlopen("https://api.github.com/repos/kubernetes/kubernetes")
data = json.load(req)
print("stars:", data["stargazers_count"])
EOF
$ python3 api.py
Sponsored

Reach engineers who read the man page

Native, contextual, no tracking — this is how the curriculum stays free.

Q4Capstone — write your own reconcile loopcapstone

Kubernetes is a reconcile loop. Build the smallest possible one: given desired state, make reality match, repeatedly, idempotently. This is the pattern under every controller and every Terraform apply.

Task

Write a loop that ensures a set of files exists, converging on each pass and doing nothing once matched.

Verify it yourself
verify
$ python3 reconcile.py

First pass creates the missing files; the next pass reports "in sync" and changes nothing — you just wrote a controller. That's the whole idea behind Kubernetes, in 15 lines.

Reveal solution
solution
$ cat > reconcile.py <<EOF
import os, time
desired = {"/tmp/a.txt","/tmp/b.txt","/tmp/c.txt"}
for _ in range(3):
    missing = {p for p in desired if not os.path.exists(p)}
    for p in missing:
        open(p,"w").close(); print("created", p)
    if not missing: print("in sync"); 
    time.sleep(1)
EOF
$ python3 reconcile.py
What you just built

subprocess, argparse, APIs, a reconcile loop — you can now automate every primitive this curriculum taught you to do by hand. That's the whole point: understand it from the kernel up, then make the machine do it for you.