Use this when transforming values in templates.
Filters
Template filters transform values during rendering, such as {{ name | upper }}, {{ list | join(',') }} or ipaddr filters for subnets in Ansible.
1. Filters transform values (default, join, to_nice_yaml, ipaddr).
2. Use built-ins before custom code.
3. Test custom filters.
4. Chain readably.
5. Check collection requirements.
value → filters → transformed value
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as undefined values, unsafe escaping, implicit type conversion, environment-specific filters, secret exposure, whitespace-sensitive syntax and valid render producing invalid target configuration, choosing the cases most relevant to filter input/output types, undefined/null handling, ordering/composition and target-specific escaping after transformation.
Step 2 Common mistakes Narrow the fault
- Filters missing because the collection is not installed.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Filters transform values: upper, join, default, ipaddr.
- Filters can be chained.
- Test outputs.
{{ name | upper }} / {{ ip | ansible.utils.ipaddr('address') }}— filtersansible-doc -t filter -l | head— available filters- Custom filters tested — quality
ReferenceWhy it matters1 section · depth on demand
What it is
Filters transform values in templates: default, upper, join, ipaddr (ansible.utils), to_nice_yaml.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Filters transform values in Jinja2 templates: a value is piped into a filter with optional arguments, and filters can be chained left to right. They keep formatting logic out of the data.
{{ mtu | default(1500) }} {# fallback value #}
{{ site | upper }} {# OSLO #}
{{ vlans | unique | sort | join(',') }} {# 10,20,30 #}
{{ intfs | selectattr('enabled') | list }} {# only enabled interfaces #}
{{ intfs | map(attribute='name') | list }} {# list of names #}
{{ '10.0.0.1/24' | ansible.utils.ipaddr('netmask') }} {# 255.255.255.0 #}
The most used filters are default, case filters, join, sort/unique, selectattr/rejectattr and map. Custom filters — Python functions registered in the environment or as an Ansible filter plugin — are better than complex logic inside templates, because they can be unit-tested.
Example
{{ vlans | map(attribute='id') | join(',') }}
{{ '10.0.0.1/24' | ansible.utils.ipaddr('address') }}
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Templates > Filters. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For filter input/output types, undefined/null handling, ordering/composition and target-specific escaping after transformation, prefer typed fields and stable IDs over human presentation text.