<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Jinja2 · Network Doodles</title><link>https://networkdoodles.com/tags/jinja2/</link><description>Network automation, from first playbook to agentic operations. Ansible, NetBox, MCP, Python and the occasional doodle.</description><language>en-us</language><managingEditor>David Henderson</managingEditor><lastBuildDate>Mon, 16 Nov 2026 09:00:00 -0500</lastBuildDate><atom:link href="https://networkdoodles.com/tags/jinja2/index.xml" rel="self" type="application/rss+xml"/><item><title>Ansible Loops, when and Jinja2 Templates for Network Config (Ansible 101 for Networking, Lesson 4)</title><link>https://networkdoodles.com/lessons/ansible-101/loops-and-templates/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/ansible-101/loops-and-templates/</guid><category>ansible</category><category>network-automation</category><category>automation</category><category>jinja2</category><category>capeos</category><category>howto</category><description>Configure every port on every switch from a data list with loop, treat uplinks differently with when, and render a fabric report with Jinja2 templates.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>So far every task in this course has set one thing: one banner, one list of NTP servers, one location. Real network
config isn&rsquo;t like that. A switch has forty-eight ports, and you want every one of them described, enabled and
sized correctly, from data, without forty-eight tasks.</p>
<p>This lesson covers the three tools that make that possible: <strong>loops</strong> to repeat a task over a list,
<strong>conditionals</strong> to skip the items that don&rsquo;t apply, and <strong>templates</strong> to turn data into any text you like.</p>
<hr>
<h2 id="ports-as-data">Ports as data</h2>
<p>Start with the data. Each switch&rsquo;s ports go in its host file, next to the location from
<a href="/lessons/ansible-101/variables-and-facts/">lesson 3</a>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="c"># inventory/host_vars/leaf1.yml</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">location</span><span class="p">:</span><span class="w"> </span><span class="l">Row A, rack 2</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">interfaces</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- {<span class="w"> </span><span class="nt">name: GigabitEthernet1, description</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;to spine1 Gi1&#34;</span><span class="nt">, uplink</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w"> </span>}<span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- {<span class="w"> </span><span class="nt">name: GigabitEthernet2, description</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;host1&#34;</span><span class="w"> </span>}<span class="w">
</span></span></span></code></pre></div><p>Each port is a small dictionary: a name, a description, and, for uplinks, <code>uplink: true</code>. The <code>{ }</code> form is
just compact YAML for a dictionary on one line; it reads better than three lines per port when you have
dozens.</p>
<p>This list is now the definition of leaf1&rsquo;s ports. Want to add a port? Add a line. Want to know what a port is
for? Read the file. The playbook doesn&rsquo;t change.</p>
<hr>
<h2 id="loop-one-task-every-port">loop: one task, every port</h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Description and no shutdown, one port at a time</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">cisco.ios.ios_interfaces</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">config</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ item.name }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">            </span><span class="nt">description</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ item.description }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">            </span><span class="nt">enabled</span><span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">loop</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ interfaces }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">loop_control</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">label</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ item.name }}&#34;</span><span class="w">
</span></span></span></code></pre></div><p><code>loop</code> runs the task once per entry in the list, and each time <strong><code>item</code></strong> is the current entry. So
<code>{{ item.name }}</code> is <code>GigabitEthernet1</code> the first time round and <code>GigabitEthernet2</code> the second.</p>
<p><code>loop_control: label</code> controls what Ansible prints for each item. Without it, Ansible prints the entire
dictionary for every iteration, which gets unreadable fast. With it, each port gets one short line, like
<code>changed: [leaf1] =&gt; (item=GigabitEthernet1)</code>.</p>
<p>Each switch loops over <em>its own</em> <code>interfaces</code>, because the variable comes from <code>host_vars</code>. One task, three
switches, six ports, zero hostnames in the playbook.</p>
<h3 id="why-a-resource-module-for-ports">Why a resource module for ports</h3>
<p>The task uses <code>cisco.ios.ios_interfaces</code>, not the <code>ios_config</code> lines from lesson 2, and <code>enabled: true</code> is the
reason. A port that&rsquo;s up shows <strong>no</strong> <code>no shutdown</code> line in the running config: &ldquo;not shut down&rdquo; is the
default, and defaults don&rsquo;t print. So <code>ios_config</code> with <code>no shutdown</code> in its lines would never find that line,
and would send it on every run. <code>ios_interfaces</code> is a resource module: it reads each interface&rsquo;s config into
data (description, enabled, MTU), compares that with what you asked for, and knows that <code>enabled: true</code> means
&ldquo;there should be no <code>shutdown</code> line&rdquo;. You describe the port; it works out the commands.</p>
<p>A note on performance: a loop makes one change per item, which means a round trip to the switch per port.
For a lab, or a handful of ports, that&rsquo;s perfect, because you can see exactly which port changed. For 48 ports
on 200 switches, you&rsquo;d hand <code>ios_interfaces</code> the whole list in one <code>config:</code> instead. Get it right first; make
it fast when it matters.</p>
<hr>
<h2 id="when-not-every-item-applies">when: not every item applies</h2>
<p>Uplinks get a jumbo MTU; host ports don&rsquo;t. That&rsquo;s a <strong>conditional</strong>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Jumbo MTU on uplinks only</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">cisco.ios.ios_interfaces</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">config</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ item.name }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">            </span><span class="nt">mtu</span><span class="p">:</span><span class="w"> </span><span class="m">9100</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">loop</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ interfaces }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">loop_control</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">label</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ item.name }}&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">when</span><span class="p">:</span><span class="w"> </span><span class="l">item.uplink | default(false)</span><span class="w">
</span></span></span></code></pre></div><p><code>when</code> is evaluated for each item. If it&rsquo;s true, the task runs for that item; if not, Ansible skips it and says
so. On leaf1, <code>GigabitEthernet1</code> (the uplink) gets the MTU and <code>GigabitEthernet2</code> (host1&rsquo;s port) shows
<code>skipping</code>. On spine1 both ports are uplinks, so nothing is skipped.</p>
<p>Two details worth copying:</p>
<ul>
<li><strong>No <code>{{ }}</code> in <code>when</code>.</strong> It&rsquo;s already an expression. Writing <code>when: &quot;{{ item.uplink }}&quot;</code> works but earns a
warning, and it&rsquo;s a sign someone copied from an old blog post.</li>
<li><strong><code>| default(false)</code></strong> handles the ports that don&rsquo;t have an <code>uplink</code> key at all. Without it, the first host
port fails with &ldquo;&lsquo;dict object&rsquo; has no attribute &lsquo;uplink&rsquo;&rdquo;. Defaults on optional keys are what keep your data
files short.</li>
</ul>
<p><code>when</code> works on whole tasks too, not just loop items. <code>when: inventory_hostname in groups['spines']</code> runs a
task on the spines only; that&rsquo;s often cleaner than writing a separate play.</p>

<figure class="netsim" data-netsim tabindex="0" aria-label="leaf1&#39;s two ports as a list in host_vars: one loop task configures each port in turn, a when condition gives the jumbo MTU to the uplink and skips the host port, and a template turns the same data into the fabric report. Use the buttons, or the left and right arrow keys, to step through it." data-autoplay="false">
  <svg class="ns-svg" role="img" aria-label="leaf1&#39;s two ports as a list in host_vars: one loop task configures each port in turn, a when condition gives the jumbo MTU to the uplink and skips the host port, and a template turns the same data into the fabric report"></svg>
  <div class="ns-ctl">
    <button class="icon-btn" type="button" data-ns="restart" aria-label="Back to the first step" title="Start again"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 5v14M19 5l-9 7 9 7z"/></svg>
</button>
    <button class="icon-btn" type="button" data-ns="prev" aria-label="Previous step" title="Previous step"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5M11 6l-6 6 6 6"/></svg>
</button>
    <button class="btn sm primary ns-play" type="button" data-ns="play" aria-pressed="false"><span data-ns-play>Play</span></button>
    <button class="icon-btn" type="button" data-ns="next" aria-label="Next step" title="Next step"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
</button>
    <button class="icon-btn" type="button" data-ns="again" aria-label="Play this step again" title="Play this step again"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 12a8 8 0 1 0 2.5-5.8M4 4v4h4"/></svg>
</button>
    <span class="ns-count mono" data-ns-count></span>
    <button class="btn sm ns-speed" type="button" data-ns="speed" aria-pressed="false">Slow: off</button>
  </div>
  <div class="ns-dots" data-ns-dots role="group" aria-label="Jump to a step"></div>
  <figcaption class="ns-caption" data-ns-caption aria-live="polite"></figcaption>
  <div class="ns-panels">
    <div class="ns-codebox" data-ns-code hidden></div>
    <div class="ns-frame" data-ns-frame hidden></div>
    <div class="ns-tablebox" data-ns-table hidden></div>
  </div>
  <script type="application/json">{"h":340,"links":[],"narrow":{"at":{"data":[80,60],"gi1":[180,190,300],"gi2":[180,270,300],"leaf1":[270,60],"rep":[180,370]},"h":450,"w":360},"nodes":{"data":{"kind":"file","label":"leaf1.yml","sub":"host_vars/","x":90,"y":160},"gi1":{"h":44,"kind":"box","label":"GigabitEthernet1","sub":"to spine1 Gi1 · uplink","w":230,"x":340,"y":100},"gi2":{"h":44,"kind":"box","label":"GigabitEthernet2","sub":"host1","w":230,"x":340,"y":220},"leaf1":{"kind":"switch","label":"leaf1","sub":"Row A, rack 2","x":640,"y":110},"rep":{"hidden":true,"kind":"file","label":"fabric-report.md","sub":"build/","x":640,"y":260}},"steps":[{"code":{"lines":["interfaces:","  - { name: GigabitEthernet1, description: \"to spine1 Gi1\", uplink: true }","  - { name: GigabitEthernet2, description: \"host1\" }"],"title":"inventory/host_vars/leaf1.yml"},"hot":["data","gi1","gi2"],"text":"leaf1's ports are a list in its host file: a name, a description, and `uplink: true` on the uplink.","title":"The ports are data."},{"code":{"hl":[3,6],"lines":["- name: Description and no shutdown, one port at a time","  cisco.ios.ios_interfaces:","    config:","      - name: \"{{ item.name }}\"","        description: \"{{ item.description }}\"","        enabled: true","  loop: \"{{ interfaces }}\""],"title":"one task"},"dim":["gi2"],"hot":["gi1"],"packets":[{"label":"item 1","path":["gi1","leaf1"]}],"table":{"cols":["item","description","jumbo MTU"],"hl":0,"rows":[["GigabitEthernet1","to spine1 Gi1",""],["GigabitEthernet2","",""]],"title":"ios_interfaces on leaf1, per item"},"text":"`item` is the current entry, so `{{ item.name }}` is `GigabitEthernet1` the first time round.","title":"loop: the first item."},{"dim":["gi1"],"hot":["gi2"],"packets":[{"label":"item 2","path":["gi2","leaf1"]}],"table":{"cols":["item","description","jumbo MTU"],"hl":1,"rows":[["GigabitEthernet1","to spine1 Gi1",""],["GigabitEthernet2","host1",""]],"title":"ios_interfaces on leaf1, per item"},"text":"Second time round, `item` is GigabitEthernet2. One task, every port, no port names in the playbook.","title":"The same task, the next item."},{"code":{"hl":[6],"lines":["- name: Jumbo MTU on uplinks only","  cisco.ios.ios_interfaces:","    config:","      - name: \"{{ item.name }}\"","        mtu: 9100","  loop: \"{{ interfaces }}\"","  when: item.uplink | default(false)"],"title":"the MTU task"},"hot":["gi1"],"packets":[{"label":"mtu 9100","path":["gi1","leaf1"]},{"end":"drop","label":"skipping","path":["gi2","leaf1"]}],"table":{"cols":["item","description","jumbo MTU"],"rows":[["GigabitEthernet1","to spine1 Gi1","9100"],["GigabitEthernet2","host1","skipping"]],"title":"ios_interfaces on leaf1, per item"},"text":"The MTU task loops again. `when: item.uplink | default(false)` is true for Gi1 and false for Gi2, which shows `skipping`.","title":"when: the uplink only."},{"code":{"hl":[0,2],"lines":["{% 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 %}"],"title":"fabric-report.md.j2"},"dim":["leaf1"],"hot":["rep"],"packets":[{"c":"green","label":"template","path":["data","rep"]}],"show":["rep"],"table":{"cols":["Switch","Role","Version","Location"],"rows":[["leaf1","leaf","0.1.2","Row A, rack 2"],["leaf2","leaf","0.1.2","Row A, rack 3"],["spine1","spine","0.1.2","Row A, rack 1"]],"title":"build/fabric-report.md"},"text":"A template loops over `groups['fabric']` and reads each switch's `hostvars`. Out comes Markdown you could paste into a wiki.","title":"The same data, into a report."}],"w":760}</script>
</figure>
<script src="/js/netsim.c40a98d103487ae5866edfa12abb4afc1d3aa7ad2d12fed4fe925fe00b588106.js" integrity="sha256-xAqY0QNIeuWGbt&#43;hKrtK/B06p60tEv7U/pJf4AtYgQY=" defer></script>

<hr>
<h2 id="templates-data-into-any-text">Templates: data into any text</h2>
<p>Loops and conditionals produce config. <strong>Templates</strong> produce <em>anything</em>: a config file for a device that
doesn&rsquo;t have an API, a CSV, an email body, or a report. Ansible uses Jinja2, the same <code>{{ }}</code> syntax you&rsquo;ve
been using, plus <code>{% %}</code> for logic.</p>
<p>Here&rsquo;s the lab&rsquo;s fabric report template:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-jinja" data-lang="jinja"><span class="line"><span class="cl"><span class="x">| Switch | Role | Version | Location |
</span></span></span><span class="line"><span class="cl"><span class="x">|---|---|---|---|
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">sw</span> <span class="k">in</span> <span class="nv">groups</span><span class="o">[</span><span class="s1">&#39;fabric&#39;</span><span class="o">]</span> <span class="o">|</span> <span class="nf">sort</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">h</span> <span class="o">=</span> <span class="nv">hostvars</span><span class="o">[</span><span class="nv">sw</span><span class="o">]</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">| </span><span class="cp">{{</span> <span class="nv">sw</span> <span class="cp">}}</span><span class="x"> | </span><span class="cp">{{</span> <span class="s1">&#39;spine&#39;</span> <span class="k">if</span> <span class="nv">sw</span> <span class="k">in</span> <span class="nv">groups</span><span class="o">[</span><span class="s1">&#39;spines&#39;</span><span class="o">]</span> <span class="k">else</span> <span class="s1">&#39;leaf&#39;</span> <span class="cp">}}</span><span class="x"> | </span><span class="cp">{{</span> <span class="nv">h.sw_version</span> <span class="cp">}}</span><span class="x"> | </span><span class="cp">{{</span> <span class="nv">h.location</span> <span class="cp">}}</span><span class="x"> |
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">## Ports
</span></span></span><span class="line"><span class="cl"><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">sw</span> <span class="k">in</span> <span class="nv">groups</span><span class="o">[</span><span class="s1">&#39;fabric&#39;</span><span class="o">]</span> <span class="o">|</span> <span class="nf">sort</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">### </span><span class="cp">{{</span> <span class="nv">sw</span> <span class="cp">}}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">i</span> <span class="k">in</span> <span class="nv">hostvars</span><span class="o">[</span><span class="nv">sw</span><span class="o">]</span><span class="nv">.interfaces</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">- `</span><span class="cp">{{</span> <span class="nv">i.name</span> <span class="cp">}}</span><span class="x">` </span><span class="cp">{{</span> <span class="nv">i.description</span> <span class="cp">}}{%</span> <span class="k">if</span> <span class="nv">i.uplink</span> <span class="o">|</span> <span class="nf">default</span><span class="o">(</span><span class="kp">false</span><span class="o">)</span> <span class="cp">%}</span><span class="x"> **(uplink)**</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x">
</span></span></span><span class="line"><span class="cl"><span class="x"></span><span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x">
</span></span></span></code></pre></div><p>The new ideas:</p>
<ul>
<li><strong><code>{% for %}</code> / <code>{% endfor %}</code></strong> loops, inside the template instead of on the task.</li>
<li><strong><code>{% if %}</code></strong> is the template&rsquo;s <code>when</code>.</li>
<li><strong><code>groups['fabric']</code></strong> is the list of every host in the group, and <strong><code>hostvars[sw]</code></strong> is any other host&rsquo;s
variables. That&rsquo;s what lets one template describe the whole fabric, not just the current switch.</li>
</ul>
<p>The playbook that renders it has two plays: the first runs <code>ios_facts</code> on every switch and keeps its version
(so <code>sw_version</code> exists in <code>hostvars</code>), the second runs once on <code>localhost</code> and writes the file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl">- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Write the report</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">hosts</span><span class="p">:</span><span class="w"> </span><span class="l">localhost</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">gather_facts</span><span class="p">:</span><span class="w"> </span><span class="kc">false</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">connection</span><span class="p">:</span><span class="w"> </span><span class="l">local</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">tasks</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="nt">name</span><span class="p">:</span><span class="w"> </span><span class="l">Render build/fabric-report.md</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span><span class="nt">ansible.builtin.template</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">src</span><span class="p">:</span><span class="w"> </span><span class="l">fabric-report.md.j2     </span><span class="w"> </span><span class="c"># Ansible looks in playbooks/templates/ for you</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">dest</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ playbook_dir }}/../build/fabric-report.md&#34;</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">        </span><span class="nt">mode</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;0644&#34;</span><span class="w">
</span></span></span></code></pre></div><p>The result is a Markdown file you could paste straight into a wiki:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">| Switch | Role | Version | Location |
</span></span><span class="line"><span class="cl">|---|---|---|---|
</span></span><span class="line"><span class="cl">| leaf1 | leaf | 0.1.2 | Row A, rack 2 |
</span></span><span class="line"><span class="cl">| leaf2 | leaf | 0.1.2 | Row A, rack 3 |
</span></span><span class="line"><span class="cl">| spine1 | spine | 0.1.2 | Row A, rack 1 |
</span></span></code></pre></div><hr>
<h2 id="where-people-get-it-wrong">Where people get it wrong</h2>
<h3 id="1-templates-full-of-logic">1. Templates full of logic</h3>
<p>A template with nested <code>if</code>s, string surgery and twenty <code>set</code> statements is a program written in the worst
language available. When a template gets clever, move the cleverness into the data (a better-shaped variable)
or into a filter, and keep the template mostly <code>{{ }}</code>.</p>
<h3 id="2-whitespace-surprises">2. Whitespace surprises</h3>
<p>Jinja2 in Ansible trims the newline after a <code>{% %}</code> tag, which is usually what you want and occasionally
eats a blank line you needed. My first version of that report ran every switch&rsquo;s heading straight into the
previous bullet list. The fix was one empty line after the inner <code>{% endfor %}</code>. When a template&rsquo;s output looks
squashed, look at the blank lines around your tags first.</p>
<h3 id="3-looping-where-one-call-would-do">3. Looping where one call would do</h3>
<p>Loops are the right first tool. But a loop over 48 ports is 48 round trips and 48 lines of output per switch.
When you know it works, build the list in one go (a <code>map</code> filter, or the data as it already is) and pass it in
one call.</p>
<h3 id="4-templating-whole-configs-when-a-module-would-do">4. Templating whole configs when a module would do</h3>
<p>It&rsquo;s tempting to render a switch&rsquo;s entire config from a template and push it. That&rsquo;s a real strategy (full
config replace), but it&rsquo;s a different one: you take on the diffing, the ordering and every default that does
or doesn&rsquo;t print. For the things resource modules cover, like ports and NTP, let the module compare data with
the running config. Reach for config templates when you&rsquo;ve decided to own the whole file.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/ansible-101/#chapter-4">Chapter 4 of the lab</a> lists the leaves&rsquo; ports as data, brings every port up with
one looped task, gives only the uplinks a jumbo MTU, and finishes the template for the fabric report.</p>
<p>Next, in <a href="/lessons/ansible-101/handlers-tags-errors/">lesson 5</a>, we save the config only when something actually
changed, run part of a playbook with tags, and recover cleanly when a change fails halfway through.</p>
]]></content:encoded></item></channel></rss>