Automation
Ansible and AWX: validate a rolling deployment before touching all of production
A production runbook for bounding Ansible batches, testing failure stops, and deciding when to restore service or roll back from AWX.
An AWX job starts an application update across servers behind a load balancer. The first host restarts, but its health check fails. If the playbook proceeds across the fleet, a contained change can become a service-wide outage. Stopping the job, however, does not necessarily restore the first host.
This runbook covers a rolling Ansible deployment. It separates three decisions: how many hosts may change at once, when an error stops further batches, and how service is explicitly restored. Load balancer operations, application checks and rollback commands must be adapted to the environment.
Establish the deployment contract
Record the AWX project revision and execution environment, inventory and last synchronization, template, permitted variables and launch limit. Preserve the effective target list and compare it with the intended fleet. An incorrect AWX limit can make a sound playbook unsafe; an empty target list proves nothing.
For each batch, agree on:
- available capacity and the maximum number of members that can be removed without compromising service;
- the connection-draining criterion, deadline and decision when connections persist;
- the target release and last release that can actually be restored, including data and migration compatibility;
- local and external health checks, observation window and accountable operator;
- who authorizes continuation, how the job is stopped and how the change is reversed.
Host count alone is not a capacity calculation. Zone distribution, current load and dependencies may reduce headroom. Freeze concurrent changes to the same targets and check whether a data migration would make application rollback unsafe.
Know what Ansible controls
With the linear strategy, serial divides hosts into batches. The play completes on the current batch before moving to the next. A value of 1 limits the batch to one host; it does not drain traffic or verify remaining capacity.
any_errors_fatal: true stops progression after a fatal failure. Ansible finishes the fatal task on hosts in the current batch, then stops subsequent tasks and plays. This is neither an instantaneous cancellation of work in flight nor a rollback. Asynchronous tasks, external operations and already accepted commands may continue after the job stops.
max_fail_percentage provides a different policy. With serial, it applies to each batch, and the threshold must be exceeded, not merely reached. With four hosts in a batch, a threshold of 49 stops the play after two failures; 50 does not stop it at two. The final batch may be smaller. Choose a stop policy the team understands and has tested; a percentage is not a capacity guarantee.
A successful rescue can also clear the original failed status for these controls. If recovery must stop the rollout, end it with an explicit failure and verify the AWX outcome. Do not use ignore_errors to turn a mandatory health check into a warning.
Test progression without changing hosts
Use a lab inventory with at least three reachable test hosts. This play only emits messages and injects a failure. Substitute the group and choose a target that exists. The third host must not reach the final task after the second fails.
---
- name: Test rolling stop
hosts: rolling_lab
gather_facts: false
strategy: linear
serial: 1
any_errors_fatal: true
tasks:
- name: Mark batch entry
ansible.builtin.debug:
msg: "Entering the test batch"
- name: Inject a failure on the second lab host
ansible.builtin.fail:
msg: "Expected test failure"
when: inventory_hostname == "lab-02"
- name: Mark successful batch
ansible.builtin.debug:
msg: "Batch completed"
Set the lab inventory order explicitly and confirm it in job events: the name lab-02 does not itself guarantee execution order. Repeat with an unreachable host and a failure after a simulated drain. Inspect job status, reached hosts and events; do not assume every failure follows the same recovery path. Use the same Ansible version and configuration as the production execution environment.
Build the production batch
Make each host transition observable: check capacity on the remaining members, request traffic removal, confirm draining, install the release, check locally, reintroduce the member, then observe the external path and errors for the agreed window.
The drain command must be idempotent or expose the member’s actual state. A load balancer API acknowledgement does not prove active connections have drained. Likewise, a running process does not prove the application is ready to serve requests. If drain or health validation fails, keep the member out of traffic, stop progression and notify the operator. Do not automatically re-enable it in an always block.
A first job limited to a canary permits a human decision before expansion. A play with several serial sizes may move through batches automatically; it is not an approval gate. To require a pause, separate launches or use a tested approval mechanism in the orchestrator. Ensure concurrent jobs cannot drain members of the same service at the same time.
Prove the stop and prepare rollback
In staging, inject a health-check failure after traffic removal. Verify that later hosts remain unchanged, the failing member stays isolated, the job reports failure and useful alerts remain active. Test controller interruption or job cancellation separately: a remote action may still be running, or a member may remain drained without cleanup tasks executing.
For each host, retain the before and after versions, check results, load balancer state, job ID and change ID. If an action’s outcome is uncertain, inspect the target system before retrying. Do not blindly replay the full job: an already applied action, migration or live traffic can change the decision.
Rollback first restores a compatible version on the isolated member, validates local health, then reintroduces it and checks external traffic. If data compatibility is not established, keep the member isolated and invoke the agreed recovery procedure with its owner. Stopping AWX does not perform these steps.
Decision
Continue in batches only when canary capacity, draining, health and telemetry meet the contract. Otherwise pause launches, stabilize affected members and choose a bounded correction or rollback based on their actual state. The result is not merely a green job: it is controlled progression, an observable stop and verified service recovery.
References: Ansible strategies and batches, error handling, blocks and rescue.