Load Balancer Drain & Re-Enable
Take nodes out and back in safely.
Load Balancer Drain & Re-Enable is a free Ansible Academy lesson on CoddyKit — lesson 4 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.
Updating Live Traffic Is Risky
If you patch a server while users hit it, requests fail. The fix is to drain the node from the load balancer first. 🚦
What Draining Means
To drain a node is to stop sending it new traffic while letting in-flight requests finish. Then it is safe to update.
The Drain, Update, Re-Enable Cycle
The safe pattern is three steps: drain the node, deploy your change, then re-enable it in the load balancer.
Disable a Node in HAProxy
The community.general.haproxy module sets a backend node to disabled, telling HAProxy to stop routing traffic to it.
- name: Drain web node
community.general.haproxy:
state: disabled
host: "{{ inventory_hostname }}"
socket: /var/run/haproxy.sockRun the LB Task From the LB
The HAProxy socket lives on the load balancer, so delegate_to the LB host while still naming the web node being drained.
delegate_to: lb01Wait for Connections to Drain
Give active requests time to finish. Use wait_for or a short pause so you do not cut off users mid-request.
- name: Let traffic drain
ansible.builtin.wait_for:
timeout: 30
delegate_to: localhostNow Deploy Safely
With the node out of rotation, run your update tasks: install packages, push config, restart the app. No live traffic is affected. 🔧
Re-Enable the Node
After updating, set the node back to enabled so HAProxy resumes routing traffic to your freshly deployed server.
- name: Re-enable web node
community.general.haproxy:
state: enabled
host: "{{ inventory_hostname }}"
socket: /var/run/haproxy.sock
delegate_to: lb01Health Check Before Re-Enabling
Confirm the app is actually up before adding it back. A wait_for or uri check on the health endpoint avoids serving a broken node.
- ansible.builtin.uri:
url: http://localhost:8080/health
status_code: 200
retries: 5
delay: 3Do One Node at a Time
Pair this cycle with serial so only one or two nodes drain at once. The rest keep serving, giving you zero downtime.
- hosts: web
serial: 1Always Re-Enable, Even on Failure
Wrap the deploy in a block with always so a failed update still puts the node back into rotation instead of leaving it stranded.
Quick Check
Let us check the safe rollout pattern.
Recap
For zero downtime, drain a node, deploy, health-check, then re-enable, one node at a time with serial and an always block. ✅
Frequently asked questions
Is the “Load Balancer Drain & Re-Enable” lesson free?
Yes — the full text of “Load Balancer Drain & Re-Enable” 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 “Load Balancer Drain & Re-Enable”?
Take nodes out and back in safely. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Load Balancer Drain & Re-Enable” 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
- Automating Network Devices with cli_command
- delegate_to & run_once for Orchestration
- Coordinate Web, App & DB Tiers
- Load Balancer Drain & Re-Enable