Skip to main content
Templating lets you separate fixed structure (the template) from changing content (the variables). For example, a company’s CEO may want to send a personalized invitation to every employee: the letter layout remains the same (the template) while names and family members vary (the variables). A templating engine takes a template plus a set of variables and renders one or many final outputs (one email or hundreds).
A dark presentation slide titled "Templating Engine" showing four colored
text boxes, each containing a sample invitation letter addressed to different
people. The letters have different colored monospaced text (yellow, orange,
blue, green) on black
panels.
In IT, templating is widely used to generate HTML pages, configuration files, or inputs for automation tools such as Ansible. Keep a template file and feed variables to produce valid output automatically.

Simple HTML templating example

Template:
Variables:
Rendered HTML:

Using Jinja2 in Ansible

Ansible uses Jinja2 templating everywhere: playbooks, task parameters, templates, and inventory. Example: using a variable for a file path in a task. Template playbook:
Variables:
Rendered task after substitution:
You can template configuration files too. Example MySQL configuration template and result. Template:
Variables:
Rendered configuration:

What is Jinja2?

Jinja2 is a full-featured, powerful templating engine for Python. It provides a compact syntax for:
  • Expressions that output values
  • Control structures (loops, conditionals)
  • Template inheritance (blocks)
  • Filters to transform values
  • Tests to check values (defined, none, etc.)
Use Jinja2 to create reusable templates for web pages, configuration files, cloud templates, and automation playbooks.

Common Jinja2 constructs

  • Expressions: {{ ... }} — evaluate and output a value.
  • Statements: {% ... %} — control structures like for/if or block definitions.
  • Comments: {# ... #} — template comments not rendered in output.

Blocks and template inheritance

Blocks let a base template define placeholders that child templates can override. Example:

Filters: transform values inline

Filters use the pipe (|) syntax and let you transform a variable before rendering.
  • Strings: upper, lower, title, replace, default
  • Sequences: min, max, unique, union, intersect, random, join
Examples:
Use filters to transform values inline. The default filter prevents undefined-variable errors by providing a fallback.
Table: selected filters, use cases, and examples | Filter | Use case | Example | | ------------------- | --------------------------------- | ---------------- | ----------------------------------- | | upper, lower, title | Normalize string case | {{ name | upper }} -> “BOND” | | replace | Replace substrings | {{ text | replace("a","b") }} | | default | Fallback when undefined | {{ value | default("N/A") }} | | join | Join list elements into a string | {{ words | join(" ") }} -> “The name is Bond” | | unique | Remove duplicates from a sequence | {{ [1,2,2] | unique }} -> [1,2] | | union, intersect | Combine or intersect sequences | {{ [1,2] | union([2,3]) }} -> [1,2,3] | | random | Pick a random element | {{ range(1,201) | random }} -> random 1–200 | Selected examples (sequence filters):

Blocks vs expressions

  • Use {{ ... }} when you want to output a value or the result of an expression.
  • Use {% ... %} for control flow, including loops, conditionals, and block definitions.

Loops and conditionals

Example template that prints a message for numbers 0–4 and highlights when the number equals 2:
Rendered output:

When to use Jinja2 templates

Next steps and references

Jinja2 provides many more filters, tests, and extension points. Combine filters, tests, template inheritance, and macros to build clean, maintainable templates for web apps, automation, and infrastructure. Links and references:

Watch Video