DevOps Automation & Scripting

Published by Brayan K

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

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.

A scheduled job must tolerate being run again

A script that succeeds once can still fail as automation. A scheduler retries after a crash, someone launches the job twice, or a previous run leaves half its files behind. Start by describing the desired state and comparing it with the observed state. “Ensure this setting has this value” is easier to repeat than “append this setting again”.

from pathlib import Path

settings = Path("settings.txt")
settings.write_text("", encoding="utf-8")
for _ in range(2):
    with settings.open("a", encoding="utf-8") as output:
        output.write("enabled=true;")
print("append:", settings.read_text(encoding="utf-8"))

def ensure_text(target, desired):
    current = target.read_text(encoding="utf-8") if target.exists() else None
    if current == desired:
        return "unchanged"
    target.write_text(desired, encoding="utf-8")
    return "changed"

print(ensure_text(settings, "enabled=true;"))
print(ensure_text(settings, "enabled=true;"))


# ✅ Expected output:
# append: enabled=true;enabled=true;
# changed
# unchanged

The second operation is idempotent for this file: repeating it with the same desired content leaves the same state. That does not make it immune to disk errors, conflicting writers or a changed input. This first example isolates repeat behaviour; the later replacement example avoids truncating the destination before its new content is ready.

Path operations are taught in Modern File Paths with pathlib, and opening/encoding files in File Handling. Here the new work is deciding when a job may change a file and what a partial failure means.

Separate the plan from the effects

A dry run should compute and report the same plan the real run would use, without applying it. Do not bury writes in your “check” function. Here the observed and desired dictionaries are explicit snapshots; the planner returns only differences in a stable order. Deletions are deliberately outside this job's contract.

def plan_updates(observed, desired):
    return [(name, desired[name]) for name in sorted(desired)
            if observed.get(name) != desired[name]]

def apply_updates(state, changes, dry_run):
    if dry_run:
        return "preview", len(changes)
    for name, value in changes:
        state[name] = value
    return "applied", len(changes)

observed = {"mode": "safe", "workers": "1"}
desired = {"mode": "safe", "workers": "2"}
changes = plan_updates(observed, desired)
print(changes)
print(apply_updates(observed, changes, dry_run=True))
print("workers after preview:", observed["workers"])
print(apply_updates(observed, changes, dry_run=False))
print("next plan:", plan_updates(observed, desired))


# ✅ Expected output:
# [('workers', '2')]
# ('preview', 1)
# workers after preview: 1
# ('applied', 1)
# next plan: []

The planner is ordinary data transformation, so it can be tested without administrator privileges or cloud accounts. Its input snapshot can become stale between preview and apply. For a real shared system, recheck a version/hash, acquire the appropriate lock, or use a service's conditional-update API; a dry-run flag alone does not prevent another writer from changing the target.

Reject the target before touching it

A relative filename such as ../outside.txt can escape a directory. Validate the resolved target against the resolved job root before writing or deleting. String-prefix tests are insufficient: a sibling folder can start with the same letters as the approved folder.

from pathlib import Path

root = Path("job-root").resolve()
root.mkdir()

def approved_target(relative_name):
    target = (root / relative_name).resolve()
    if target == root or root not in target.parents:
        raise ValueError("target must stay inside the job directory")
    return target

approved_target("../outside.txt")

# ✅ Expected error:
# ValueError: target must stay inside the job directory

The error happens before a write. Resolving an existing symlink also checks its current destination, but this is not a security sandbox against an attacker changing links between validation and use. Run with minimal privileges and keep the job directory under trusted control. For a job that owns only three configuration files, an allow-list of those exact filenames is even easier to review.

Validate configuration equally early. Environment variables are text; a missing value is different from an explicit zero. This parser rejects a nonpositive worker count before planning any changes.

def worker_count(settings):
    raw = settings.get("WORKERS", "2")
    if not isinstance(raw, str) or not raw.isascii() or not raw.isdecimal():
        raise ValueError("WORKERS must be a positive whole number")
    count = int(raw)
    if not 1 <= count <= 8:
        raise ValueError("WORKERS must be between 1 and 8")
    return count

worker_count({"WORKERS": "0"})

# ✅ Expected error:
# ValueError: WORKERS must be between 1 and 8

Tests pass a dictionary instead of changing your actual environment. At the application boundary, pass os.environ to the same parser. Supply secrets separately and avoid printing that entire mapping; configuration diagnostics should name a bad setting without exposing its secret value.

Prepare a replacement before publishing it

Opening an existing destination in write mode truncates it immediately. If validation then fails, the old configuration is already gone. Instead, prepare a sibling temporary file, validate it, and replace the destination only after it is ready. The destination and temporary file share a filesystem here.

from pathlib import Path
from tempfile import NamedTemporaryFile

def publish_config(destination, text):
    temporary = None
    try:
        with NamedTemporaryFile(mode="w", encoding="utf-8", dir=destination.parent,
                                delete=False) as output:
            temporary = Path(output.name)
            output.write(text)
        if temporary.read_text(encoding="utf-8") not in ("mode=safe", "mode=fast"):
            raise ValueError("unsupported mode")
        temporary.replace(destination)
    finally:
        if temporary is not None:
            temporary.unlink(missing_ok=True)

config = Path("service.conf")
config.write_text("mode=safe", encoding="utf-8")
try:
    publish_config(config, "mode=unknown")
except ValueError as error:
    print("rejected:", error)
print("after rejection:", config.read_text(encoding="utf-8"))
publish_config(config, "mode=fast")
print("after publication:", config.read_text(encoding="utf-8"))


# ✅ Expected output:
# rejected: unsupported mode
# after rejection: mode=safe
# after publication: mode=fast

The temporary file closes before replacement, which also matters on Windows. Its unpredictable name is never printed. This single-file pattern does not make a batch of file replacements transactional, preserve all ownership/permission metadata, or prove durability after sudden power loss. A deployed job must define those requirements, check permissions, and test restoration before claiming it has a backup.

NamedTemporaryFile creates the temporary file rather than guessing an unused name. Its creation and cleanup options are covered in Temporary Files & Directories.

Do not call a rollback successful merely because you attempted it. If both the update and restoration fail, retain both failures in the report and leave the job failed. Actual database transactions belong to Using SQLite & ORMs (SQLAlchemy) in Python.

A scheduler needs a trustworthy result

A job must make failure visible to its caller. Catching every exception and exiting successfully tells a scheduler that broken work completed. Aggregate independent results only when continuing is safe; stop a dependent workflow before later steps consume an invalid result.

def run_jobs(jobs):
    report = []
    for name, action in jobs:
        try:
            action()
            report.append((name, "ok"))
        except OSError:
            report.append((name, "failed"))
    exit_code = 1 if any(status == "failed" for _, status in report) else 0
    return report, exit_code

def save_settings():
    return None

def save_inventory():
    raise OSError("fixture write failure")

report, exit_code = run_jobs([("settings", save_settings), ("inventory", save_inventory)])
print(report)
print("exit decision:", exit_code)


# ✅ Expected output:
# [('settings', 'ok'), ('inventory', 'failed')]
# exit decision: 1

The fixture reports a decision rather than exiting the editor. The terminal entry point can use raise SystemExit(exit_code). Argument parsing and exit conventions are owned by Building Command-Line Tools with argparse & Typer; persistent diagnostics belong to Logging, Debugging & Error Handling at Scale.

When a job launches another program, use an explicit argument list, check its exit status and bound its runtime. Those APIs are taught in Running Commands with subprocess; this lesson's jobs do not invoke shell commands or cloud services.

Configure cron, Task Scheduler or a CI scheduler with an explicit interpreter, working directory, environment and timezone. A scheduled shell may not have your interactive PATH. Decide what happens when a run overlaps the previous one, and what catches up after downtime. A lock or a single-worker scheduler controls overlap; idempotency alone does not serialize two writers.

Complete a preview that leaves state untouched

Fill the dry-run guard and the desired value. The first call must describe an intended change while leaving the setting alone; the applied call changes it, and repeating the applied call reports no work.

def ensure_mode(state, desired, dry_run):
    if state["mode"] == desired:
        return "unchanged"
    if ___:
        return "would change"
    state["mode"] = ___
    return "changed"

state = {"mode": "safe"}
print(ensure_mode(state, "fast", True), state["mode"])
print(ensure_mode(state, "fast", False), state["mode"])
print(ensure_mode(state, "fast", False), state["mode"])

# 👉 replace ___ with dry_run
# 👉 replace ___ with desired


# ✅ Expected output:
# would change safe
# changed fast
# unchanged fast

Plan a cleanup before removing anything

A log-retention planner receives the fixed records ("alpha", 4), ("beta", 12) and ("gamma", 9), where the second value is age in days. Return sorted names whose age is strictly greater than a ten-day retention setting. Reject a negative setting. Print the plan and prove the input list is unchanged. This exercise prepares data only; do not read the clock or delete actual files.

def cleanup_plan(records, retention_days):
    if type(retention_days) is not int or retention_days < 0:
        raise ValueError("retention_days must be a nonnegative integer")
    return sorted(name for name, age in records if age > retention_days)

records = [("alpha", 4), ("beta", 12), ("gamma", 9)]
before = records.copy()
print(cleanup_plan(records, 10))
print("input unchanged:", records == before)
try:
    cleanup_plan(records, -1)
except ValueError as error:
    print(type(error).__name__ + ":", error)


# ✅ Expected output:
# ['beta']
# input unchanged: True
# ValueError: retention_days must be a nonnegative integer

Applying this plan would be a separate operation that revalidates each target and handles failures. A retention plan is neither a backup nor evidence that anything has been deleted. The distinction lets you review intended work before granting a real job destructive privileges.

Practice quiz

A job appends the required configuration line on every run, so rerunning it creates duplicates. Which update makes repeated runs converge on the desired file content?

  • Compare with the desired content and replace only if different
  • Append the line again whenever the scheduler retries the job
  • Use exclusive append access so only one writer appends at once
  • Truncate the destination before checking the proposed content

Answer: Compare with the desired content and replace only if different. Reconcile the existing content with the desired state. Serializing appends still creates duplicates across runs; truncating before validation can destroy a working configuration.

A dry-run function saves the file and then prints what it would change. Where should the save move so a dry run performs no write?

  • Into the planner that computes the proposed changes
  • Before the dry-run flag is read from the command line
  • Into the apply branch used only when dry-run is false
  • Into both branches with file-write exceptions suppressed

Answer: Into the apply branch used only when dry-run is false. Planning returns data about intended changes. Only the non-dry-run apply branch may perform those writes; printing a preview after saving is already too late.

A string-prefix check accepts /jobs/export-old as if it were inside /jobs/export. For the lesson's trusted-directory case, what should the check compare?

  • The common text prefix of the two path strings
  • The resolved path and its actual parent chain
  • Whether both path strings begin at a filesystem root
  • Whether the submitted string contains a literal ..

Answer: The resolved path and its actual parent chain. A sibling can share the same text prefix. Check resolved path ancestry, while remembering that this check alone is not a sandbox against hostile filesystem races.

A job replaces two files in sequence. The first replacement succeeds; the second fails before changing its destination. What state must recovery handle?

  • Both original files, restored by the second failure
  • Both replacement files, committed as a single pair
  • Neither destination file, removed by the failed pair
  • The first replacement file and the second original file

Answer: The first replacement file and the second original file. A replacement of one file does not make several replacements a transaction. Plan recovery for partial completion; a successful first replacement is not automatically undone.

A required step fails. The job logs the error but exits with status zero, and the scheduler reports success. Which change fixes the false success signal?

  • Exit with a nonzero status after handling the failure
  • Keep status zero and add another error log message
  • Print a failure summary after the normal success message
  • Catch the error in the scheduler and keep status zero

Answer: Exit with a nonzero status after handling the failure. Logging explains the failure to a reader; the process exit status communicates it to the scheduler. A required-step failure must not be reported as a successful exit.

Two copies of an otherwise idempotent job overlap and overwrite each other's work. Which change addresses the overlap itself?

  • Repeat the same desired-state comparison in each copy
  • Rerun both copies whenever their outputs are different
  • Use a lock or scheduler rule that prevents overlap
  • Preview each copy's changes before either copy starts

Answer: Use a lock or scheduler rule that prevents overlap. Idempotence describes the effect of repeating an operation, not simultaneous execution. Coordinate writers with an appropriate lock or a scheduler concurrency rule.

WORKERS is present with the text value "0". This application requires a positive integer. How should configuration loading handle it?

  • Accept zero because the environment variable is present
  • Reject it because it violates the positive-integer rule
  • Treat it as missing and silently use the default value
  • Replace it with one before checking the supplied value

Answer: Reject it because it violates the positive-integer rule. Presence, parsing and validity are separate checks. The explicit value zero is not a missing setting and violates this application's stated contract.

A pure planner selects names whose age is strictly greater than 10. Its records, in order, are alpha: 10, beta: 12, gamma: 9. Which list does it return?

  • ['alpha', 'beta']
  • ['gamma']
  • ['alpha', 'beta', 'gamma']
  • ['beta']

Answer: ['beta']. Strictly greater excludes the boundary age as well as younger records. The planner returns proposed names; this example does not delete files.

Continue this course

Related lessons