Run a command on 200 hosts in parallel and produce a summary of which succeeded and failed.
IntermediateScenarioScenarios: deployments, user management and fleet operations
Answer
Situation read → Bound concurrency, add timeouts, capture per-host output and exit codes.
What I'd do →
out=$(mktemp -d)
run_one() {
local h=$1
if timeout 60 ssh -n -o BatchMode=yes -o ConnectTimeout=5 "$h" 'sudo systemctl is-active app' > "$out/$h.log" 2>&1
then echo "$h OK"; else echo "$h FAIL($?)"; fi
}
export -f run_one; export out
xargs -a hosts.txt -P 20 -I{} bash -c 'run_one "$1"' _ {} | sort > "$out/summary.txt"
grep -c ' OK' "$out/summary.txt"; grep FAIL "$out/summary.txt" || true
-P 20limits parallel SSH sessions;-nstops SSH from reading stdin.- Pass the host as an argument (
_ {}), never into the command string.
Why → You get a reproducible summary and per-host logs for the failures, without hammering the network.
Interview tip → Mention Ansible ad-hoc (ansible all -m shell -a ... -f 20) or pssh as ready-made alternatives.
More on Scenarios: deployments, user management and fleet operations
Write the core of a deploy script with release directories, an atomic symlink switch and rollback.IntermediateDeploy to 10 servers behind an AWS ALB one at a time, removing each from the target group during its deploy. Outline the script.AdvancedAfter a deploy, the health check fails intermittently. Your script must roll back automatically, but only if the failure persists. How?IntermediateImplement a blue/green switch on a single nginx host with a script.AdvancedOnboard 30 engineers: create accounts and install their SSH keys from their GitHub usernames, idempotently.IntermediateAn engineer leaves the company. Write the offboarding steps for Linux servers.Intermediate