Stop 05 · Scripting & Programming for DevOpsTopics · 1/8

Python for Automation

Part of the DevOps Roadmap

In short

Bash is perfect until your script needs to call three APIs, parse JSON and handle retries. That's when DevOps engineers reach for Python, and the script stays readable a year later.

A general-purpose language widely used for DevOps automation beyond what Bash comfortably handles — API interactions, complex logic, and cross-platform scripts.

The mascot stands between an auto-rickshaw in a narrow lane and a roomy car on an open road.

Think of it like this

Auto-rickshaw vs a car

For a quick trip down the gully, an auto is perfect: hop in, two minutes, done. For a long drive with family and luggage, you want a car with space and a proper boot. Bash is the auto for short jobs. Python is the car for bigger, longer ones.

Watch it happen

A health-check script at work

check.py
Shop API
Slack

GET /health

1/5The script calls each service's health endpoint with a 5-second timeout.

The big ideas

What you need to know

Batteries included

Python's standard library handles files, JSON, subprocesses and dates. requests and boto3 cover HTTP and AWS.

Use a virtual environment

python3 -m venv .venv keeps each tool's libraries separate, so one upgrade doesn't break another script.

argparse for real CLIs

Give scripts proper flags like --env and --dry-run with help text, instead of guessing what $1 means.

Bash or Python?

Mostly gluing commands together? Bash. Logic, loops over data, APIs or error handling? Python.

See it in code

Walk through the code

python
1import os, sys2import requests3 4URLS = ["https://shop.example.com/health", "https://api.example.com/health"]5WEBHOOK = os.environ["SLACK_WEBHOOK_URL"]6failed = []7for url in URLS:8    try:9        r = requests.get(url, timeout=5)10        r.raise_for_status()11    except requests.RequestException as e:12        failed.append(f"{url}: {e}")13 14if failed:15    requests.post(WEBHOOK, json={"text": "\n".join(failed)}, timeout=5)16    sys.exit(1)

1/4Standard library modules plus requests, the go-to library for HTTP calls.

Install requests inside a venv with pip install requests. Reading the webhook from an environment variable keeps the secret out of the code.

Save yourself the pain

Common mistakes

Oops

Calling requests.get without a timeout, so the script hangs forever when a server stops responding.

Do this instead

Always pass timeout=. A stuck automation script is often worse than one that fails quickly.

Oops

Installing packages with sudo pip into the system Python.

Do this instead

Use a venv per project. Many distros now block system-wide pip installs for this reason.

Your mission

Your first ops script

~30 min

Quick check

Why should an automation script exit with a non-zero code when a check fails?

Go deeper

  1. 1Write a Python script automating a task you'd otherwise do manually (e.g. renaming files by a pattern).
  2. 2Learn to use Python's requests library for basic API automation.
  3. 3Practice reading and debugging a Python traceback rather than guessing at fixes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge