Filters and Tests¶
Section status: outline
This page is scoped but not yet written in full prose. The sections below define what it will cover.
Why This Exists¶
Filters transform a value (| default, | join); tests answer a yes/no question about a value (is defined, is failed) — together they're most of what makes a Jinja2 expression in a real playbook longer than a bare variable name.
What It Will Cover¶
| default('fallback')and| default([], true)(the second argument treats an empty string/list as undefined too)| join(', '),| length,| unique,| sort| dict2items/| items2dictfor looping over dictionaries withloop| to_json/| from_json,| to_yaml/| from_yaml| select/| selectattr/| mapfor filtering and projecting lists of dictionaries- Tests:
is defined,is undefined,is none,is failed,is success,is changed - The difference between a filter (
|) and a test (is) syntactically and conceptually
Common Mistakes¶
- Using
| default('')and still hitting an "undefined variable" error because the variable is genuinely undefined in a nested lookup Jinja2 evaluates before the filter applies. - Confusing
is defined(variable exists) with a falsy-value check — an empty string or0is still "defined."
Interview Questions¶
- What does
| default([], true)do differently from plain| default([])? - How would you filter a list of dictionaries down to only the ones matching a condition, without a full
forloop?
Next¶
Continue to Templates for Config Generation.