Lesson 9 of 9 · 29m
Safe Rollouts, Drift, and Operational Evidence
Operate Ansible as a change-management system: canaries, health gates, rollback strategy, drift detection, and evidence that survives the run.
At small scale, Ansible feels like a convenient remote execution tool. At fleet scale, it is a production control plane. That means change management: what is being deployed, which hosts are in scope, how failure is detected, how rollout stops, what rollback means, and what evidence remains afterward. The teams that answer these questions before pressing enter are the ones who sleep afterward.
Targeting is a safety control
Before a large run, print exactly what will be affected:
ansible-playbook site.yml --limit 'production:&web' --list-hosts
ansible-playbook site.yml --limit 'production:&web' --list-tasksThe --list-hosts output is cheap insurance against an inventory intersection mistake. Never rely on memory when a limit pattern decides production scope. If the list surprises you, stop. Surprise is information.
Put health gates inside the rollout
A deployment play should prove service health after activation, not merely report systemd state:
- name: Wait for application health endpoint
ansible.builtin.uri:
url: http://127.0.0.1:8080/healthz
status_code: 200
register: health
retries: 10
delay: 3
until: health.status == 200
- name: Stop rollout when health cannot be proven
ansible.builtin.fail:
msg: Application health check failed; halting fleet rollout
when: health.status != 200Use meaningful checks: process state, local health endpoint, upstream connectivity, queue workers, and critical configuration version. A service can be active while the application is dead.
Rollback is a designed path, not a hope
Ansible does not provide transactional rollback. If a deployment breaks, what exactly happens?
configuration rollback restore prior template and activate service
binary rollback reinstall or relink prior release artifact
data rollback often impossible; design forward migrations carefully
fleet rollback repeat safe rollout procedure in reverse scopeKeep prior release artifacts available long enough to use them. Record versions in a local fact or deployment manifest. A rollback that depends on a package version no longer in the repository is theater.
Configuration drift is a lifecycle problem
Drift happens because humans make emergency changes, vendors rewrite files, cloud-init reruns, package postinstall scripts mutate config, and other automation systems compete. Decide the reconciliation policy. Some files are fully owned; others are enforced only for security-critical directives:
- name: Enforce critical sshd directives
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: "^{{ item.key }}\s"
line: "{{ item.key }} {{ item.value }}"
loop:
- { key: PermitRootLogin, value: "no" }
- { key: PasswordAuthentication, value: "no" }Full-file templates provide stronger convergence but higher ownership conflict. Directive enforcement is narrower but can leave contradictory duplicates if written carelessly. Choose deliberately per file.
Preserve evidence after the recap
For every meaningful fleet change, retain:
git commit and role versions
inventory source or dynamic inventory snapshot
exact command and limit expression
host list and batch plan
changed/unreachable/failed counts
health-gate outcomes
artifact versions deployed
operator and ticket referenceThis is not bureaucratic decoration. It is how you answer "why did these seventeen hosts behave differently?" three weeks later.
Emergency changes still need management
The worst configuration drift comes from heroic fixes. Create an emergency path that is fast but not invisible: apply the hotfix, record the exact state, then reconcile the permanent change back into inventory or roles. If production truth never returns to code, the next Ansible run becomes an unplanned rollback.
Field notes
--checkwith--diffis useful review input, but not every module implements check mode truthfully.- A green health check on the canary does not prove capacity under real load; know what the gate actually measures.
- Avoid automated remediation loops that fight human incident response without explicit coordination.
- Store run logs outside the control node so losing the runner does not lose the evidence.
- The best rollback test is performed before the emergency, on a small representative host.
Was this lesson helpful?