Why DevOps Needs a Language — Python
Every Monday you log in to the server web1, check that the notes-api service answers, look at the free disk space, and restart the service if it has hung. It takes twenty minutes and you skip a step whenever you are in a hurry. A short program would do it in seconds, exactly the same way every time.
The rule
Do it by hand once, then write it down as code.
DevOps is about making operations repeatable. A manual checklist lives in someone's head and changes with their mood. A script is written down, can be reviewed, can be stored in Git, and runs the same way at 3 a.m. as at 3 p.m.
You need one general-purpose language for this, and Python is the most common first choice for three plain reasons:
- It reads like the checklist. A newcomer can follow a Python script without knowing much Python.
- The standard library covers ops work.
subprocessruns other programs,pathlibandshutilhandle files,jsonreads API responses,urllib.requestmakes HTTP calls. - It is usually already there. Most Linux distributions ship a Python 3 interpreter, so a small script often needs nothing extra installed.
Here is the Monday health check, the first step of it:
import shutil, urllib.request
with urllib.request.urlopen("https://notes.example.com/health", timeout=5) as r:
print("notes-api answered", r.status)
total, used, free = shutil.disk_usage("/")
print(f"free disk: {free // 2**30} GiB")