91.12 Safe rolling changes — serial, run_once and delegate_to
Story (fictional teaching scenario). At a fictional e-commerce company in Jaipur, Jonas from Oslo updated all 10 web servers at once. For two minutes no server was healthy. The next release used serial: 2 so only two servers changed at a time. Each server was taken out of the load balancer first with delegate_to, and one Slack-style announcement was printed with run_once. During their first test they noticed something surprising about run_once — you will see it below.
Why
Changing every server at the same moment can take the whole service down. Small batches keep most servers serving users.
What
- serial — How many hosts per batch. Can be a number, a percent like
"25%", or a list like[1, 5, "50%"]. - delegate_to — Run this task on another host (like
localhostor the load balancer), but on behalf of the current host. - run_once — Run this task for only one host in the current batch.
- max_fail_percentage — Stop the play if more than this percent of a batch fails.
How
- Add
serial:to the play. - Drain each host from the load balancer with a delegated task.
- Update, then health-check, then add it back.
- Add
max_fail_percentage: 0so the first broken batch stops the release.
Example 1: Rolling play
- name: Rolling change
hosts: web
gather_facts: false
serial: 1
tasks:
- name: Take node out of the load balancer
ansible.builtin.debug:
msg: "drain {{ inventory_hostname }}"
delegate_to: localhost
tags: [lb]
- name: Update app
ansible.builtin.debug:
msg: "update {{ inventory_hostname }}"
tags: [app]
- name: Announce once
ansible.builtin.debug:
msg: "release started"
run_once: true
tags: [notify]
Output (tested on ansible-core 2.22.0, local practice lab)
PLAY [Rolling change]
TASK [Take node out of the load balancer]
ok: [web1 -> localhost] => { "msg": "drain web1" }
TASK [Update app]
ok: [web1] => { "msg": "update web1" }
TASK [Announce once]
ok: [web1] => { "msg": "release started" }
PLAY [Rolling change]
TASK [Take node out of the load balancer]
ok: [web2 -> localhost] => { "msg": "drain web2" }
TASK [Update app]
ok: [web2] => { "msg": "update web2" }
TASK [Announce once]
ok: [web2] => { "msg": "release started" }
What it means: serial: 1 made two batches, so the play header appears twice. web1 -> localhost shows the drain step ran on localhost for web1. And the surprise: "release started" printed twice — run_once means once per batch, not once per whole run. For a true once-only announcement, put it in a separate play before the rolling play.
Example 2: Stop on first failure (shape)
- name: Rolling change
hosts: web
serial: 2
max_fail_percentage: 0
Output (illustrative — not run on a real server)
fatal: [web3]: FAILED! ...
NO MORE HOSTS LEFT
What it means: One failure in a batch stopped the release. The other servers kept the old, working version.
Ravindra Bagale's Tip
run_once inside a serial play runs once per batch. Put one-time steps in their own play.
serial play मध्ये run_once प्रत्येक batch ला एकदा चालतो. एकदाच करायचे steps वेगळ्या play मध्ये ठेवा.
serial play में run_once हर batch में एक बार चलता है. एक बार वाले steps अलग play में रखो.
Did you understand? Let us try it once more.