DevOps Bootcamp · Lesson

The template Module & .j2 Files

Deploy a rendered file to a host.

Lesson 1 of 413 steps

The template Module & .j2 Files is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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.conf

The .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.conf

Where 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.yml

A 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 nginx

template 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. ✨

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “The template Module & .j2 Files” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “The template Module & .j2 Files”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “The template Module & .j2 Files”?

Deploy a rendered file to a host. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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

  1. The template Module & .j2 Files
  2. Variables & Expressions in Jinja2
  3. Loops & if Blocks in Templates
  4. Filters: default, join & upper
← Back to DevOps Bootcamp