Imperative and declarative configuration
Quick answer
Imperative automation describes steps; declarative automation describes the desired result. Idempotent operations converge on the intended state without unnecessary changes on repeated runs.
For example, appending the same setting on every run can create duplicates; setting its value to the desired value once avoids that drift. Test convergence by running twice and comparing the resulting state, not just the exit status.
Prepare the practice environment
Run python3 lab-fixtures/automation/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Only private files are used; optional examples require an authorized snapshot VM. Do not modify system services. Review the lab guide.
Prerequisites
Review: Deployment and rollback, Testing and debugging shell scripts.
How it works
An instruction such as appending a line can produce duplicates on every run. A desired-state approach compares the current configuration with a trusted definition, changes only what differs, and verifies the result. Drift means actual state diverges from the approved definition, for example after a manual edit. Keep non-secret desired configuration in version control, review changes and separate development and production values. A script can implement declarative behavior; the label alone does not guarantee idempotence. The helper uses a private scratch file, compares bytes and mode, and replaces it only when required. It does not manage a real service.
Try the examples
The helper appends the same line twice in a temporary directory. Repeated execution creates duplicate data.
Expected stdout:
2 linesFirst apply corrects content; second apply preserves the inode and modification time. The fixture checks mode 0600 as well.
Expected stdout:
changedunchangedThe helper changes a private copy manually and compares it with the approved definition before reconciling.
Expected stdout:
drift detectedchangedverifiedCommon mistakes
Do not assume every repeated command is safe. Review scope and compare state; never overwrite unknown production edits automatically.
Practice
Run converge and drift, then explain which state changes are expected. Identify a configuration change that must be reviewed rather than automatically reverted.
Related references
Study: diff. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Imperative and declarative configuration — Knowledge Check
Apply configuration management concepts to synthetic fixtures and VM-only scenarios.
What does declarative configuration specify?
What is drift?
Which second apply demonstrates idempotence?
Why review manual drift?