Declarative Tools Converge on a Desired State, Imperative Scripts Replay Steps

In progress

Infrastructure as Code (IaC) manages servers and cloud resources from machine-readable files instead of clicks in a console. Those files come in two styles. An imperative script lists the steps to run; a declarative definition describes the end state and lets the tool work out the steps. The declarative style can be run again safely, which is what makes IaC dependable.

Two Styles Side by Side

Imperative ("how") Declarative ("what")
You write a sequence of commands the desired state
Example apt-get install nginx && systemctl start nginx Ansible state: present, Terraform resources, Kubernetes replicas: 3
Second run repeats every step, may fail or duplicate compares current and desired state, changes only the difference
Unexpected state the script doesn't notice the tool reports or corrects the drift

Why Code Beats Clicks

  • Reviewable: the definition sits in version control, so changes are visible, discussed and reversible.
  • Repeatable: the same file builds the same environment for dev, staging and production.
  • Fewer manual slips: no forgotten checkbox or mistyped value in a console.
  • Readable intent: others see what was meant to exist, not just what happens to exist.

Declarative definitions are naturally idempotent; imperative scripts can be made idempotent too, with explicit state checks.

References

Review cards

Flashcard Why can a declarative definition be run again safely when an imperative script can't?

The tool compares the current state with the desired one and changes only the difference. A script replays every step and assumes the starting state it was written for.