← All writing
blog

Nomad Plan Diff

Understanding nomad diffs is a big pain

~/.local/bin/nomad-tmpl-diff (or wherever you keep scripts):

#!/usr/bin/env bash
# Usage: nomad job plan foo.hcl 2>&1 | nomad-tmpl-diff
grep "EmbeddedTmpl:" | sed 's/.*EmbeddedTmpl:[[:space:]]*//' | while IFS= read -r val; do
diff \
<(echo "$val" | awk -v SIDE=L -f ~/.local/bin/nomad_diff.awk) \
<(echo "$val" | awk -v SIDE=R -f ~/.local/bin/nomad_diff.awk)
done

~/.local/bin/nomad_diff.awk:

{
p = "\\n\" => \""
idx = index($0, p)
left = substr($0, 2, idx - 2)
right = substr($0, idx + 8)
sub(/"$/, "", right)
s = (SIDE == "L") ? left : right
gsub(/\\n/, "\n", s)
gsub(/\\"/, "\"", s)
printf "%s\n", s
}

Then just run and see the magic!

Terminal window
nomad job plan monitoring.hcl | nomad-tmpl-diff

Note:

  • The inline quoting for awk’s gsub with escaped quotes is a nightmare in bash — the separate .awk file avoids that entirely and actually works reliably.