Lesson 3 of 9 · 26m
Failure, Facts, and Task Design Under Imperfect Reality
Design tasks around observable evidence, precise conditions, controlled failure, and the operational meaning of unreachable or partially converged hosts.
Ansible does not run in a world where every host is healthy, every package mirror works, and every machine has the same facts. Production automation is negotiation with imperfect infrastructure. Good task design makes uncertainty visible: check what can be checked, fail when assumption is unsafe, and never confuse a skipped task with a successful one.
Facts are evidence, not decoration
Gathered facts describe what Ansible could observe at the start of the play:
- name: Configure firewall for Debian family
ansible.builtin.include_tasks: firewall-debian.yml
when: ansible_os_family == 'Debian'
- name: Configure firewall for Red Hat family
ansible.builtin.include_tasks: firewall-redhat.yml
when: ansible_os_family == 'RedHat'Facts can be stale, missing, cached, or wrong because discovery failed. They are not a substitute for checking the concrete resource you are about to mutate. For critical branching, read the evidence you actually depend on.
Prefer precise conditions over broad regexes
Weak conditions produce automation that works on the control node and lies in the field:
- name: Enable automatic security updates
ansible.builtin.template:
src: 20auto-upgrades.j2
dest: /etc/apt/apt.conf.d/20auto-upgrades
when:
- ansible_os_family == 'Debian'
- security_auto_updates_enabled | boolThis condition names the operating platform and the policy input. It does not guess based on /etc/os-release text scraped inside a shell task. Precision makes review possible.
failed_when is a policy statement
Some commands return nonzero for reasons humans consider acceptable, while others return zero after doing the wrong thing. Make the failure policy explicit:
- name: Validate application configuration
ansible.builtin.command: /opt/app/bin/app validate
register: app_validate
changed_when: false
failed_when: "'valid' not in app_validate.stdout"changed_when: false is especially important for validation and read-only probes. Otherwise every health check pollutes the change count and teaches operators to ignore it.
ignore_errors hides the bodies
Avoid this pattern in production roles:
- ansible.builtin.command: /opt/vendor/bin/reconfigure
ignore_errors: trueThe recap may be green while one host never converged. If partial failure is acceptable, encode what partial means:
- ansible.builtin.uri:
url: https://telemetry.internal/report
method: POST
register: telemetry
failed_when: false
- ansible.builtin.fail:
msg: Telemetry endpoint repeatedly unavailable
when: telemetry.status is defined and telemetry.status >= 500That still is not ideal — often the right answer is to remove brittle coupling — but it turns hidden failure into explicit policy.
Unreachable is not converged
An unreachable host is absent from most of the play. The final recap can look comforting if you read only changed and failed. Track unreachable as a first-class failure. At fleet scale, five silent unreachable nodes can be the five nodes responsible for a regional outage.
Field notes
registervariables are snapshots. Do not reuse them as if they remain live system state.assertis useful for preconditions, but write assertion messages for the operator who reads them at 3 a.m.- Do not branch on distribution version strings when a capability check is more truthful.
rescueandalwaysblocks can be valuable, but they are not transaction semantics. Ansible has no automatic rollback.- A task that merely reports state should normally use
changed_when: falseand rarely mutate the system.
Was this lesson helpful?