The template Module & .j2 Files
Deploy a rendered file to a host.
The template Module & .j2 Files is a free Ansible Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Static Files Are Not Enough
The copy module ships a file unchanged. But config files often need a port or hostname that differs per server, so static copies fall short. 🤔
Enter the template Module
The template module renders a file through Jinja2 first, filling in variables, then copies the result to the host.
- name: Deploy config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.confThe .j2 Convention
Template source files end in .j2 to mark them as Jinja2. It is a convention, not a hard rule, but everyone follows it.
src and dest
The src points at the .j2 file on your control node, and dest is the final path on the managed host.
template:
src: app.conf.j2
dest: /etc/app/app.confWhere Templates Live
By default Ansible looks for templates in a templates directory next to your playbook or inside a role. No path prefix needed.
templates/
nginx.conf.j2
site.ymlA Minimal Template
Inside the .j2 file, plain text stays as is and {{ }} markers get replaced. Here a port variable becomes a real number.
listen {{ http_port }};
server_name {{ domain }};Set File Permissions
Like copy, template accepts owner, group and mode, so the rendered file lands with the right ownership.
template:
src: app.conf.j2
dest: /etc/app.conf
mode: "0644"Validate Before Writing
The validate option runs a check command on the rendered file before installing it, so a broken config never ships.
template:
src: sshd.j2
dest: /etc/ssh/sshd_config
validate: "sshd -t -f %s"Templates Are Idempotent
If the rendered output matches the file already on the host, template reports ok and changes nothing. Safe to run again and again.
Notify a Handler on Change
Pair template with notify so a service only restarts when the config file actually changed. A perfect match.
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginxtemplate vs copy
Reach for copy when a file is the same everywhere, and template when any part of it must vary by host.
Quick Check
You need a config file whose port differs per server. Which module fits?
Recap
You met the template module: it renders a .j2 file with Jinja2, drops it at dest, sets permissions and stays idempotent. ✨
Frequently asked questions
Is the “The template Module & .j2 Files” lesson free?
Yes — the full text of “The template Module & .j2 Files” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “The template Module & .j2 Files”?
Deploy a rendered file to a host. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The template Module & .j2 Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The template Module & .j2 Files
- Variables & Expressions in Jinja2
- Loops & if Blocks in Templates
- Filters: default, join & upper