Python for DevOps

From first script to DevOps automation tool

Learn Python the way DevOps engineers actually use it β€” writing CLI tools with argparse, automating files and processes with pathlib and subprocess, handling JSON/YAML configs, calling REST APIs, and building a complete automation tool that queries GitHub and generates reports.

17 lessons
5 modules
450 min total
136 steps

Before You Start

  • βœ”Terminal access (macOS, Linux, or WSL on Windows)
  • βœ”Python 3.10+ installed (Lesson 101 walks you through setup)
  • βœ”Basic command-line familiarity (see Linux & Bash track)
  • βœ”No prior Python required β€” the track starts from the basics

5 modules β€’ 1 open

1

101 β€” Setting Up Python for DevOps

Install Python 3.12 on macOS or Linux, create an isolated virtual environment, and confirm pip is ready β€” the foundation every DevOps automation script depends on.

Beginner20 minutes8 steps
Required
Basic terminal use: cd, ls, and running commandsmacOS with Homebrew installed, or a Debian/Ubuntu Linux system
2

102 β€” Writing Your First DevOps Script

Write a Python script from scratch, add a shebang line so it runs directly from the terminal, and accept command-line arguments with sys.argv β€” the three foundational patterns every DevOps Python tool is built on.

Beginner20 minutes8 steps
Required
Lesson 101 complete: Python 3 installed and a virtual environment created and activated
3

103 β€” Building CLI Tools with argparse

Replace manual sys.argv handling with Python's built-in argparse module to define named arguments, optional flags, restricted choices, and auto-generated --help output β€” the pattern behind every professional Python CLI tool.

Intermediate25 minutes8 steps
Required
Lesson 102 complete: deploy_check.py script with shebang, chmod +x, and sys.argv argument guard
4

104 β€” Logging and Exit Codes

Replace bare print() calls with Python's stdlib logging module to emit structured, level-based output, then use sys.exit() with meaningful non-zero codes so CI/CD pipelines can detect failures automatically.

Intermediate25 minutes8 steps
Required
Lesson 103 complete: deploy_check.py with argparse, positional environment argument, --format flag, and --verbose flag
1

201 β€” Reading and Writing Files with pathlib

Use Python's stdlib pathlib module to read, write, append, rename, delete, and search files on disk β€” then combine those primitives into a DevOps script that reads a deploy config file and writes a timestamped run log.

Beginner25 minutes8 steps
Required
Lesson 104 complete: virtual environment active at ~/devops-python/lesson-101/devops-env
2

202 β€” Working with the OS and Environment Variables

Use Python's stdlib `os` module to read and write environment variables, inspect the running OS, and build portable file paths β€” then extend `deploy_check.py` to read DEPLOY_ENV, APP_VERSION, and a configurable base path from the environment so behaviour changes without touching argparse.

Intermediate25 minutes8 steps
Required
Lesson 201 complete: deploy_log.py in ~/devops-python/lesson-201; virtual environment active at ~/devops-python/lesson-101/devops-env
3

203 β€” Running Shell Commands from Python

Use Python's stdlib `subprocess.run()` to shell out to system commands from your scripts β€” capturing stdout and stderr, checking exit codes, raising errors on failure with `check=True`, and passing custom environments. Then extend `env_deploy_check.py` with a live system health check that shells out to `git` or `df`.

Intermediate25 minutes8 steps
Required
Lesson 202 complete: env_deploy_check.py in ~/devops-python/lesson-202; virtual environment active at ~/devops-python/lesson-101/devops-env
4

204 β€” Practical File Automation

Combine `pathlib`, `os`, and `subprocess` to build a log-rotation script that globs for old log files, compresses them into a dated archive with `tar`, and deletes the originals. You will add a `--dry-run` flag, read config from environment variables, and wire everything together with `argparse` β€” the same pattern used in real DevOps tooling.

Intermediate30 minutes8 steps
Required
Lesson 201 complete: pathlib file operationsLesson 202 complete: os and environment variables; env_deploy_check.py in ~/devops-python/lesson-202Lesson 203 complete: subprocess.run(); env_deploy_check.py extended with health checks in ~/devops-python/lesson-203Virtual environment active at ~/devops-python/lesson-101/devops-env
1

301 β€” Reading and Writing JSON Config Files

Use Python's built-in `json` module to parse config files, serialize Python objects to JSON, handle malformed input safely, and build a reusable config loader β€” the same pattern used by deployment scripts that read environment config from JSON files.

Beginner25 minutes8 steps
Required
Lesson 201 complete: pathlib file operationsLesson 202 complete: os and environment variablesVirtual environment active at ~/devops-python/lesson-101/devops-env
2

302 β€” Managing YAML Configuration Files

Install PyYAML and use yaml.safe_load() and yaml.dump() to read, write, and update YAML config files β€” the format used by Docker Compose, Kubernetes manifests, and GitHub Actions workflows. Learn why yaml.safe_load() is always required and why bare yaml.load() is a security risk you must avoid.

Intermediate25 minutes8 steps
Required
Lesson 301 complete: json config filesVirtual environment active at ~/devops-python/lesson-101/devops-envPyYAML not yet installed (installed in this lesson)
3

303 β€” Loading .env Files and Environment Variables

Install python-dotenv and use load_dotenv() to load .env files into the process environment. Understand env var precedence, handle missing .env files safely, and learn why .env files must never be committed to source control β€” with a .gitignore pattern and a .env.example template to enforce that boundary.

Beginner20 minutes8 steps
Required
Lesson 302 complete: yaml config filesVirtual environment active at ~/devops-python/lesson-101/devops-envpython-dotenv not yet installed (installed in this lesson)
4

304 β€” Config Management Patterns

Build a layered config loader that merges settings from four sources β€” hardcoded defaults, a YAML or JSON config file, a .env secrets file, and runtime environment variable overrides β€” with explicit precedence at every layer: env vars win over .env, .env wins over the config file, and the config file wins over defaults. This pattern is used in virtually every production DevOps automation tool.

Intermediate25 minutes8 steps
Required
Lesson 303 complete: python-dotenv>=1 installed; env_loader.py built at ~/devops-python/lesson-303/Virtual environment active at ~/devops-python/lesson-101/devops-envPyYAML>=6 installed from lesson 302python-dotenv>=1 installed from lesson 303
1

401 β€” Making HTTP Requests with requests

Install the requests library and use it to send GET and POST calls to real public APIs, inspect response objects, handle HTTP errors with raise_for_status(), and catch network failures with try/except. Every request in this lesson includes an explicit timeout β€” the single most important habit for production HTTP scripts. The lesson closes by importing load_config() from lesson 304's layered_config.py to show how api_base_url flows from the config layer directly into requests calls.

Beginner25 minutes8 steps
Required
Lesson 304 complete: layered_config.py built at ~/devops-python/lesson-304/Virtual environment active at ~/devops-python/lesson-101/devops-envpython-dotenv>=1 and PyYAML>=6 installed from Module 3
2

402 β€” Authenticating with REST APIs

Replace the placeholder token from lesson 401 with a real GitHub Personal Access Token stored as GITHUB_TOKEN and learn every authentication pattern used in production REST API work: Bearer token headers, API key headers, and query-string tokens. Credentials are loaded from .env via load_dotenv() and os.environ.get() β€” never hardcoded. Operational config (base URL, timeout) stays in config.yaml via load_config(). The lesson closes with a reusable requests.Session for authenticated sessions and explicit 401/403 error handling.

Intermediate25 minutes8 steps
Required
Lesson 401 complete: requests installed, safe_get() pattern understood, api_client.py runs correctlyLesson 304 complete: layered_config.py built at ~/devops-python/lesson-304/A free GitHub account at github.com β€” required to create a Personal Access TokenVirtual environment active at ~/devops-python/lesson-101/devops-env
3

403 β€” Consuming JSON REST APIs

Go beyond single API calls: parse JSON responses into Python data structures, navigate nested and optional fields safely, paginate through multi-page GitHub API results using the Link header, and implement explicit exponential backoff for rate-limited endpoints. The lesson closes with a reusable json_api_client.py that imports check_credentials() from lesson 402 and composes all these patterns into a single, production-ready function.

Intermediate30 minutes8 steps
Required
Lesson 402 complete: authenticated_github_client.py runs, check_credentials() in credential_checklist.py, .env with real GitHub PATLesson 304 complete: layered_config.py built at ~/devops-python/lesson-304/Virtual environment active at ~/devops-python/lesson-101/devops-env
4

404 β€” Receiving Webhooks in Python

Close the API loop by receiving inbound HTTP payloads from external services β€” without a web framework. Build a webhook server using Python's stdlib http.server, read POST bodies from the raw socket stream, validate GitHub-style HMAC-SHA256 signatures with hmac.compare_digest, and respond with correct HTTP status codes (200, 400, 401, 411). The lesson closes with webhook_receiver.py: a self-contained server that loads the shared secret from .env, routes events by type, and emits structured log output β€” all patterns used daily in DevOps event pipelines.

Intermediate35 minutes8 steps
Required
Lesson 403 complete: json_api_client.py built at ~/devops-python/lesson-403/Lesson 303 complete: python-dotenv installed in devops-envVirtual environment active at ~/devops-python/lesson-101/devops-env