EX294 Manage task execution and roles Practice Question
An administrator is writing a playbook that must execute a task only when a specific variable is defined. The variable may be set in the inventory, group_vars, or host_vars. Which conditional ensures the task runs only if the variable 'app_port' is defined?
⚠ Common exam trap
Many candidates confuse variable existence with truthiness or using an invalid test like 'is not undefined' or 'is exists'.
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
✓
when: app_port is defined
The 'is defined' test is the standard way to check if a variable exists in Ansible. It correctly handles variables set from any source and avoids errors when the variable is missing. The other options either test truthiness, use invalid syntax, or confuse file existence with variable definition. Only the correct option reliably ensures the task runs when 'app_port' is defined.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
when: app_port is exists
Why it's wrong here
There is no 'exists' test in Ansible for variable existence. The 'exists' test is used for file paths (e.g., 'path is exists'), not variables. Using it here would result in an error because the test is not recognized. This option confuses file existence tests with variable definition checks, and thus does not work.
- ✗
when: app_port is not undefined
Why it's wrong here
The correct Ansible test for checking if a variable is defined is 'is defined'. 'is not undefined' is not a valid Jinja2 test in Ansible; it will cause a template error. Ansible does not provide an 'undefined' test, so this condition is syntactically invalid and will fail at runtime rather than achieving the desired conditional execution.
- ✗
when: app_port
Why it's wrong here
Using 'when: app_port' evaluates the variable's truthiness, not its existence. If 'app_port' is set to 0, false, or an empty string, the condition fails even though the variable is defined. This does not reliably meet the requirement to run only when the variable is defined, and it may also raise an error if the variable is undefined.
- ✓
when: app_port is defined
Why this is correct
The 'is defined' test is the correct Ansible conditional to check if a variable exists in the current host's context. It evaluates to true if 'app_port' is set from any source, including inventory, group_vars, or host_vars. This directly satisfies the requirement without erroring if the variable is missing, unlike a direct truthiness check.
Go deeper
Related to this question
About these practice questions
This EX294 question is part of Courseiva's 392-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Red Hat exam blueprint
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.