Python for DevOps Automation
☁️ Cloud Automation · 🔄 CI/CD · 🏗️ Infrastructure as Code · 🐳 Containers
Level: DevOps Engineer Focus |
Goal: Cloud Automation, CI/CD, Infrastructure as Code
📚 Concepts Covered
What is Python?
Python is a simple, readable programming language created in 1991. It reads almost like English, making it the fastest language to learn and the #1 choice for DevOps automation.
📖
Readable
Code reads like plain English — easy to write and maintain
☁️
Cloud-Ready
Best SDKs for AWS, Azure, GCP built in Python
⚡
Fast to Script
Write automation scripts in minutes, not hours
🔑 Remember It Simply:
- Python = English-like code — No complex { } or ; like Java/C++
- Python = Glue language — Connects AWS, Docker, Kubernetes, Terraform together
- Python = #1 DevOps language — Ansible, Boto3, Fabric — all Python
Why Python for DevOps? ⚙️
| DevOps Task | Python Tool | What It Does |
|---|---|---|
| Cloud (AWS) | boto3 |
Manage EC2, S3, Lambda, RDS with Python |
| Config Management | ansible |
Automate server setup and configuration |
| Containers | docker SDK |
Build, run, and manage Docker containers |
| Kubernetes | kubernetes |
Deploy and scale pods via Python |
| Terraform | subprocess |
Run terraform commands from Python scripts |
| CI/CD | requests / subprocess |
Trigger pipelines, call GitHub/Jenkins APIs |
| Jenkins CI/CD | requests / jenkinsapi |
Trigger builds, check job status, parse results |
| Ansible Automation | ansible-runner |
Run playbooks, manage inventory from Python |
| Monitoring | prometheus_client |
Expose metrics, parse logs, send alerts |
Key DevOps Libraries 📦
☁️ Cloud
boto3— AWS SDK (EC2, S3, Lambda)azure-sdk— Azure resourcesgoogle-cloud— GCP resources
🐳 Containers & Orchestration
docker— Docker SDK for Pythonkubernetes— K8s Python client
🔧 Infrastructure & Automation
subprocess— Run shell/terraform commandsparamiko— SSH into serversfabric— Remote command executionansible-runner— Run Ansible playbooks from Python
🏗️ CI/CD — Jenkins
requests— Call Jenkins REST API directlyjenkinsapi— High-level Jenkins Python clientpython-jenkins— Manage jobs, builds, nodes
📊 Monitoring & APIs
requests— Call REST APIs (Slack, PagerDuty)prometheus_client— Expose metricslogging— Built-in structured logging
💡 Install all at once:
pip install boto3 docker kubernetes requests paramiko fabric ansible-runner python-jenkins prometheus-client
Quick Setup 🔧
1. Install Python
# Ubuntu / Debian (most DevOps servers)
sudo apt update && sudo apt install python3 python3-pip -y
python3 --version # should show 3.10+
# macOS
brew install python@3.11
# Verify
python3 -c "print('Python ready!')"
2. Install DevOps Packages
# Install essential DevOps packages pip3 install boto3 docker kubernetes requests paramiko # Save dependencies for your project pip3 freeze > requirements.txt # On another machine, restore them pip3 install -r requirements.txt
3. Common pip Commands
| Command | What it does |
|---|---|
pip install boto3 |
Install a package |
pip list |
See all installed packages |
pip install -r requirements.txt |
Install from file |
pip uninstall boto3 |
Remove a package |
DevOps Code Examples 💻
1. AWS EC2 — List Running Instances
import boto3
ec2 = boto3.client('ec2', region_name='us-east-1')
response = ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
for reservation in response['Reservations']:
for instance in reservation['Instances']:
print(f"ID: {instance['InstanceId']} Type: {instance['InstanceType']}")
2. Docker — List Running Containers
import docker
client = docker.from_env()
for container in client.containers.list():
print(f"Name: {container.name} Status: {container.status}")
3. Terraform — Run Apply from Python
import subprocess
result = subprocess.run(
['terraform', 'apply', '-auto-approve'],
capture_output=True, text=True
)
if result.returncode == 0:
print("✅ Terraform applied successfully")
else:
print(f"❌ Error: {result.stderr}")
4. Kubernetes — Scale a Deployment
from kubernetes import client, config
config.load_kube_config() # reads ~/.kube/config
apps_v1 = client.AppsV1Api()
apps_v1.patch_namespaced_deployment_scale(
name='my-app',
namespace='production',
body={'spec': {'replicas': 5}}
)
print("✅ Scaled to 5 replicas")
5. Slack Alert — Send a Notification
import requests
webhook_url = "https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
requests.post(webhook_url, json={
"text": "✅ Deployment to production succeeded!"
})
6. SSH — Run Command on Remote Server
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.100', username='ubuntu', key_filename='~/.ssh/id_rsa')
stdin, stdout, stderr = ssh.exec_command('df -h')
print(stdout.read().decode())
ssh.close()
7. Jenkins — Trigger a Build via Python
What is Jenkins? Jenkins is a CI/CD server. Python can talk to it using its REST API to trigger
builds, check status, and get logs — without clicking the UI.
import requests
from requests.auth import HTTPBasicAuth
JENKINS_URL = "http://jenkins.mycompany.com"
JOB_NAME = "deploy-production"
USER = "admin"
API_TOKEN = "your-api-token" # from Jenkins → User → Configure
# Trigger a build
response = requests.post(
f"{JENKINS_URL}/job/{JOB_NAME}/build",
auth=HTTPBasicAuth(USER, API_TOKEN)
)
if response.status_code == 201:
print("✅ Jenkins build triggered!")
else:
print(f"❌ Failed: {response.status_code}")
8. Jenkins — Check Build Status
import requests
from requests.auth import HTTPBasicAuth
JENKINS_URL = "http://jenkins.mycompany.com"
JOB_NAME = "deploy-production"
BUILD_NUM = "lastBuild"
response = requests.get(
f"{JENKINS_URL}/job/{JOB_NAME}/{BUILD_NUM}/api/json",
auth=HTTPBasicAuth("admin", "your-api-token")
).json()
print(f"Job: {response['fullDisplayName']}")
print(f"Result: {response['result']}")
print(f"URL: {response['url']}")
9. Ansible — Run a Playbook from Python
What is Ansible? Ansible automates server configuration using YAML playbooks. Python's
ansible-runner library lets you trigger and control those playbooks from your Python scripts.
import ansible_runner
# Run a playbook
result = ansible_runner.run(
playbook='deploy.yml',
inventory='hosts.ini',
private_data_dir='/opt/ansible'
)
if result.rc == 0:
print("✅ Ansible playbook succeeded")
else:
print(f"❌ Playbook failed — status: {result.status}")
10. Ansible — Dynamic Inventory with Python
import boto3
import json
# Build Ansible inventory from AWS EC2 automatically
ec2 = boto3.client('ec2', region_name='us-east-1')
response = ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
inventory = {'all': {'hosts': []}}
for r in response['Reservations']:
for i in r['Instances']:
inventory['all']['hosts'].append(i['PublicIpAddress'])
# Print as JSON — Ansible reads this as a dynamic inventory
print(json.dumps(inventory, indent=2))
# Run: ansible-playbook -i script.py deploy.yml
Simple Way to Remember 🧠
📝 Syntax Rules (3 to remember)
- Indentation = code blocks — use 4 spaces, no { }
- No semicolons — just press Enter
- import first — always import library before using it
import boto3 # 1. import
ec2 = boto3.client() # 2. use it
if status == 'ok': # 3. indent block
print('ok') # 4 spaces indent
🔑 DevOps Data Types (4 to remember)
- List —
servers = ['web1', 'web2'] - Dict —
config = {'env': 'prod'} - String —
region = 'us-east-1' - Boolean —
healthy = True
🔄 Control Flow (3 patterns)
# Loop over servers
for server in servers:
deploy(server)
# Check condition
if status == 'running':
restart(server)
# Function
def deploy(server):
print(f"Deploying {server}")
🧠 Memory Shortcut — "PILAF"
| P | Packages — always import first |
| I | Indentation — 4 spaces = 1 block level |
| L | Loops — for x in list to repeat tasks |
| A | Actions — functions def deploy(): |
| F | Flow — if/else to handle success/failure |
Quick Knowledge Review 📝
Q1: What Python library do you use to manage AWS resources?
💡 boto3 — the official AWS SDK for Python.
import boto3 then call
boto3.client('ec2'), boto3.client('s3') etc.
Q2: How do you run a shell command (like terraform apply) from Python?
💡 Use the built-in subprocess module:
subprocess.run(['terraform', 'apply', '-auto-approve'], capture_output=True)
Q3: What does indentation mean in Python?
💡 In Python, 4 spaces = 1 code block. There are no { } braces. Every line inside a loop,
if-statement, or function must be indented by 4 spaces.
Q4: Which library do you use to control Docker containers from Python?
💡 The docker SDK:
import docker → client = docker.from_env() →
client.containers.list()
Q5: How do you SSH into a server and run a command using Python?
💡 Use paramiko:
ssh.connect('server-ip', username='ubuntu', key_filename='~/.ssh/id_rsa') then
ssh.exec_command('your-command')
Q6: What does PILAF stand for in Python DevOps?
💡 Packages (import) · Indentation (4 spaces) · Loops (for) · Actions
(def) · Flow (if/else)
Q7: How does Python talk to Jenkins to trigger a build?
💡 Use the requests library to call Jenkins REST API with HTTP Basic Auth (username + API
token).
A 201 response = build triggered. You can also check status at
requests.post(f"{JENKINS_URL}/job/{JOB_NAME}/build", auth=HTTPBasicAuth(user, token))A 201 response = build triggered. You can also check status at
/job/<name>/lastBuild/api/json.
Q8: What is Ansible and how do you run a playbook from Python?
💡 Ansible automates server setup/config using YAML playbooks. From Python, use
ansible-runner:
Check
ansible_runner.run(playbook='deploy.yml', inventory='hosts.ini', private_data_dir='/opt/ansible')Check
result.rc == 0 for success. You can also build a dynamic inventory using
boto3 (pull live EC2 IPs) and pass it to Ansible automatically.
0 Comments