Interview Prep/Shell Scripting/Scenarios: deployments, user management and fleet operations

Push an updated nginx config to a fleet, but only reload hosts where the file changed and the config validates.

IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer

Situation read → Reloading unchanged hosts is noise; reloading with an invalid config takes the site down.

What I'd do →

for h in "${hosts[@]}"; do
  remote_sum=$(ssh -n "$h" 'sha256sum /etc/nginx/conf.d/app.conf 2>/dev/null | cut -d" " -f1')
  local_sum=$(sha256sum app.conf | cut -d' ' -f1)
  [[ $remote_sum == "$local_sum" ]] && { echo "$h unchanged"; continue; }
  scp -q app.conf "$h:/tmp/app.conf.new"
  ssh -n "$h" 'sudo cp -a /etc/nginx/conf.d/app.conf /etc/nginx/conf.d/app.conf.bak 2>/dev/null;
               sudo install -m 0644 /tmp/app.conf.new /etc/nginx/conf.d/app.conf &&
               sudo nginx -t && sudo systemctl reload nginx ||
               { sudo mv /etc/nginx/conf.d/app.conf.bak /etc/nginx/conf.d/app.conf; exit 1; }' ||
    { echo "$h FAILED, restored previous config" >&2; exit 1; }
done

Why → Checksums make the operation idempotent; validation plus restore makes it safe.

Interview tip → Mention that this is what Ansible's template + validate: + handler notify gives you for free.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: deployments, user management and fleet operations