CertGrid CertGrid
Hands-on Lab·Ansible

Ansible Templates with Jinja2

A template is a configuration file with the machine-specific parts left as expressions, and it is the single most useful thing Ansible does. This builds one that uses every feature you actually need - variables, facts, arithmetic, a for loop, an if - deploys it with validation and a backup, and finishes with the filters worth memorising.

Templates, Files and Vault Guide 18 of 45 Intermediate

Written against the versions above. `template` renders on the **control node** and copies the result, so the managed node never needs Jinja2 or the variables. It is `copy` with a render step, and it takes the same `owner`, `group`, `mode`, `backup` and `validate` parameters.

One managed node. The rendered values differ per host, which is the point - a second host would produce a second file with its own IP and memory.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB
ANS-A01192.168.0.37Ubuntu 26.04 LTSManaged Node (group: web)2 Core3 GB50 GB

Before you start

  1. The configuration this guide assumes

  2. A template is a config file with holes in it

  3. Render it

  4. Running it again changes nothing

  5. Change one variable and diff it

  6. The filters that earn their keep

Official sources