Lesson 2 of 7 · Automation

Automation vs Scripts

Why a script run by hand isn't automation: idempotency, declarative and imperative code, and what turns a script into automation.

Beginner 6 sections 4 min read

Scripts are a start, not the finish

Most DBAs already write shell, PowerShell, or SQL scripts, and that's a great start. But a script that one person runs by hand isn't the same as automation. Real automation is versioned, reviewed, tested, safe to run repeatedly, logged, and runs at scale without someone watching it.

A SCRIPT Kofi's laptop: add_space.sh prod-db-01 run by hand, when someone remembersno review, no log, no test AUTOMATION GitPipelineAnsible tests, approval db-01db-02db-03 versioned, reviewed, tested, logged,and repeatable at scale

What turns a script into automation

A typical scriptAutomation
Where it livesSomeone's laptop or home directoryGit, reviewed through pull requests
How it runsSomeone starts it by handA schedule, a pipeline, an event, or a self-service request
Running it twiceOften breaks or does the work twiceSafe: it only changes what isn't already right (idempotent)
Describing the workEvery step, in order (imperative)Often the end state wanted (declarative)
TargetsOne server, typed inAn inventory of many servers, grouped by role and environment
ErrorsMay carry on after a failureStops, reports, and can roll back
PreviewNone: it just runsA dry run shows what would change first
SecretsPasswords inside the scriptFetched from a vault at run time
RecordWhatever scrolled past on screenEvery run logged, with who, what, and when
TestingTried in productionTested automatically on test systems first

Idempotency: safe to run again

The single most important idea in automation is idempotency: running the same automation twice gives the same result as running it once. The first run makes the changes needed; later runs see that everything's already right and change nothing.

A typical script isn't idempotent. Run this twice and the second run fails, or worse, half-succeeds:

# create_user.sh: fails on the second run because the user already exists
sqlplus -s / as sysdba <<EOF
CREATE USER app_ro IDENTIFIED BY "Changeme1";
GRANT CREATE SESSION TO app_ro;
EOF

An automation tool checks first, and only acts if needed. In Ansible, the same task describes the desired state, and is safe to run any number of times:

# Ansible task: makes sure the user exists, and does nothing if it already does
- name: Ensure app_ro user exists
  oracle_user:
    schema: app_ro
    schema_password: "{{ vault_app_ro_password }}"
    grants: [create session]
    state: present

Idempotency is what lets automation run on a schedule, or fix drift: if someone changes a server by hand, the next run quietly puts it back the way the code says it should be.

Imperative and declarative

Imperative: how
You write each step: "create the directory, then copy the file, then change the owner". Shell scripts and PowerShell are imperative. You're responsible for checking what's already there.
Declarative: what
You describe the end state: "this directory should exist, owned by oracle". The tool works out the steps, and does nothing if it's already true. Terraform is declarative, and most Ansible modules behave declaratively.

Declarative code is shorter, idempotent by nature, and easier to review, because it reads as a description of what should exist.

Scripts still matter

None of this makes scripting skills less useful. Automation tools run scripts all the time, and some tasks are easiest as a short script. The difference is how the script is treated: kept in Git, reviewed, made safe to rerun, run by a tool or pipeline rather than by hand, and logged. A good rule: if you've run it by hand three times, it's time to automate it properly.

Key takeaways

  • A script run by hand from a laptop isn't automation; automation is versioned, reviewed, triggered, tested, logged, and runs at scale.
  • Idempotency means running automation again changes nothing if everything's already right, which makes it safe to schedule and to fix drift.
  • Imperative code lists steps; declarative code describes the end state and lets the tool work out the steps.
  • Good automation previews changes, stops on errors, uses a vault for secrets, and is tested before production.
  • Scripts still matter, inside automation; run something by hand three times, then automate it.