Devops Automation

Master Python automation for infrastructure management, deployment pipelines, monitoring, backups, and production system orchestration

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn

Why Python for DevOps Automation?

Python has become the de facto standard for DevOps automation, replacing shell scripts with safer, more maintainable solutions.

Common Use Cases

File & Directory Automation

Every DevOps workflow involves managing files: rotating logs, cleaning temporary data, synchronizing directories, and organizing backups.

Common Tasks

A production CI server runs a cleanup script every hour to remove build artifacts older than 7 days, preventing disk space exhaustion. This same pattern applies to log management, cache cleanup, and temporary file handling.

System Command Execution

The subprocess module provides safe, controlled execution of system commands with proper error handling and timeout management.

Best Practices

Common Operations

Task Scheduling

Modern DevOps requires more intelligent scheduling than traditional cron. Python provides flexible alternatives.

Scheduling Options

More powerful: retry on failure, parallel execution, event-based triggers, state management

Distributed task queue with advanced scheduling capabilities

Typical Scheduled Tasks

Server Health Monitoring

Proactive monitoring prevents outages. Python can track system resources and alert teams before problems escalate.

Metrics to Monitor

The psutil Library

psutil is the standard for cross-platform system monitoring in Python:

Provides CPU, memory, disk, network, and process information on Linux, macOS, and Windows.

API & Webhook Automation

Modern infrastructure is API-driven. Python integrates seamlessly with CI/CD systems, monitoring tools, and cloud platforms.

Common Integrations

Automation Patterns

High CPU → send Slack alert → scale infrastructure

Service down → restart automatically → notify team

Docker Automation

The Docker Python SDK enables comprehensive container lifecycle management from within Python scripts.

Installation

Automation Tasks

A maintenance script runs nightly to clean up stopped containers and dangling images, preventing disk space issues. It also restarts any containers marked as unhealthy by Docker's health checks.

Kubernetes Automation

The Kubernetes Python client allows programmatic cluster management, enabling GitOps-style automation.

Automation Capabilities

Advanced Patterns

• Blue/green deployments - maintain two production environments

• Canary releases - gradually roll out changes to a subset of users

• Automatic rollback - revert on health check failure

• Multi-cluster management - orchestrate across regions

Backup Automation & Data Rotation

Regular, automated backups are essential for disaster recovery. Python orchestrates the entire backup lifecycle.

Backup Strategy

3 copies of data • 2 different media types • 1 offsite copy

Python scripts can implement this automatically: local disk, network storage, cloud backup.

Log Processing & Automated Alerts

Logs contain critical information about system health, security events, and errors. Automated analysis prevents issues from going unnoticed.

Log Analysis Tasks

Alert Triggers

• Error threshold - Alert when error rate exceeds 1%

• Security events - Failed login attempts, suspicious patterns

• Performance degradation - Response time above threshold

• Service crashes - Application or container restarts

Zero-Downtime Deployment

Production deployments must minimize or eliminate downtime. Python orchestrates sophisticated deployment strategies.

Deployment Pipeline

Safety Mechanisms

Practice quiz

Which module is the safe, recommended way to run system commands from Python?

  • os.system
  • commands
  • subprocess
  • shlex

Answer: subprocess. subprocess.run gives controlled execution with output capture, timeouts, and return-code checking.

What is a best practice when calling subprocess for security?

  • Pass the command as a list and avoid shell=True to prevent injection
  • Always use shell=True
  • Concatenate user input into a string
  • Disable timeouts

Answer: Pass the command as a list and avoid shell=True to prevent injection. Passing a list like ['ls', '-la'] and avoiding shell=True prevents shell-injection attacks.

Why set a timeout on subprocess calls?

  • To speed up the command
  • To capture stderr
  • It is required syntax
  • To prevent the script hanging on an unresponsive command

Answer: To prevent the script hanging on an unresponsive command. A timeout stops the script from hanging indefinitely if a command never returns.

Which library is the standard for cross-platform system metrics (CPU, memory, disk)?

  • os
  • psutil
  • sys
  • platform

Answer: psutil. psutil provides CPU, memory, disk, network, and process info across Linux, macOS, and Windows.

Which class in the lesson uses the Docker SDK to clean up stopped containers and restart unhealthy ones?

  • DockerAutomation
  • DockerManager
  • ContainerBot
  • DockerClient

Answer: DockerAutomation. The DockerAutomation class wraps docker.from_env() to remove stopped containers, prune images, and restart unhealthy ones.

In the deployment workflow, what happens if the Kubernetes rollout fails its health check?

  • The script ignores it
  • It deletes the deployment
  • It automatically rolls back with kubectl rollout undo and alerts the team
  • It retries forever

Answer: It automatically rolls back with kubectl rollout undo and alerts the team. On a failed rollout the script runs kubectl rollout undo to revert and sends an alert about the failure.

What is the recommended place to store secrets like API keys in automation scripts?

  • Hard-coded in the script
  • Environment variables or a secret manager
  • In the log files
  • In the Git history

Answer: Environment variables or a secret manager. Secrets belong in environment variables or secret managers, never hard-coded in code.

What does the '3-2-1 backup rule' mean?

  • 3 servers, 2 regions, 1 admin
  • 3 daily backups kept for 21 days
  • 3 scripts, 2 schedules, 1 alert
  • 3 copies of data, 2 different media types, 1 offsite copy

Answer: 3 copies of data, 2 different media types, 1 offsite copy. The 3-2-1 rule keeps 3 copies of data on 2 media types with 1 stored offsite.

Why does the lesson favor Python over bash scripts for DevOps automation?

  • Python runs only on Linux
  • Better error handling, cross-platform support, and clean native API integration
  • Bash cannot run commands
  • Python is always faster

Answer: Better error handling, cross-platform support, and clean native API integration. Python offers try/except error handling, works across platforms, and integrates with APIs via libraries like requests and boto3.

What does it mean for an automation script to be 'idempotent'?

  • It runs only once ever
  • It requires root access
  • It can run multiple times safely without causing harm
  • It never writes to disk

Answer: It can run multiple times safely without causing harm. An idempotent script produces the same safe result whether run once or many times, a key property of reliable automation.