0Pricing
Ansible Academy · Lesson

Loops & if Blocks in Templates

Generate repeated config sections.

Logic Lives in {% %}

Beyond printing values, Jinja2 runs control logic inside {% %} tags. This is how one template generates many lines. 🔁

A Basic for Loop

Use a for loop to repeat lines over a list. Every item produces another block of rendered text.

{% for host in backends %}
server {{ host }};
{% endfor %}

All lessons in this course

  1. The template Module & .j2 Files
  2. Variables & Expressions in Jinja2
  3. Loops & if Blocks in Templates
  4. Filters: default, join & upper
← Back to Ansible Academy