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.
Most ops scripting is: run a command, parse its output, decide. Modern tools speak JSON so you don't regex.
Call ip -j addr from Python and print your interface names.
$ python3 ifaces.pyIt prints your interface names — parsed from real kernel JSON, no fragile text scraping.
Reveal 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
Good tools have a real interface: flags, help, exit codes. argparse gives you all three.
Write a tiny CLI that takes --name and greets.
$ python3 tool.py --name world--help prints usage; --name world greets. This is the skeleton of every internal tool you'll write.
Reveal 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
Automation lives on top of APIs. The stdlib can do it with zero installs.
GET a JSON endpoint and print one field.
$ python3 api.pyIt prints a field from a live JSON response — the pattern behind every integration you'll build.
Reveal 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
Reach engineers who read the man page
Native, contextual, no tracking — this is how the curriculum stays free.
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.
Write a loop that ensures a set of files exists, converging on each pass and doing nothing once matched.
$ python3 reconcile.pyFirst 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
$ 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
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.