Mastering Python for DevOps Automation with pip, os, and subprocess

Day 3 of #30DaysOfDevOps — Post 2 Knowing Python syntax is one thing. Using Python to automate real infrastructure tasks is another. Here's where it gets practical. 1. Managing Packages with pip pip is Python's package manager. It connects you to thousands of libraries built for every DevOps task imaginable. Install a library: pip install boto3 Install a specific version: pip install boto3==1.26.0 Save all dependencies to a file: pip freeze > requirements.txt Restore them on another machine: pip install -r requirements.txt Always pin versions in requirements.txt for reproducible environments. 2. Working with the os Module The os module lets your Python scripts interact directly with the operating system. import os # Create a directory if it doesn't exist if not os.path.exists("/var/app/releases"):   os.makedirs("/var/app/releases") # List all files in a directory for f in os.listdir("/etc/nginx/conf.d"):   print(f) # Get an environment variable db_url = os.getenv("DATABASE_URL", "localhost:5432") 3. Automation Scripting This is where Python earns its place in DevOps. A few lines can replace hours of manual work. Rename all .log files to .log.bak for archiving: import os for filename in os.listdir("/var/log/app"):   if filename.endswith(".log"):     os.rename(       f"/var/log/app/{filename}",       f"/var/log/app/{filename}.bak"     )     print(f"Archived: {filename}") 4. Running System Commands with subprocess Need to run shell commands from Python? Use subprocess. import subprocess result = subprocess.run(   ["systemctl", "status", "nginx"],   capture_output=True,   text=True ) print(result.stdout) This lets you build scripts that check service health, restart processes, or run deployments. 5. Scheduling Scripts Once your script is ready, automate it with cron. Run a cleanup script every day at midnight: 0 0 * * * /usr/bin/python3 /opt/scripts/cleanup.py Run a health check every 5 minutes: */5 * * * * /usr/bin/python3 /opt/scripts/healthcheck.py 6. Challenges for Today 1. Write a script that generates a random 16-character password using the secrets module. 2. Use subprocess to check if nginx is running and print its status. 3. Write a script that monitors CPU and memory usage every 5 seconds using psutil. 4. Use the requests library to fetch data from a public API and print the response. 5. Automate bulk file renaming in a directory based on a pattern. 6. Write a script that creates a Linux user and verifies the creation. Drop your scripts in the comments — let's see what you build. #DevOps #Python #Automation #Scripting #30DaysOfDevOps #LearningInPublic #DevOpsEngineer

To view or add a comment, sign in

Explore content categories