The most detailed free FDE + DevOps library: 140+ lessons, 70+ labs and 80 long-form articles, in English and Turkish. Start learning →

DevOps Foundations · Module 3: Safe automation with Bash and Python · Lab

Write a Setup Script Harmless on Second Run

45 min hands-on · Core

Local Linux shell; all state under /tmp/dlab-m03-02 (fixture config, fake service flag, package list).

Two ways to do this lab: in your browser on Killercoda (free, no install), or on your own machine as a local guide. Killercoda runs one free scenario at a time: if you see a waiting queue, close other Killercoda tabs and wait a minute.

Objectives

  • Write a multi-step setup script that converges on rerun
  • Prove the second run is a no-op with state diffs
  • Add a --check mode that reports without changing anything
  1. Step 1

    Write the naive version first

    Write a setup script that appends three config lines, creates two directories and writes a status file. Run it twice and diff the state after each run; the second diff must currently show duplication.

  2. Step 2

    Make every step idempotent

    Convert each step to check-then-change against real state (grep -qxF for lines, test -d for directories, cmp for file contents). Re-run twice; both diffs after the first must be empty.

  3. Step 3

    Add the dry-run contract

    Add --check support that prints exactly what would change and exits nonzero when changes exist, zero when converged. Demonstrate all three states: would-change, converged, and real run.

How to confirm it worked

  • The naive double-run duplication is shown before the repair
  • After the repair, the second-run state diff is empty
  • No marker files: checks read real state
  • --check reports correctly in would-change and converged states