Write the same thing twice, imperatively and declaratively

short · 45 min · Objective 2.2

Task

Build one small environment two ways and then run each twice. The declarative version will converge; the imperative one will either fail or duplicate, and seeing that is what makes idempotence concrete.

Steps

  1. Write an imperative script that creates three things in order: a network, a container attached to it, and a file recording the container's address. Do not add any existence checks.
  2. Run it once and confirm it worked. Run it a SECOND time without cleaning up, and record exactly what happened in lab/imperative.txt -- an error, a duplicate, or a corrupted file.
  3. Write the declarative equivalent describing the same end state.
  4. Apply it twice and record the second run's output in lab/declarative.txt. It should report no changes.
  5. Delete the container by hand, apply the declarative version again, and record what it did. Write one sentence on why the same file can both create and repair.

Verify

grep -Eci 'error|exists|duplicate|fail' lab/imperative.txt
grep -Eci 'no change|up.to.date|0 to add|nothing to do' lab/declarative.txt
grep -Eci 'recreat|restor|repair' lab/declarative.txt

All three non-zero. If the second imperative run succeeded cleanly, you added an existence check and made it idempotent -- which is the lesson, but record that you did it deliberately.

This is an independent study companion for CompTIA Cloud+ CV0-004 and is not produced by or endorsed by CompTIA.