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")The shortcut
Turn the checklist into code one line per manual step. Write each step as a sentence first, then replace each sentence with the call that does it. You never have to design a program; you only translate a list you already follow.
Write the manual steps
"Ask the health URL. Read the free disk space. If the service did not answer, restart it."
The trap is automating a task you do not yet understand. Do it by hand first, carefully, so the script encodes the right steps, then stop doing it by hand.
Drills
Answer fast — trust the rule.
Why do DevOps engineers learn a general-purpose language?
Which Python standard-library module runs another program, such as systemctl?
A 12-step release checklist is done by hand every Friday and a step is often missed. What is the best first move?
Which is NOT a reason Python is a common first ops language?
Remember
- Do it by hand once, then write it down as code.
- Shortcut: one line of code per manual step, in the same order.
- Trap: automating a task you do not understand yet.
