0Pricing
Ansible Academy · Lesson

Rolling Deploy Behind the Load Balancer

Release with zero downtime in serial.

Rolling Deploy Behind the Load Balancer is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Zero Downtime Is the Promise

Update all web nodes without dropping a single request. The trick is a rolling deploy: change a few hosts at a time. 🔄

serial Controls the Batch

Add serial to your play so Ansible works through hosts in small waves instead of all at once.

- hosts: web
  serial: 1
  tasks:
    - import_role:
        name: webapp

Percentages Work Too

serial accepts a percentage or a list of sizes, so you can ramp: a canary first, then larger batches.

serial:
  - 1
  - 25%
  - 50%

Drain Before You Touch

Before updating a node, take it out of rotation. Use delegate_to to tell the load balancer to stop sending it traffic.

- name: Drain node
  community.general.haproxy:
    state: disabled
    host: "{{ inventory_hostname }}"
  delegate_to: lb1

Why delegate_to the LB

The web host cannot disable itself in the balancer; the load balancer must. delegate_to runs that task on lb1 for each web host.

Deploy the New Version

With the node drained, deploy code, render config and let a handler reload the app service for that host only.

- name: Pull release
  ansible.builtin.git:
    repo: "{{ app_repo }}"
    version: "{{ app_version }}"
    dest: /srv/app
  notify: Reload app

Health Check Before Re-Enable

Confirm the node is healthy with uri before sending users back. Never re-add a broken host to the pool.

- name: Wait for health
  ansible.builtin.uri:
    url: http://localhost:8080/health
    status_code: 200
  retries: 5
  delay: 3

Re-Enable in the Balancer

Once healthy, put the node back into rotation, again via delegate_to the load balancer. Traffic flows to the new version.

- name: Enable node
  community.general.haproxy:
    state: enabled
    host: "{{ inventory_hostname }}"
  delegate_to: lb1

max_fail_percentage Guards You

Set max_fail_percentage so the rollout halts if too many hosts fail, stopping a bad release from spreading further.

- hosts: web
  serial: 1
  max_fail_percentage: 20

run_once for Shared Steps

Some steps, like a one-time db migration, should run a single time. Use run_once so only one host executes them.

- name: Migrate db
  ansible.builtin.command: /srv/app/migrate
  run_once: true

The Whole Loop

Per host: drain, deploy, health-check, re-enable. serial repeats that loop in safe waves until the fleet is updated. ✅

Quick Check

One keyword makes the LB step work.

Recap

You built a rolling deploy: serial batches, drain and re-enable via delegate_to, health checks, and a fail guard. Zero downtime. 🟢

Frequently asked questions

Is the “Rolling Deploy Behind the Load Balancer” lesson free?

Yes — the full text of “Rolling Deploy Behind the Load Balancer” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Rolling Deploy Behind the Load Balancer”?

Release with zero downtime in serial. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rolling Deploy Behind the Load Balancer” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Architect the Project: Roles & Inventory
  2. Provision, Harden & Bootstrap Hosts
  3. Rolling Deploy Behind the Load Balancer
  4. Smoke Tests, Rollback & Notifications
← Back to Ansible Academy