Red Hat Certified Engineer RHCE · Free Practice Question Medium
Question 8
You are running an Ansible playbook, and one of the tasks fails unexpectedly. How would you configure Ansible to continue running the playbook even if one of the tasks fails, ensuring that the rest of the tasks are executed?
-
A
Configuring Ansible to Continue After a Task Fails
To ensure a playbook continues after a task failure, useignore_errors:- - hosts: all
- tasks:
- - name: Run task that may fail
- ansible.builtin.command: /bin/false
- ignore_errors: yes
- - name: Run next task even if the previous one fails
- ansible.builtin.debug:
- msg: "This task runs even if the previous one fails."
-
B
This is a performance based question and not multiple choice. Therefore the answer is in #1. If you require additional explanation, please ask your question in the Q&A section.
Reveal correct answer
Correct answer: A
A.
In this playbook, the ignore_errors directive is used to ensure that Ansible continues executing subsequent tasks even if a specific task fails. This is particularly useful in scenarios where some failures are acceptable or expected, and you do not want the playbook to halt entirely. By setting ignore_errors: yes in a task, Ansible prevents a failure in that task from interrupting the overall execution flow, allowing the playbook to complete and execute all other tasks as planned.
Line-by-Line Explanation
- - hosts: all
Defines that the tasks will be executed on all hosts specified in the inventory.
- tasks:
Starts the list of tasks to be performed.
- - name: Run task that may fail
Specifies the first task, which is intentionally set up to fail.
- ansible.builtin.command: /bin/false
Executes the /bin/false command, which always returns a failure status (exit code 1).
- ignore_errors: yes
Ensures that the playbook will not stop if this task fails. Ansible will log the failure but proceed to subsequent tasks.
- - name: Run next task even if the previous one fails
Specifies the next task to be executed regardless of the outcome of the previous task.
- ansible.builtin.debug:
Uses the debug module to display a message for demonstration.
- msg: "This task runs even if the previous one fails."
Prints the message, confirming that the playbook continues execution.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
