🐍 Why Python for DevOps?
›Python is the glue that holds the DevOps world together
Every major DevOps tool — AWS, Kubernetes, Jenkins, GitHub, Terraform — exposes either a Python SDK or a REST API. Shell scripts are great for simple tasks but break down fast when you need to parse JSON, handle errors gracefully, retry on failure, or make HTTP calls. Python handles all of this cleanly.
What Python actually does in DevOps
| Task | Without Python | With Python |
|---|---|---|
| Check deployment health | Manual kubectl + grep in Bash | Python polls K8s API, retries, alerts Slack on fail |
| Upload build artifact | AWS CLI in shell script | boto3 with retry, versioning, metadata |
| Trigger downstream jobs | Jenkins UI click | Python calls Jenkins REST API from pipeline |
| Parse Terraform output | jq in shell (complex) | json.loads() in 2 lines |
| Post-deploy smoke test | curl in loop | Python with retry, timeout, proper error handling |
| Scale down dev at night | Manual | Cron calls Python → K8s SDK → scales to 0 |
⚙️ subprocess — Run Shell Commands
›The most used Python module in DevOps
subprocess.run() lets you run any shell command and capture the output. This is how Python scripts wrap kubectl, helm, terraform, docker — any CLI tool you use.
The pattern you use every time
result = subprocess.run(["cmd", "arg1", "arg2"], capture_output=True, text=True, timeout=30)
if result.returncode != 0:
print(result.stderr) # Show what went wrong
sys.exit(1)
print(result.stdout) # Use the output
🌐 HTTP APIs — Talking to DevOps Tools
›Every DevOps tool has a REST API
Jenkins, GitHub, Kubernetes, PagerDuty, Slack — they all expose HTTP endpoints. Python's requests library lets you call them in a few lines. This is how you build integrations between tools that have no native connection.
📄 YAML & JSON — The Infrastructure Language
›All infrastructure is defined in YAML or JSON
Kubernetes manifests, Helm values, Ansible playbooks, GitHub Actions workflows — everything is YAML. Python reads and writes YAML in a few lines. This is how you build dynamic config generation, environment promotion scripts, and manifest patching tools.
☁️ Cloud SDKs — boto3 & Azure
›AWS boto3 — the most important DevOps library
boto3 lets Python control every AWS service. EC2, S3, EKS, CloudWatch, SSM — all available as Python objects. Authentication uses the IAM Role attached to the EC2 instance or K8s pod automatically. Never hardcode AWS credentials in Python code.
☸️ Kubernetes SDK — Manage K8s from Python
›The kubernetes Python library is a full K8s API client
Everything you can do with kubectl, you can do with the Python SDK — and more. Read pod status, wait for deployments, scale replicas, read ConfigMaps, find crashing pods. This is used in CI/CD post-deploy verification scripts and cost-saving automation.
🏗️ Production Script Patterns
›Templates every DevOps engineer should memorise
Every production automation script needs the same structure: proper logging (not print), read config from environment variables (not hardcoded), retry logic for flaky operations, and notification on failure. This template is your starting point for every script you write.