Skip to the workspace
Pebellslearn by building
Learn a Programming Language  ›  Lesson

Why DevOps Needs a Language — Python

Python 12 minOperations work becomes reliable when repeated manual steps are written as code, and Python is a common first choice because it is readable, well equipped and usually preinstalled.

devops / Why DevOps Needs a Language — Python

Explore each scene at your own pace.

01 · The rule

The rule

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. subprocess runs other programs, pathlib and shutil handle files, json reads API responses, urllib.request makes 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")
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.