A playbook is configured to be executed on a single host.
That playbook has a single play.
That single play has 4 tasks.
If the first task fails on that single host, will the remaining 3 tasks execute?
If the first task fails on a host, Ansible won’t run the next tasks on that host; it stops right there. That’s the default behavior to avoid making things worse after something goes wrong. But if you want Ansible to keep going even when something fails, you can tell it to do so by adding ignore_errors: yes to that task. A better way, though, is to use a block with rescue and always sections, which lets you handle the failure more cleanly and still run follow-up steps if needed. This approach is clearer and is also recommended as a best practice.
@Trevor Very important concept on Ansible error handling in playbooks.
By default, Ansible stops executing tasks on a host when a task fails on that host.
You can use ignore_errors: yes on the failing task to allow the play to continue despite failure.
But the ignore_errors directive in Ansible only takes effect if a task actually runs and fails. It won’t help if the playbook hits problems like missing variables, connection issues, missing packages, or syntax errors in which case Ansible will still stop if those things happen.
You can use the ignore_unreachable keyword to skip task failures when a host is marked as ‘UNREACHABLE’. This lets Ansible bypass errors for those hosts and continue running tasks that follow, even if it couldn’t connect to them. Normally, when Ansible cannot reach a host, it flags the host as ‘UNREACHABLE’ and removes it from the active list for the remainder of the play.
Also, Ansible gives you control over task failures with the failed_when conditional. By default, if you specify a list of conditions under failed_when, the task only fails if all are true (implicit “and”). If you want the task to fail when any one condition is met, you need to combine them in a string using the explicit “or” operator.
Last but not the least :
You can manage how Ansible handles task failures by grouping tasks in a block and using the rescue and always sections.
If a task inside a block fails, the tasks under rescue will run automatically, letting you handle errors in a way that is similar to exception handling in programming.
The always section runs tasks no matter what, whether there was an error or not.
Keep in mind, though - {{ if a failure is due to an invalid task (like a syntax mistake) or an unreachable host, neither rescue nor always will be triggered during playbook execution }}
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_error_handling.html
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html#block-error-handling
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.