How can an Ansible playbook roll out a new application version to many servers without downtime?
Update the servers in batches (for example 10 at a time). For each batch: take it out of monitoring and load balancing, stop services, deploy, then reverse those steps before moving on.
* Only one batch of 10 is out of service at a time, so the rest keep serving users. *
For each batch of 10 application servers:
- Stop monitoring, so the planned outage doesn't trigger alarms.
- Remove from load balancing, so no user traffic reaches these servers.
- Stop services.
- Deploy the application.
- Reverse steps 3, 2, 1: start services, put the servers back into the load balancer, re-enable monitoring.
- Move on to the next 10 servers.
Because only one batch is out of service at a time, the remaining servers carry the traffic and users see no outage. This is called a rolling update. In a playbook, the batch size is set with serial: 10, and the six steps are ordinary tasks. Orchestrating several systems in order (monitoring, load balancer, app servers) is exactly where a push-based tool shines.
Go deeper:
Controlling playbook execution: strategies and more (official docs) —
serialas a number, percentage or ramp-up list of batch sizes.Controlling where tasks run: delegation and local actions (official docs) — the official example of pulling web servers out of a load balancer with
delegate_toduring aserialrollout.Blue Green Deployment (Martin Fowler) — the main alternative zero-downtime pattern, for comparison with rolling updates.