Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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")

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.

From checklist to scriptStep 1 of 4
  1. 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.

quick drill

Why do DevOps engineers learn a general-purpose language?

quick drill

Which Python standard-library module runs another program, such as systemctl?

quick drill

A 12-step release checklist is done by hand every Friday and a step is often missed. What is the best first move?

quick drill

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.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFrom checklist to script

Pick a task you would repeat on a server every week, such as checking that notes-api answers and that the disk is not full. Explain how you would turn it into a Python script and why the script is better than doing it by hand.