LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

Where do the values in a template come from, and where do template files usually live?

From any variable available to the task (play vars, inventory, vars_files, registered results) and from the host's facts. Templates are usually kept in the project's templates/ directory with a .j2 extension, which is a convention, not a requirement.

A template consists of three parts: plain data (text copied as is), variables ({{ ssh_port }}) and expressions ({{ users | length }}, {% if ... %}). Rendering replaces variables and expressions by their values for the host being configured.

Because the template is rendered per host, host-specific sources work naturally: host_vars, group_vars and especially facts such as ansible_facts['fqdn'] all resolve to that host's values.

Conventions that help:

  • templates/ next to the playbook (or inside a role): the template module looks there, so src: sshd_config.j2 is enough.
  • .j2 at the end of the name (sshd_config.j2) tells every reader that the file is not deployed as is. Jinja2 doesn't care about the extension.

Go deeper:

From Quiz: ITIA / Ansible: Managing Files and Jinja2 Templates | Updated: Nov 12, 2026