To ensure that an Ansible task is not executed if a previous task was unsuccessful, you can use Ansible's built-in error handling and conditional execution features. Here are several methods to achieve this:
1. Using failed_when and when
You can use the failed_when parameter to explicitly define when a task should be considered failed. Then, use the when parameter to conditionally execute subsequent tasks based on the success of the previous task.
Example:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
register: task1_result
failed_when: task1_result.rc != 0 # Explicitly define failure condition
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
when: task1_result is succeeded # Only run if Task 1 succeeded
2. Using block and rescue
The block and rescue structure allows you to group tasks and handle errors for the entire block.
Example:
- block:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
rescue:
- name: Handle error
ansible.builtin.debug:
msg: "An error occurred in the block"
In this example, if any task in the block fails, the tasks in the rescue section will be executed.
3. Using ignore_errors and when
If you want to ignore errors in a task but still prevent subsequent tasks from running, you can use ignore_errors combined with a when condition.
Example:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
ignore_errors: true
register: task1_result
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
when: task1_result is succeeded # Only run if Task 1 succeeded
4. Using any_errors_fatal
You can set any_errors_fatal: true at the play level to stop the entire play if any task fails.
Example:
- hosts: all
any_errors_fatal: true
tasks:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
In this example, if Task 1 fails, Task 2 will not be executed, and the play will stop immediately.
5. Using failed_when with when
You can use failed_when to define a custom failure condition and then use when to conditionally execute the next task.
Example:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
register: task1_result
failed_when: task1_result.rc != 0 # Define failure condition
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
when: task1_result is succeeded # Only run if Task 1 succeeded
6. Using meta: end_play
If you want to stop the play entirely after a task fails, you can use the meta: end_play task.
Example:
- name: Task 1 - Install a package
ansible.builtin.apt:
name: some_package
state: present
register: task1_result
failed_when: task1_result.rc != 0 # Define failure condition
- name: Stop play if Task 1 failed
ansible.builtin.meta: end_play
when: task1_result is failed # End play if Task 1 failed
- name: Task 2 - Configure the package
ansible.builtin.command: "some_command"
In this example, if Task 1 fails, the play will end immediately, and Task 2 will not be executed.
Conclusion
By using these methods, you can ensure that subsequent tasks are not executed if a previous task fails, providing robust error handling in your Ansible playbooks. 🚀