How does the ansible.builtin.template module deploy a template, and where is it rendered?
src names the template on the control node, dest the file on the managed host; Ansible renders the template on the control node with the host's variables and facts, then transfers the result like copy.
* Rendered on the control node, once per host, then copied like any file. *
- name: write hostname using jinja2
ansible.builtin.template:
src: templates/test.j2
dest: /tmp/hostname
with templates/test.j2:
My name is {{ ansible_facts['hostname'] }}
On host web1 the file /tmp/hostname contains My name is web1.
Rendering happens on the control node, so the managed hosts need neither Jinja2 nor the template file. For each host, Ansible renders a separate result (that host's facts, that host's variables) and then behaves like copy: compare checksums, replace the file only if the content differs, report changed or ok. That is why a template task can notify a restart handler that fires only when the rendered config actually changed.
Go deeper:
ansible.builtin.template module (official docs) โ
src,dest,backupand the rendering options.