online posts 16
ansible 101 for networking

task 4.2 · chapter 4 of 8: loops, conditionals, templates · 25 min

A report from a template

objective

Finish a Jinja2 template that renders a Markdown report of the whole fabric: every switch, its version and location, and its ports with the uplinks marked.

you edit, in the labplaybooks/templates/fabric-report.md.j2

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
Playbookplaybooks/report.yml, written for you: reads versions, then renders the template
Outputbuild/fabric-report.md
1 explain

What is a Jinja2 template?

A template is a text file with gaps, and Ansible fills the gaps from variables. Ansible’s templates use Jinja2, the same language as the {{ }} you’ve been writing in playbooks. A template can make any text: a config file, an email, or here, a Markdown report of the fabric that you could paste into a wiki.

playbooks/report.yml is already written. It gathers every switch’s facts and keeps its version, then renders templates/fabric-report.md.j2 once, on this machine, into build/fabric-report.md. Your job is the template.

2 show

What are the parts of a template?

Jinja2 has three kinds of tag, and everything else is copied through as it is:

TagDoesExample
{{ ... }}print a value{{ sw }}
{% ... %}logic: loops, ifs, setting a variable{% for sw in groups['fabric'] %} … {% endfor %}
{# ... #}a comment, not printedthe TODOs in the file

Inside a template, two things let you see the whole fabric at once, not just one switch:

  • groups['fabric']: the list of host names in the group.
  • hostvars[sw]: every variable of the host called sw: its location, its interfaces, and the sw_version that report.yml saved.
3 follow along

The table

  1. Open the template. Replace the first {# TODO 4.2a ... #} comment with a loop that prints one table row per switch:

    editplaybooks/templates/fabric-report.md.j2Ctrl+P → fabric-report.md.j2
    | Switch | Role | Version | Location |
    |---|---|---|---|
    {% for sw in groups['fabric'] | sort %}
    {% set h = hostvars[sw] %}
    | {{ sw }} | {{ 'spine' if sw in groups['spines'] else 'leaf' }} | {{ h.sw_version }} | {{ h.location }} |
    {% endfor %}
    

    Line by line: for each switch, in alphabetical order (| sort); call its variables h for short; print a row. The role is decided inline: 'spine' if ... else 'leaf'.

  2. Run it with make run T=4.2, then open build/fabric-report.md. Right-click its tab and choose Open Preview to see the table rendered.

4 challenge

The ports

Replace the second TODO with a section that has, for every switch, a heading and one bullet per interface, and **(uplink)** after the uplinks:

### leaf1

- `GigabitEthernet1` to spine1 Gi1 **(uplink)**
- `GigabitEthernet2` host1
hint

That’s a loop inside a loop: switches on the outside, hostvars[sw].interfaces on the inside.

hint

{% if i.uplink | default(false) %} **(uplink)**{% endif %} adds the marker on the same line. Ansible removes the newline after each {% %} tag, so if two lines run together, add an empty line after the tag.

show the solutionplaybooks/templates/fabric-report.md.j2

or pull it into your lab: make solve T=4.2, then git diff --staged shows what changed

{% for sw in groups['fabric'] | sort %}
### {{ sw }}

{% for i in hostvars[sw].interfaces %}
- `{{ i.name }}` {{ i.description }}{% if i.uplink | default(false) %} **(uplink)**{% endif %}

{% endfor %}

{% endfor %}

5 check: run it, then prove it

$ make run T=4.2
$ make check T=4.2

when it's right, you'll see

╭─ task 4.2 · a report from a template
│  ✓ playbooks/report.yml runs cleanly
│  ✓ build/fabric-report.md exists
│  ✓ the report has a row for spine1 with its version
│  ✓ the report has a row for leaf1 with its version
│  ✓ the report has a row for leaf2 with its version
│  ✓ the challenge: uplinks are marked (the `if` part)
╰─ 6/6 checks passed  verified