NetAuto Academy

Configuration & Template Automation

Jinja2 Fundamentals Through Network Examples

Intermediate12 min read

What you'll learn

  • What Jinja2 is, and why it's the tool this module builds on
  • pip install jinja2 — this module's one new dependency
  • {{ }} expression syntax, and how it maps to values in a Python dict
  • How to render a template string against real device data
🎓 Teach This Lesson

Listen to a guided explanation of this lesson.

Spoken narration isn't supported in this browser.

What Jinja2 actually is

Jinja2 is a templating engine: it takes a piece of text with placeholders in it, plus a source of real values, and produces the text with those placeholders filled in. That's it — no networking-specific magic. What makes it worth a whole module is that this exact mechanism is what Ansible, NAPALM tooling, and most real network automation configuration generation is built on.

This curriculum's second external package

Netmiko was the first external package this curriculum introduced (pip install netmiko, in "Netmiko Fundamentals"). Jinja2 is the second — pip install jinja2, run by you, the learner, exactly the same way. Like Netmiko, it's never part of this website's own code; it's a tool your own Python scripts use.

Quick checkMyth check
Is Jinja2 specific to network automation?

Your first template

first_render.py
from jinja2 import Template

device = {
  "hostname": "SW1",
  "description": "Uplink to core switch",
}

template = Template("""hostname {{ hostname }}
interface GigabitEthernet1/0/1
description {{ description }}""")

rendered = template.render(**device)
print(rendered)

Template(...) takes a string with {{ }} placeholders. .render(**device) unpacks the dict's keys as the values those placeholders look up.

Running this prints:

output
hostname SW1
interface GigabitEthernet1/0/1
description Uplink to core switch

Every {{ placeholder }} was replaced with the matching key's value from the device dict. Nothing else about the text changed.

This isn't a new idea — it's a new tool for one you already know

An f-string does the same substitution: f"hostname {device['hostname']}". The difference is audience. An f-string only runs inside Python. A Jinja2 template is a separate piece of text — it can live in its own file, be read by a non-programmer, and be reused by any script that imports it. Lesson 5 makes that separation real by loading a template from its own file.

Quick checkIdentify the correct code
Given device = {"hostname": "SW2"}, which correctly renders a template that uses {{ hostname }}?
Quick checkPredict output
Using the same template from above, what does template.render(hostname="SW2", description="Access port for Finance VLAN") print?

Device data → template → rendered configuration

  1. Device dataa Python dict — hostname, description, ...
  2. Jinja2 templatetext with {{ }} placeholders
  3. Rendered configurationreal, device-specific CLI text

This three-stage shape — data in, template applied, real text out — is the core mechanism every remaining lesson in this module builds on. Everything from here is about making the data side richer (Lesson 3) and the template side more capable (Lesson 4), not changing this fundamental shape.

Quick checkScenario
A template renders without raising any exception. Does that mean the rendered configuration is correct?

Key takeaways

  • Jinja2 is a general-purpose templating engine — this module's second external package after Netmiko, installed the same way (pip install jinja2, learner-side only).
  • {{ variable }} inside a template string gets replaced by a matching value passed to .render().
  • This is the same substitution an f-string does — the difference is a template is its own reusable, shareable piece of text, not Python-only code.
  • The core shape — device data → template → rendered configuration — is the foundation every later lesson in this module builds on.
  • A template rendering without an error is not the same as the rendered configuration being correct — a theme this module returns to directly in Lesson 5.