Devops Automation Example
Here are production-tested, real-world DevOps automation examples in Python and Bash from reputable DevOps resources.
Python DevOps Automation Scripts
1. System Resource Monitoring
Monitor CPU and memory usage, sending alerts if thresholds are exceeded:
import psutil
def check_system_resources():
cpu_usage = psutil.cpu_percent(interval=1)
memory_usage = psutil.virtual_memory().percent
if cpu_usage > 80:
print(f"High CPU usage: {cpu_usage}%")
if memory_usage > 80:
print(f"High Memory usage: {memory_usage}%")
check_system_resources()
This type of monitoring is fundamental for production reliability.[1][2]
2. AWS Automation (List S3 Buckets)
Automate AWS tasks like listing all S3 buckets with boto3:
import boto3
s3 = boto3.client('s3')
response = s3.list_buckets()
for bucket in response['Buckets']:
print(f"Bucket: {bucket['Name']} (Created on {bucket['CreationDate']})")
Used widely in cloud provisioning and auditing.[3][4][5]
3. Service Uptime Monitoring
Check the status of an external service endpoint:
import requests
try:
response = requests.get("http://api.your-service.com")
if response.status_code != 200:
print("Service Down Alert!")
except Exception:
print("Service Unreachable!")
Useful for API and endpoint monitoring in production.[2]
Bash DevOps Automation Scripts
1. Automated Backup to Remote Server
Automate backup of a directory using rsync:
#!/bin/bash
SOURCE_DIR="/var/www/html/"
BACKUP_DIR="/mnt/backup"
rsync -avz --delete $SOURCE_DIR $BACKUP_DIR
echo "Backup completed on $(date)"
Ensures critical data is regularly secured.[6][2]
2. Disk Space Monitoring Script
Alert if disk usage exceeds a threshold:
#!/bin/bash
THRESHOLD=85
df -h | awk -v threshold=$THRESHOLD '{if($5+0 > threshold) print $0}'
Prevents outages by highlighting storage issues.[7][2]
3. Log Rotation Automation
Archive and clear old logs to prevent disk bloat:
#!/bin/bash
LOG_DIR="/var/log/app/"
tar -czf $LOG_DIR/archive_$(date +%F).tar.gz $LOG_DIR/*.log
rm $LOG_DIR/*.log
echo "Logs archived and cleared."
Essential for long-running applications that generate many logs.[2]
These scripts are used in many production environments to automate system monitoring, backups, cloud interaction, and logging. For more extensive libraries and real-world Bash toolkits, see repositories like “DevOps-Bash-tools” on GitHub. You can modify these scripts as needed to fit your specific production requirements.[8]
[1] https://blog.devops.dev/22-python-scripts-every-devops-engineer-should-automate-0df7177675cd
[4] https://dev.to/prodevopsguytech/scripting-in-devops-a-complete-guide-from-beginner-to-advanced-noa
[5] https://www.activestate.com/blog/top-10-python-packages-for-devops/
[7] https://faun.pub/top-5-bash-scripts-for-devops-b6ce4c6060ae
[8] https://github.com/HariSekhon/DevOps-Bash-tools
[9] https://www.youtube.com/watch?v=PiRNGGSCaIs
[10] https://github.com/AbeTavarez/Python_DevOps_Scripts
[11] https://blog.devops.dev/3-python-projects-to-kickstart-python-learning-d069f95fef39
[12] https://www.reddit.com/r/devops/comments/14zhmvp/what_are_some_cool_things_youve_automated_with/
[13] https://www.reddit.com/r/bash/comments/1av0gp6/50_useful_scripts_you_use_as_a_devops/
[14] https://www.youtube.com/watch?v=aK-lkJTfUEc