Interview Prep/Python for DevOps/OS, filesystem, subprocess & CLIs

How do you implement a proper `--dry-run`?

IntermediateConceptOS, filesystem, subprocess & CLIs
Answer

Compute the full plan first, print it, and only execute when not in dry-run mode, so dry-run exercises the same code path up to the side effect.

plan = [s for s in snapshots if is_expired(s)]
for s in plan:
    log.info("%s delete %s", "[dry-run] would" if dry_run else "will", s["SnapshotId"])
if not dry_run:
    for s in plan:
        ec2.delete_snapshot(SnapshotId=s["SnapshotId"])

Many AWS APIs also accept DryRun=True, which validates IAM permissions server-side and returns DryRunOperation on success; that is a stronger test than a local flag.

All Python for DevOps questions Roadmap: Python for automation

More on OS, filesystem, subprocess & CLIs