EX294 Implement advanced Ansible automation Practice Question
Exhibit
- name: Check if package is installed command: rpm -q httpd register: result failed_when: result.rc != 0 and 'not installed' not in result.stderr changed_when: false
Refer to the exhibit. What is the purpose of the 'failed_when' condition?
⚠ Common exam trap
Many candidates assume 'failed_when' always causes failure when the condition is true, but they overlook that the condition is a logical AND of two parts, and the second part ('not installed' not in stderr) is a negative check that prevents failure when the expected error message appears.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
It fails the task only if the return code is non-zero and the error does not indicate 'not installed'.
The 'failed_when' condition in Ansible allows you to define custom failure criteria for a task. In the exhibit, the condition 'failed_when: result.rc != 0 and "not installed" not in result.stderr' means the task will only be marked as failed if the return code is non-zero AND the error message does not contain the string 'not installed'. This is useful when a command returns a non-zero exit code for expected reasons (e.g., package not found), and you want to treat that as a non-failure.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
It fails the task only if the return code is non-zero and the error does not indicate 'not installed'.
Why this is correct
The failed_when condition overrides Ansible's default failure detection, so the task fails only when the return code is non-zero and the error output does not contain 'not installed'. This prevents a benign absence from being treated as a genuine failure.
- ✗
It ensures the task never fails regardless of return code.
Why it's wrong here
failed_when overrides the default return-code failure test with a custom expression; it does not suppress failure universally, since a true expression still fails the task. It is tempting because ignoring a non-zero return code is a real requirement when a command legitimately exits non-zero, achievable by setting failed_when to false.
- ✗
It fails the task only if the package is installed.
Why it's wrong here
The condition as written inverts the logic: it would fail the task when the package is present, whereas failed_when should flag genuine failure conditions such as a non-zero command result. It tempts because failed_when does override default failure detection, but the correct use targets unsuccessful outcomes, not successful installation.
- ✗
It fails the task if the package is not installed.
Why it's wrong here
The condition only triggers failure when its expression evaluates true, so it cannot detect an uninstalled package unless the expression inspects package state, which the exhibit's return-code test does not. It is tempting because failed_when can gate on arbitrary facts, and asserting package presence would be a legitimate use in a compliance playbook.
Go deeper
Related to this question
About these practice questions
One of 392 original EX294 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This EX294 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX294 exam.