Idempotence and safe updates
An idempotent operation reaches the same intended state when repeated with the same inputs. For configuration automation, compare existing content with desired content before replacing it. A no-change run should not unnecessarily rewrite data or trigger a restart. Idempotence is a property of the specified state, not a guarantee that every observable effect, such as logs or access times, is identical.
Plan, validate, replace, verify
Use a private destination directory, generate the candidate there, validate the full candidate before replacement, and compare bytes. cmp -s returns 0 for equal files, 1 for differences and a higher status for trouble; do not treat every failure as a difference. In this fixed lab both files are known regular files and comparison errors block the update. A rename within one filesystem can provide atomic name replacement where supported: readers opening the path see either old or new content, but readers with existing open descriptors may still see the old file. GNU mv -T treats the target as a single path, and the lab assumes a private directory with no competing writers. A real system needs a locking and path trust strategy, correct ownership and permissions, durability guarantees and an explicit rollback plan. Atomic rename is not automatically crash durable or transactional across multiple files.
Separate dry run from apply: dry run explains intended changes and never replaces the target. Preserve original data on validation failure, and keep recovery material under a protected policy. Do not modify system configuration, services or real disks in these exercises. The invalid-candidate drill checks the write of a fixed expected file and distinguishes cmp status 1 from an operational comparison error. Both cases preserve the original target; only a confirmed difference is reported as an invalid candidate. The preview also compares the target bytes after the preview. The invalid-candidate drill completes successfully after demonstrating rejection; an actual update entry point must return a nonzero status when its requested change is rejected.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Apply once and then skip identical content
umask 077printf 'mode=dev\n' > app.conf || exit 1update() ( local_candidate=$(mktemp ./candidate.XXXXXXXX) || exit 1 trap 'rm -f -- "$local_candidate"' EXIT printf 'mode=test\n' > "$local_candidate" || exit 1 cmp -s "$local_candidate" app.conf case $? in 0) printf 'unchanged\n'; exit 0 ;; 1) mv -T -- "$local_candidate" app.conf || exit 1 ;; *) exit 2 ;; esac printf 'updated\n')update || exit 1first=$(stat -c '%i' app.conf) || exit 1update || exit 1second=$(stat -c '%i' app.conf) || exit 1[[ $second == "$first" ]] || exit 1cat app.confExpected stdout:
updatedunchangedmode=testThe candidate has a fixed allowed value. The second run does not replace the target; the inode assertion verifies this.
Reject an invalid candidate
printf 'mode=dev\n' > app.conf || exit 1printf 'mode=unknown\n' > candidate || exit 1printf 'mode=test\n' > expected.conf || exit 1cmp -s candidate expected.confrc=$?case $rc in 0) mv -T -- candidate app.conf || exit 1 ;; 1) printf 'invalid candidate\n' || exit 1 ;; *) exit 2 ;;esaccat app.confExpected stdout:
invalid candidatemode=devOnly exact known bytes pass this deliberately narrow validation. A real parser needs its own contract.
Preview a planned change
printf 'mode=dev\n' > app.conf || exit 1printf 'mode=dev\n' > before.conf || exit 1preview() { printf 'would set mode=test\n'}preview || exit 1cmp -s before.conf app.conf || exit 1cat app.confExpected stdout:
would set mode=testmode=devThe preview describes the operation and leaves the fixture unchanged.
Common mistakes
Do not truncate the existing configuration before validating its replacement. mv across filesystems may copy and remove rather than rename; atomicity does not imply durability. A dry run must not write the actual target.
Practice
Test a private config update with changed, unchanged and invalid desired contents. Confirm the unchanged run retains the same inode, the invalid run preserves bytes, and dry run does not change the target.
Next: Network and API automation