Python for Automation
Python is the most common glue language in operations work: readable, batteries-included and supported by every cloud SDK.
Beginner · about 25 hours · difficulty 2/5
Every DevOps engineer needs one real programming language for tasks that outgrow a shell script: calling cloud APIs, parsing JSON and YAML, writing small CLIs, generating config, or building a Slack bot that reports deploys. Python is our recommendation because it is easy to read, installed almost everywhere, and has first-class libraries for AWS (boto3), Azure, GCP, Kubernetes and Ansible.
What to learn
- Core syntax: variables, functions, lists, dicts, loops, comprehensions, exceptions.
- Files and paths with
pathlib, running commands withsubprocess.run, reading env vars withos.environ. - Data formats:
json,csv, and YAML viaPyYAML. - HTTP calls with
requestsorhttpx, and handling timeouts and retries. - Building CLIs with
argparse(orclick/typer), and logging with theloggingmodule. - Isolated environments with
venvand pinned dependencies (requirements.txtorpyproject.toml).
Pitfalls
Installing packages into the system Python, catching every exception silently, and hard-coding credentials. Always read secrets from the environment or a secret manager and exit with a non-zero code on failure so pipelines notice.
Good enough to move on: you can write a 100-200 line script with arguments, logging, error handling and a virtual environment, that talks to an HTTP API and writes a report.
Key concepts
- Syntax, data structures and functions
- Files, paths and subprocess
- JSON, YAML and CSV handling
- HTTP clients and API calls
- argparse / click CLIs
- venv and dependency pinning
- boto3 and cloud SDKs
- pytest for scripts
Other options for this step
- Go for Tooling: Go compiles to a single static binary and is the language most cloud-native tools (Docker, Kubernetes, Terraform) are written in.
- Bash as Your First Language: Going deep on Bash first gets you productive on servers quickly, as long as you know when to switch to a real language.
- JavaScript / Node.js for Automation: If you already know JavaScript, Node.js is a practical automation language with strong SDKs and a huge package ecosystem.
Mini project
Write a Python CLI that takes a list of URLs from a YAML file, checks each one with a timeout, and prints a table of status codes and latency, exiting non-zero if any check fails.
Check yourself
Why should an automation script exit with a non-zero status code on failure, and what breaks if it does not?
Free resources
- The Python Tutorial (python.org)
- Automate the Boring Stuff with Python (Al Sweigart)