Quiz Entry - updated: 2026.11.12
How can the ansible.builtin.fail module replace failed_when, and what does that two-task approach add?
Run the command with ignore_errors: true, then a separate fail task with a when condition and a clear msg; it adds a readable error message and lets you delay the failure.
tasks:
- name: Run user creation script
ansible.builtin.shell: /usr/local/bin/create_users.sh
register: command_result
ignore_errors: true
- name: Report script failure
ansible.builtin.fail:
msg: "The password is missing in the output"
when: "'Password missing' in command_result.stdout"
The fail module does nothing except fail, with the message you choose. Compared with failed_when on the command itself, this gives you:
- A clear message. The output says "The password is missing in the output" instead of a long command result.
- A delayed failure. Because the check is a separate task, other tasks can be placed between the command and the
failtask, for example to finish or undo other changes before the play stops on that host.
ansible.builtin.assert is a close relative: it fails with a message when the conditions in its that: list are not all true.
Go deeper:
ansible.builtin.fail module (official docs) — the module and its
msgoption.ansible.builtin.assert module (official docs) — checking several conditions at once.