Courseiva

EX294 Transform data with filters and plugins Practice Question

A playbook uses the 'debug' module to print a variable 'my_var' but the output is 'VARIABLE IS UNDEFINED'. The variable is defined in group_vars/all.yml. Which filter could be used to provide a default value and avoid this error?

⚠ Common exam trap

Many candidates confuse the `default` filter with the `mandatory` filter, thinking that 'mandatory' provides a fallback, when in fact it enforces definition and causes an error if the variable is missing.

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

✓

{{ my_var | default('fallback') }}

The `default` filter in Ansible provides a fallback value when a variable is undefined, preventing the 'VARIABLE IS UNDEFINED' error. Using `{{ my_var | default('fallback') }}` ensures that if `my_var` is not defined in group_vars/all.yml or any other precedence level, the string 'fallback' is used instead of causing a 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.

  • ✗

    {{ my_var | mandatory }}

    Why it's wrong here

    mandatory raises an error when the variable is undefined rather than supplying a fallback, so it cannot prevent the failure. It is tempting because it is a genuine Jinja2 filter for enforcing that a variable must be defined, and would be correct when you want the play to fail loudly instead of silently using a default.

  • ✓

    {{ my_var | default('fallback') }}

    Why this is correct

    The default filter substitutes 'fallback' when my_var is undefined, preventing the undefined-variable error. It resolves the stem's problem because group_vars/all.yml evidently is not being loaded for this host, so the variable never reaches the template.

  • ✗

    {{ my_var | ternary('yes', 'no') }}

    Why it's wrong here

    The ternary filter selects between two values based on a condition; it does not supply a fallback when the variable is undefined, so the undefined error persists. It is tempting because ternary is genuinely used for conditional value selection, and would be correct when my_var is defined and you must choose between two outputs.

  • ✗

    {{ my_var | dflt('fallback') }}

    Why it's wrong here

    dflt is not a valid Ansible filter; the correct name is default (or its alias d), so this expression raises a template error rather than substituting a fallback. It is tempting because the abbreviation resembles the real filter, which would be correct when my_var may be undefined and a fallback value is required.

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 →

How Courseiva writes practice questions · Editorial policy

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.