Pcornel
Automation · Ansible

Templates

Cheat sheet · https://pcornel.dk/topic.php?topic=5405

Back to the Topic

Answer

Ansible templates use Jinja2 to build configuration files from variables before copying them to the target with the template module. Check the rendered file on the target, not just the template source, when a service misbehaves.

Remember

  • Template source looks correct ≠ rendered output correct; variable precedence and conditionals alter content.
  • Rendered file delivered ≠ service accepted it; syntax validation/reload may still fail.
  • No task change ≠ template source unchanged; current rendered content may coincidentally match.
  • Template output ≠ secret-safe automatically; sensitive values can be materialized on disk/logs.

First checks

  • ansible-playbook site.yml --check --diff — template diff
  • template module with validate: — syntax check
  • backup: yes for important files — recovery

Verify in this order

vars + .j2 → rendered file → validate → handler

Where it usually breaks

Successful template deployment proves rendered content reached its destination; it does not prove the consuming service parsed, reloaded or used it successfully.

Inventory/vars → play/task selection → connection/become → module request → module result → target object read-back → handler/service effective state → workload verification.

Templates Ansible boundary map

BoundaryHealthy evidenceSuspicious evidence
Inputs / selectionCorrect inventory, hosts, variables/facts and content are explicitWrong scope, stale facts, hidden precedence or content-version drift
ExecutionConnection, privilege, module/task context and ordering match intentUnreachable, wrong execution host, privilege/plugin failure or misleading skip/change state
Result semanticsPer-host/item results are preserved and interpreted correctlyAggregate recap hides partial/skipped/handled failures
Target proofResource/workload read-back and rerun confirm convergenceGreen Ansible status treated as end-to-end acceptance

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=5405

Confirmation

Confirm action

Continue?