Developer and Text Tools

How to Test Jinja2 and Ansible Templates Without Running a Playbook

Render Jinja2 templates live with YAML or JSON variables, macros and includes, find the exact error line, and learn where a JavaScript engine differs.

Saizul Amin
Saizul Amin19 Nov 2026 · 4 min read
How to Test Jinja2 and Ansible Templates Without Running a Playbook

How to Test Jinja2 and Ansible Templates Without Running a Playbook

If you write Ansible roles, you know the cycle: change a .j2 template, run the playbook against a test host, wait for facts to gather, and read a traceback that points somewhere near the problem. Jinja2 is also behind Salt, dbt, many Python web apps and Home Assistant, and the cycle is similar everywhere. The free Jinja2 Template Tester shortens it to seconds: you edit the template and the variables side by side and watch the output change.

What the tester runs

Templates are rendered with nunjucks 3.2.4, an open-source JavaScript engine designed after Jinja2, with its Jinja compatibility switched on. That mode adds Python habits such as True, False and None, slices like items[1:3], and string methods such as upper(), split() and startswith(). The engine is downloaded once, about 26 KB, when you press the load button, and runs in the page. On top of it the tester registers the Ansible and Jinja filters people use daily, from to_nice_yaml and combine to regex_replace, dict2items, selectattr with tests and map with an attribute.

Being honest about the engine matters, so the page lists the differences we know: no do or with statements, no break and continue, no loop.cycle or previtem, no Python percent formatting or str.format(), and a few missing filters. A small linter scans your files and warns when it sees one of these, so you know which parts to confirm with the Python version.

Step 1: set up the files

Real roles rarely use a single template. A configuration file imports macros, a page extends a base layout, a partial is included in several places. Use + File to add tabs with the names your template refers to, such as macros.j2 or base.html, and Make main to mark the file to render. Import, include and extends then find the other tabs by name. You can also open existing files from disk.

Step 2: paste the variables

The variables tab accepts YAML or JSON. For Ansible, paste the contents of group_vars or host_vars directly, including lists of dictionaries written in flow style. A YAML mistake, such as a tab or a missing space after a colon, is reported with its line before rendering starts, so you do not chase a template error that is really a data error.

Ansible adds facts and magic variables at run time. The tester does not gather facts, so add the few your template needs by hand, for example ansible_hostname or ansible_default_ipv4.address.

Step 3: match the environment options

Whitespace is where templates surprise people most. Ansible renders with trim_blocks on, which removes the first newline after a block tag such as endif. lstrip_blocks removes the spaces before a block tag on its line. Switch both to match your tool, and the output lines up as it will on the server. Turn StrictUndefined on to make any missing variable an error with its file and line, which is exactly how Ansible reports a typo in a variable name.

Step 4: read the output and the errors

The output updates a quarter of a second after you stop typing. If rendering fails, the error box names the file, line and column, and the line number is highlighted in red in the editor. Typical problems are an unclosed block, a filter used with the wrong arguments or a variable that does not exist.

A worked example

Take an nginx.conf.j2 with an upstream block built from a list of servers, one of them marked disabled. A for loop over upstreams | rejectattr('disabled') prints only the active servers. An if enable_tls block adds the listen 443 ssl line. A macro in macros.j2 writes one add_header line per entry in a dictionary, sorted with dictsort. A comment at the end shows upstreams | map(attribute='name') | join(', ') and the length of the list. Change a weight in the YAML and the server line changes as you type; misspell http_port with StrictUndefined on and the render stops at the listen line.

Filters that save time

  • combine merges dictionaries, with recursive=true for nested defaults.
  • dict2items and items2dict switch between dictionaries and lists for loops.
  • selectattr and rejectattr filter lists of dictionaries by an attribute and a test such as equalto or defined.
  • to_nice_yaml and to_nice_json print data structures readably inside configuration files.
  • regex_replace and regex_search handle the string surgery that otherwise needs Python.
  • ternary and default keep conditional values short.

Limits

Lookups that read files, password_hash, hash and other filters that need the operating system cannot run in a browser and stop with a clear message. Number handling follows JavaScript. For anything the linter flags, and for every production template at least once, render with the Python Jinja2 version your tools use. As a daily scratch pad for writing and debugging templates, the tester removes most of the waiting.

Save and share

Save the project to keep all tabs, the variables and the options together, export it as JSON for a teammate, or print the template with its output for a review.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages