Network automation lab 11 of 27

Jinja2 templates and a YAML source of truth

Generate complete router configurations from a YAML source of truth with Jinja2: loops, conditionals, whitespace control, built-in and custom filters, macros, template inheritance and strict undefined variables, then deploy them to three routers and prove OSPF and the intended path.

Run this lab

It needs Lite: its 4 nodes are set to 4 vCPU and 2,560 MB in total. That fits the free demo too.

In your workspace: Shared, then Network Automation - CCNA and CCNP Automation guided labs, then 11 - Jinja2 templates and a YAML source of truth

Try it free for 30 minutesRun it on Lite from £3 a day

The free demo needs no card and no email. Every lab plan includes every guided lab.

Level
Intermediate
Time
About 80 minutes
Nodes need
4 vCPU, 2,560 MB
Smallest plan
Lite

The scenario

Kestrel Freight is rolling out the same router design to dozens of depots, so nobody will type configurations by hand any more. Intent lives in one file, the source of truth sot.yaml: sites, NTP and syslog servers, loopbacks, links and OSPF settings. Jinja2 templates turn it into complete configurations. Today you'll build the templates for the northern triangle (r1, r2 at Leeds and r3 at York), generate the three configurations, deploy them, and prove the result, including a deliberate OSPF cost on r2 that should steer its traffic to r3 through r1.

RouterSiteLoopback0Links (from sot.yaml)
r1LDS Leeds10.255.0.1/32Gi0/1 10.1.12.1/30 → r2 · Gi0/2 10.1.13.1/30 → r3
r2LDS Leeds10.255.0.2/32Gi0/1 10.1.12.2/30 → r1 · Gi0/2 10.1.23.1/30 → r3 (OSPF cost 50)
r3YRK York10.255.0.3/32Gi0/1 10.1.13.2/30 → r1 · Gi0/2 10.1.23.2/30 → r2 · Gi0/3 spare, shut

The routers start with only their management address (Gi0/0, 10.10.20.11–13). Files in ~/lab: sot.yaml, templates/ (base.j2, macros.j2, router.j2, all TODOs), render.py (filters to write), deploy.py and check.py (ready). Login netops / NetOps-Lab1.

What you’ll practise

  • Render Jinja2 templates with variables, loops and conditionals, and control whitespace
  • Use built-in filters and write custom filters for IP address arithmetic
  • Structure templates with macros, includes and inheritance (extends and blocks)
  • Catch missing data with StrictUndefined instead of generating silently broken configuration
  • Build Day-0-style full configurations from a source of truth, deploy them and verify the result

The tasks

  1. First templates
  2. Loops, conditionals and whitespace
  3. Filters, including your own
  4. A macro for interfaces
  5. Template inheritance
  6. Make missing data fail loudly
  7. Render the configurations (Day 0)
  8. Deploy and prove the design

The Lab Tasks pane has the scenario, these tasks, the checks to run, hints and the full solution.

Topology and devices

The network as it opens in EVE-NG, drawn from the lab’s own canvas, and the image each node runs.

Topology of Jinja2 templates and a YAML source of truth4 nodes and 1 network, 7 links. Every link is listed as text below the diagram.devboxr1r2r3Mgmteth0Gi0/1Gi0/1Gi0/2Gi0/2Gi0/0Gi0/0Gi0/2Gi0/1Gi0/0
4 nodes and 1 network, with the interface names each link uses. On a narrow screen, scroll the diagram sideways.
Nodes in Jinja2 templates and a YAML source of truth, with the image each runs and the vCPU and RAM it’s set to
NodeDeviceImage in Add NodeSet to
devboxEVE-NG GUI server (Docker)eve-gui-server:latest1 vCPU, 1,024 MB
r1Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB
r2Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB
r3Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB

How to open this lab

  1. Sign in to your workspace at lab.cloudlabbox.com.

  2. In File Manager, open Shared, then the folder Network Automation - CCNA and CCNP Automation guided labs.

  3. Open 11 - Jinja2 templates and a YAML source of truth and start its nodes, then open Lab Tasks for the scenario and the steps.

The lab is read-only. You can start the nodes and configure the devices, but changes to the topology can’t be saved. To change it, rebuild it in your own folder.