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.
playbooks/templates/fabric-report.md.j2open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Playbook | playbooks/report.yml, written for you: reads versions, then renders the template |
|---|---|
| Output | build/fabric-report.md |
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.
What are the parts of a template?
Jinja2 has three kinds of tag, and everything else is copied through as it is:
| Tag | Does | Example |
|---|---|---|
{{ ... }} | print a value | {{ sw }} |
{% ... %} | logic: loops, ifs, setting a variable | {% for sw in groups['fabric'] %} … {% endfor %} |
{# ... #} | a comment, not printed | the 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 calledsw: itslocation, itsinterfaces, and thesw_versionthatreport.ymlsaved.
The table
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 variableshfor short; print a row. The role is decided inline:'spine' if ... else 'leaf'.Run it with
make run T=4.2, then openbuild/fabric-report.md. Right-click its tab and choose Open Preview to see the table rendered.
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.2when 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
background: part 4, Templates