Jinja2 default filter supplies fallback for undefined variables

300-435 By islever Editorial Last updated

Construct advanced network configuration templates using Jinja2 constructs such as loops, conditionals, output modifiers, and filters

Short answer

A The Jinja2 `|default(...)` filter supplies a fallback value when the variable is undefined, as in `{{ var|default('x') }}`.

Question

Jinja2 default filter?
  1. Fallback if variable undefined Correct Answer
  2. Default route
  3. Default interface
  4. Error

Key Takeaways

The Jinja2 default filter substitutes a fallback value when a template variable is undefined, making option A the correct choice. The pipe form {{ var|default('x') }} renders 'x' only when var has no value, whereas default route and default interface are device configuration concepts. Option D describes the UndefinedError the filter is designed to prevent rather than the filter's behavior.

Expert Analysis

{{ var|default('x') }} provides fallback if var undefined.

The deciding mechanism is Jinja2's undefined-variable handling: when a name resolves to Undefined, rendering it either raises an error or, under the chosen policy, prints an empty string, and the default filter (aliased as d) intercepts that state by substituting its own argument. So option A, "Fallback if variable undefined," states exactly what |default(...) does, and it matches the worked example {{ var|default('x') }} where 'x' is the value that appears when var was never set.

The other three options fail on their own concrete wording. Option B, "Default route," is a routing concept — a prefix or interface statement such as ip route 0.0.0.0 0.0.0.0 — and has nothing to do with a template filter applied to a Python variable inside a Jinja2 expression. Option C, "Default interface," is likewise a device-level configuration object, not a filter name; nothing in {{ var|default('x') }} selects an interface. Option D, "Error," is the tempting distractor because undefined names can indeed error: with Jinja2's default Undefined class, printing {{ var }} when var is unset raises UndefinedError at render time, and strict mode makes it fail earlier. That is precisely the failure mode |default(...) exists to prevent, so "Error" describes what the filter avoids, not what it is.

One nuance this question relies on but does not state: default only substitutes when the value is undefined, not when it is defined-but-false. In Jinja2, {{ count|default(0) }} renders nothing useful if count is explicitly 0 or None only if None counts as undefined — the standard rule is that a defined falsy value such as 0, '' or False passes through unchanged unless you write {{ count|default(0, true) }}, the two-argument form that also replaces falsey values. Worked on this question's own entity, if a provisioning template rendered {{ mgmt_vrf|default('default') }} and the inventory supplied mgmt_vrf as an empty string, the empty string would be rendered; the fallback would only kick in when the key is absent from the variable namespace entirely. Exam-wise, the one-line signature {{ var|default('x') }} is the tell: the filter's argument is a literal fallback value, so the filter's job is substitution on undefined, which is option A.

For the 300-435 blueprint this sits under advanced Jinja2 template construction, where filters such as default, join, map and selectattr are the standard vocabulary; recognizing a filter by the pipe character | and its purpose is enough to eliminate the routing-flavored distractors B and C immediately.

Official Reference

https://jinja.palletsprojects.com/en/stable/templates/#default-filter

This link opens the official vendor documentation in a new tab.

Exam Strategy

When a question names a template filter, read the pipe expression itself as the evidence: |default('x') carries a literal argument, so its purpose is value substitution, not routing or interface configuration. Use the syntax to kill distractors that are domain nouns (default route, default interface) before evaluating semantics, and reserve the error-handling option for questions that explicitly ask what happens when the variable is missing and no filter is applied.

Practice All 300-435 Questions

Access 150 questions with complete answers and detailed explanations.

View Full 300-435 Practice Test →

← Back to 300-435 Study Guide