Python Scripting and Linux for DevOps

Automate Linux servers with Python: scripts that manage files and run commands safely, command-line tools built with argparse and click, packages installed by script, and jobs scheduled with cron and systemd.

Beginner · Cloud & DevOps · about 16 hours · 5 modules · certificate on completion

Taught by Carlòs Ramirèz, Ismaìlè Rahimovì, AI Learning Companions.

About this course

DevOps work is full of small jobs that come round again and again: tidy the logs, check the disks, install the same packages on every new server, find out what went wrong at 09:14. This course teaches you to automate them with Python on Linux, building on the shell skills from Linux Command Line Essentials. You'll write scripts that manage files and configuration safely and run commands with subprocess; turn them into proper command-line tools with argparse and click, with help, validation and tests; investigate a disk-space incident with grep, awk, sed, find and the monitoring tools, adding your own Python filters to the pipeline; script package installs with apt and dnf so they can be run again safely; and leave your automation running with cron and systemd timers, configured by environment variables and recorded with logging.

Every module ends with a hands-on lab on files that come with the course, with the exact output a correct solution prints, and the course ends with devsetup, a click tool you build to set up a developer machine from a profile: packages, folders and configuration files, safely and repeatably.

What you'll learn

  • Write Python scripts that manage files, update configuration safely and run Linux commands with subprocess, failing with clear messages and exit statuses
  • Build command-line tools with argparse and click, with subcommands, help text, validated input and tests
  • Investigate a server with grep, awk, sed, find and the monitoring tools, and write Python filters that work in pipelines
  • Manage packages with apt and dnf, and automate installs so a script can be run again safely
  • Schedule and supervise Python automation with cron and systemd timers, configured by environment variables and recorded with logging

Skills you'll gain

Python scripting, Linux system administration, Command-line tools (argparse, click), subprocess and shell integration, Log analysis with grep, awk and sed, Package management (apt, dnf), Idempotent provisioning, cron and systemd timers, Logging

Tools

Python 3, Bash, click, apt, dnf, systemd, cron

Before you start

Linux Command Line Essentials, or the same comfort in a Linux shell (files, permissions, pipes and simple scripts), and basic Python: variables, functions, loops, lists and dictionaries, as in Python Programming with PyCharm. You need a Linux machine where you can use sudo, such as Ubuntu 24.04 on WSL, in a virtual machine or on a cloud server, with Python 3.11 or later and python3-venv. Docker is optional, for trying installs in a throwaway container.

Modules

  1. Python Scripting for SysAdmins

    Use Python for the jobs that outgrow a shell script: files and directories with pathlib and shutil, configuration files read and written safely, Linux commands run with subprocess, and errors reported with clear messages and exit statuses.

    • From shell scripts to Python scripts (10 min)
    • Files and directories with pathlib, os and shutil (12 min)
    • Reading and updating configuration files (12 min)
    • Running commands with subprocess, and handling errors (14 min)
    • Hands-on lab: Build a log housekeeping script
  2. Building Robust CLI Tools

    Turn scripts into tools other people can rely on: arguments, options and subcommands with argparse and click, help that explains itself, validation that rejects bad input before anything changes, tests with CliRunner, and installing the tool as a command.

    • Command-line interfaces with argparse (13 min)
    • Building tools with click (13 min)
    • Help, validation, tests and installing your tool (12 min)
    • Hands-on lab: Build logtool, a log-search command with click
  3. Core Linux Utilities

    The utilities DevOps engineers reach for every day, used for operations work: searching and reshaping logs with grep, awk and sed, finding files with find, xargs and locate, reading a system's health with top, htop and friends, and Python filters that fit into pipelines. It builds on the basics in Linux Command Line Essentials.

    • grep, awk and sed for operations work (13 min)
    • Finding files: find, xargs and locate (11 min)
    • Watching the system: top, htop and the monitoring toolkit (12 min)
    • Pipes, redirection and Python in the pipeline (12 min)
    • Hands-on lab: Investigate a disk-space alert
  4. Package Management in DevOps

    How Linux package managers install software, resolve dependencies and decide which repositories to trust; apt and dnf side by side; and provisioning by script, so every fresh server gets the right packages the same way. It goes beyond the apt basics in Linux Command Line Essentials.

    • Packages, dependencies and repositories (12 min)
    • apt and dnf side by side (12 min)
    • Automating installs: idempotent provisioning (12 min)
    • Hands-on lab: Write an idempotent package provisioner
  5. Integrating Python and Linux

    Bring Python and Linux together into automation you can leave running: wrappers around system commands, configuration from environment variables, logging an operator can rely on, and schedules with cron and systemd timers.

    • Wrapping Linux commands in Python (12 min)
    • Environment variables and configuration (11 min)
    • Logging for automation scripts (12 min)
    • Scheduling with cron and systemd timers (14 min)
    • Hands-on lab: Schedule a health check with cron and systemd

Project: devsetup: automate a developer machine with click

Your team spends half a day setting up each new developer's Linux machine by following a wiki page. Replace the page with devsetup, a click command that reads a profile (profile.toml) and makes the machine match it: it installs the listed packages with apt-get, creates the directory structure under a base folder, and writes starter configuration files such as ~/.editorconfig. devsetup plan shows what would change without changing anything; devsetup apply makes the changes and is safe to run any number of times (a second run reports nothing to do, and an existing file that differs is replaced only with --force, keeping a .bak copy); devsetup check exits with 0 when the machine matches the profile and 1 when it doesn't. It detects the distribution from /etc/os-release and refuses one it doesn't support, logs what it does to the screen and to a file under ~/.local/state/devsetup/, uses exit statuses consistently, and installs as a command with pip or pipx. The starter files give you a profile, a pyproject.toml, a first plan command and three tests to build on. Prove it on a fresh Ubuntu 24.04 machine or container: plan, apply, apply again, check.

Part of

The products this course names are trademarks of their owners, who haven't made or endorsed it.

Start the course free