02 / CONFIGURATION AND TOPOLOGY · LESSON 07

Compare configuration snapshots

Compare literal configuration values while distinguishing missing and empty settings.

18 min · learn at your pace

Compare a defined format

A vendor configuration file is a language with its own quoting, ordering and defaults. This exercise uses a smaller collector export: one identifier key, an equals sign and a literal value. Blank lines and lines starting with a hash are ignored. Keys are unique within each snapshot.

Split at the first equals sign. A value can itself contain equals signs, spaces or punctuation, and an explicitly empty value still exists. Sourcing a snapshot would execute its content as shell code; reading it with a fixed parser preserves the code-versus-data boundary.

Review the union of keys

Loading only the newer file would miss removed settings. Compare the union of baseline and candidate keys and classify each as added, removed, changed or unchanged. Use membership tests to distinguish absence from an empty string. Keep the before and after values so a reviewer can explain the classification.

The output describes differences, not whether a candidate is safe to apply. Some settings have dependencies and secret values should not be copied into public reports. The supplied teaching snapshots contain fictional, non-secret values. Keeping parsing, comparison and application separate makes those later policy decisions possible without rewriting the comparison itself.

Try an example.sh
awk 'index($0,"="){at=index($0,"=");print substr($0,1,at-1)}' baseline.conf
YOUR TURN

Put it into practice.

Compare baseline.conf and candidate.conf. Both contain unique identifier keys with literal values separated by the first =; ignore blank lines and lines starting #. Values have no tabs/newlines, may be empty or contain more =, and must never execute. Print key<TAB>before<TAB>after<TAB>change for the union of keys, sorted by key. Use - for an absent side; classify added, removed, changed or unchanged. Presence is independent of an empty value.

See an explained solution
One way to solve it.sh
export LC_ALL=C
printf 'key\tbefore\tafter\tchange\n'
awk '/^#/ || $0==""{next}
{at=index($0,"=");k=substr($0,1,at-1);v=substr($0,at+1);keys[k]=1;if(FILENAME==ARGV[1])old[k]=v;else new[k]=v}
END{for(k in keys){b=(k in old)?old[k]:"-";a=(k in new)?new[k]:"-";s=!(k in old)?"added":!(k in new)?"removed":old[k]!=new[k]?"changed":"unchanged";printf "%s\t%s\t%s\t%s\n",k,b,a,s}}' baseline.conf candidate.conf | sort

The comparison retains literal values and uses key membership instead of truthiness. That makes explicit empty settings, removed settings and unchanged command-shaped text behave consistently without executing any of them.