Ansible Tutorial · Advanced Topics · Lesson 19 of 20

Ansible Rolling Updates

Deploy to a fleet gradually with zero downtime.

Members can save this page, take notes and track progress. See membership · Sign in

The serial keyword

By default Ansible runs tasks on all hosts at once. For deployments, you want to update a few hosts at a time so the rest keep serving traffic. The serial keyword controls the batch size:

- name: Rolling deploy
  hosts: webservers
  serial: 2            # update 2 hosts at a time
  become: true
  tasks:
    - name: Pull latest code
      ansible.builtin.git:
        repo: https://github.com/org/app.git
        dest: /var/www/app
        version: "{{ release_tag }}"

    - name: Restart app
      ansible.builtin.service:
        name: app
        state: restarted

Percentage and stepping

serial: "30%"              # 30% of hosts per batch
serial: [1, 5, "100%"]     # first batch=1, second=5, rest=all (canary pattern)

The stepping pattern is a canary deploy: update 1 host, verify, then 5, then the rest.

max_fail_percentage

Abort the rollout if too many hosts fail:

- name: Safe deploy
  hosts: webservers
  serial: 3
  max_fail_percentage: 10

If more than 10% of hosts in a batch fail, Ansible stops and does not process remaining batches.

pre_tasks and post_tasks

Run tasks before and after the main task list — commonly used to drain and restore a load balancer:

- name: Rolling deploy with LB
  hosts: webservers
  serial: 2
  become: true

  pre_tasks:
    - name: Remove from load balancer
      ansible.builtin.uri:
        url: "http://lb.internal/api/drain/{{ inventory_hostname }}"
        method: POST

    - name: Wait for connections to drain
      ansible.builtin.pause:
        seconds: 10

  tasks:
    - name: Deploy new code
      ansible.builtin.git:
        repo: https://github.com/org/app.git
        dest: /var/www/app
        version: "{{ release_tag }}"

    - name: Restart app
      ansible.builtin.service:
        name: app
        state: restarted

  post_tasks:
    - name: Health check
      ansible.builtin.uri:
        url: "http://{{ inventory_hostname }}:{{ app_port }}/health"
        status_code: 200
      retries: 5
      delay: 3

    - name: Add back to load balancer
      ansible.builtin.uri:
        url: "http://lb.internal/api/restore/{{ inventory_hostname }}"
        method: POST

delegate_to

Run a task on a different host (useful for health checks from the load balancer):

- name: Check health from LB
  ansible.builtin.uri:
    url: "http://{{ inventory_hostname }}:8080/health"
  delegate_to: lb-1.internal

wait_for — wait for a port or file

- name: Wait for app to start
  ansible.builtin.wait_for:
    port: 8080
    host: "{{ inventory_hostname }}"
    delay: 5
    timeout: 60

Full zero-downtime pattern

serial: [1, "30%", "100%"]    # canary → small batch → rest
max_fail_percentage: 10        # abort early on failures
pre_tasks:                     # drain from LB
tasks:                         # deploy + restart
post_tasks:                    # health check + restore to LB

This pattern guarantees that at any point during the rollout, most hosts are still serving traffic with the previous version.

Serial deployment
cat > /tmp/rolling_demo.yml << 'EOF'
---
- name: Rolling update demo
  hosts: localhost
  connection: local
  gather_facts: false
  serial: 1
  tasks:
    - name: Simulate deploy
      ansible.builtin.debug:
        msg: "Deploying to {{ inventory_hostname }}"
    - name: Simulate health check
      ansible.builtin.debug:
        msg: "Health check passed for {{ inventory_hostname }}"
EOF
ansible-playbook /tmp/rolling_demo.yml

Output

PLAY [Rolling update demo] ****************************************************

TASK [Simulate deploy] ********************************************************
ok: [localhost] => {
    "msg": "Deploying to localhost"
}

TASK [Simulate health check] **************************************************
ok: [localhost] => {
    "msg": "Health check passed for localhost"
}

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run this on your computer — see “Get Started” for setup.

Test yourself

What does 'serial: [1, 5, "100%"]' do?

  1. Runs on 1 host, then 5, then all remaining hosts
  2. Runs all tasks 1 time, then 5 times, then 100 times
  3. Sets the speed to 1x, 5x, then 100x
  4. Limits output to 1, 5, then 100 lines
Show answer

Answer: Runs on 1 host, then 5, then all remaining hosts

This is a canary pattern: the first batch processes 1 host (the canary), the second batch processes 5, and the remaining batches process all remaining hosts.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake