Ansible Rolling Updates
Updating every server at the same moment risks a full outage. A broken release takes the whole site offline. A rolling update changes a few servers at a time, checks that they work, and moves on. Think of repainting a row of shop windows: the owner covers one window while the others stay open for customers. Ansible supports this style through serial, delegate_to, run_once, and max_fail_percentage.
Diagram: Rolling in Batches
Load Balancer
│
┌────┼────┬────┬────┬────┬────┐
▼ ▼ ▼ ▼ ▼ ▼ ▼
web1 web2 web3 web4 web5 web6 web7
serial: 2
Batch 1 ▶ [web1 web2] update, test ✔
Batch 2 ▶ [web3 web4] update, test ✔
Batch 3 ▶ [web5 web6] update, test ✔
Batch 4 ▶ [web7] update, test ✔
Servers outside the active batch keep serving visitors.
The Serial Keyword
Add serial at the play level. Ansible finishes all tasks on one batch before touching the following batch.
- name: Update web servers safely
hosts: webservers
become: true
serial: 2
tasks:
- name: Install the new package version
ansible.builtin.package:
name: shop-app
state: latest
The value accepts several formats.
serial: 1updates one host at a timeserial: "25%"updates a quarter of the group per batchserial: [1, 3, "100%"]starts with a canary host, continues with three hosts, and ends with all that remain
The Canary Pattern
A canary host takes the update before anyone else. Staff watch the result, and the rest of the fleet follows after a clean report. The list form of serial creates this pattern in one line.
Delegation
The delegate_to keyword runs a task on a different machine while facts and variables still belong to the current host. A typical case removes a web server from the load balancer before the update.
tasks:
- name: Remove this server from the load balancer
ansible.builtin.command: /usr/local/bin/lb-drain {{ inventory_hostname }}
delegate_to: lb1
- name: Update the application
ansible.builtin.package:
name: shop-app
state: latest
- name: Add this server back to the load balancer
ansible.builtin.command: /usr/local/bin/lb-enable {{ inventory_hostname }}
delegate_to: lb1
Use delegate_to: localhost for work that belongs on the control node, such as sending a chat notification or calling a web service.
Run Once
The run_once: true keyword executes a task on a single host in the batch. Database migrations, release tagging, and ticket updates fit this keyword.
- name: Run the database migration once
ansible.builtin.command: /opt/shop/migrate
run_once: true
delegate_to: web1
Stop When Things Go Wrong
Set max_fail_percentage to abort the play when too many hosts fail. A value of 0 halts on the first failure inside a batch.
- hosts: webservers
serial: 3
max_fail_percentage: 0
The any_errors_fatal: true keyword stops the whole play across all hosts after any single failure.
Health Checks Between Batches
post_tasks:
- name: Confirm the site responds
ansible.builtin.uri:
url: "http://{{ inventory_hostname }}/health"
status_code: 200
register: health
retries: 5
delay: 10
until: health.status == 200
A failed health check halts the rollout before the following batch starts, which limits the damage to a small group of servers.
Strategy Options
| Strategy | Behavior |
|---|---|
| linear | Default. Every host finishes a task before any host starts the following one |
| free | Each host runs at its own speed through the task list |
| host_pinned | Keeps a worker on one host until it finishes |
Try This
Build an inventory with four hosts that all point to localhost through different aliases. Run a play with serial: 2 and a debug task that prints inventory_hostname. Watch Ansible process two aliases per batch. Change the value to "50%" and compare the output.
Summary
Rolling updates protect uptime by changing small batches of servers. The serial keyword defines batch size, and a list creates canary stages. The delegate_to keyword runs a task on another machine, and run_once runs a task a single time. Health checks and max_fail_percentage stop a bad release early.
