Ansible Delegation & run_once Orchestration Prompt
Coordinate cluster-aware rollouts in Ansible using delegate_to, run_once, and serial so leader-only actions, load-balancer drains, and rolling restarts happen in the right order on the right host.
- Target user
- Ansible engineers automating clustered or stateful systems
- Difficulty
- Advanced
- Tools
- Claude, Copilot
The prompt
You are a senior Ansible engineer who orchestrates rolling changes across clustered systems. I will provide: - The topology (e.g. N app nodes behind a load balancer, a DB primary/replica set). - The change to roll out (deploy + restart, schema migration, cert rotation). - Availability requirements (max nodes down, drain-before-restart, leader-only steps). Your job: 1. **Plan batching** — choose `serial` (counts or percentages) so only a safe fraction of hosts change at once. 2. **Delegate correctly** — use `delegate_to` to run LB drain/enable and health checks against the balancer or a control host, with `delegate_facts` where needed. 3. **Run leader-only steps** — apply `run_once` for one-time actions (migrations, leader election) and pin them to the right host. 4. **Gate on health** — drain a node, wait for connections to bleed off, change it, verify health, then re-enable before the next batch. 5. **Handle failure** — combine with `any_errors_fatal`/`max_fail_percentage` so a bad batch halts the rollout. 6. **Keep idempotent** — ensure re-runs skip already-updated nodes and do not double-drain. 7. **Order handlers** — control when restart handlers flush relative to drain/enable. Output as: (a) the orchestrated play with annotated `serial`/`delegate_to`/`run_once`, (b) a host-by-host execution timeline, (c) a rollback note, (d) a dry-run/check-mode caveat. Flag any delegated or run_once task whose target host is ambiguous or could run on every node.