AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Jinja2 Filters: Transform Data in Playbooks (Complete Reference)

By Luca Berton · Published 2024-01-01 · Category: web-servers

Complete reference for Ansible Jinja2 filters. Transform strings, lists, dictionaries, JSON, YAML, IP addresses, hashes, and dates with practical playbook examples and cheat sheet.

Jinja2 filters transform data inline within Ansible playbooks. They convert types, format strings, filter lists, parse JSON/YAML, manipulate IPs, and more — all using the pipe (|) syntax.

String Filters

List Filters

Select & Reject Filters

Dictionary Filters

dict2items / items2dict

Type Conversion

Default Values

JSON Query (json_query)

IP Address Filters

Hash and Encryption

Path Filters

Ternary (Conditional)

FAQ

What are Ansible Jinja2 filters?

Filters transform data using the pipe (|) syntax in Ansible templates and playbooks. They convert types, format strings, filter lists, parse JSON/YAML, and more — executed inline without separate tasks.

What is the difference between select and selectattr?

select filters simple lists by value ([1,2,3] | select('greaterthan', 1)). selectattr filters lists of dictionaries by attribute (users | selectattr('active', 'equalto', true)).

How do I set a default value for undefined variables?

Use the default filter: {{ my_var | default('fallback') }}. Add true as second argument to also trigger default on empty values: {{ my_var | default('fallback', true) }}.

How do I parse JSON in Ansible?

Use from_json: {{ json_string | from_json }}. For querying complex JSON structures, use json_query with JMESPath expressions: {{ data | json_query('[?status==active].name') }}.

Conclusion

Most-used filters quick reference: • default() — Fallback for undefined variables • selectattr() / map() — Filter and transform lists of dicts • to_json / from_json — Serialize/parse JSON • regex_replace() / regex_search() — Pattern matching • combine() — Merge dictionaries • json_query() — JMESPath queries on complex data

Related ArticlesAnsible map Filter: Transform ListsAnsible regex_replace: Search & ReplaceAnsible default() Filter: Set Fallback ValuesAnsible Variable Precedence GuideAnsible regex_search Filter GuideCan You Use assert in Jinja Templates?Ansible Compare Lists: difference, intersect, union

Category: web-servers

Browse all Ansible tutorials · AnsiblePilot Home