DevOps / Ansible Interview questions
What happens when a task fails in an Ansible playbook?
By default, when a task fails on a given host, Ansible immediately stops running further tasks against that specific host for the remainder of the play, while continuing normally on any other hosts that haven't failed. The host is marked as failed and, unless later corrected, is excluded from any remaining tasks in that play.
- name: A task that might fail but shouldn't stop the play ansible.builtin.command: /opt/scripts/check.sh ignore_errors: true - name: Custom failure condition ansible.builtin.command: /opt/scripts/deploy.sh register: result failed_when: "'ERROR' in result.stdout"
ignore_errors: true lets the play continue on that host even after a failure, while failed_when lets you redefine what "failure" even means for a task, useful when a script's exit code doesn't perfectly line up with success/failure. Failed hosts are recorded in a .retry file by default, which can be fed back into --limit @site.retry to re-run the playbook only against the hosts that failed last time, rather than the whole inventory.
More Related questions...