Loops & if Blocks in Templates
Generate repeated config sections.
Loops & if Blocks in Templates is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 %}Always Close Your Blocks
Each opening tag needs a closing one: endfor ends a loop, just as endif ends a condition. Forgetting it breaks rendering.
Loop Over a Dictionary
Walk a dictionary's pairs with .items(), capturing each key and value to render structured config.
{% for key, val in settings.items() %}
{{ key }} = {{ val }}
{% endfor %}The loop Variable
Inside a loop, Jinja2 gives you loop.index for the current count and loop.first or loop.last as handy booleans.
{% for s in servers %}
server{{ loop.index }} {{ s }};
{% endfor %}A Simple if Block
An if block renders its body only when the condition is true, so optional config appears only when it should.
{% if enable_ssl %}
ssl on;
{% endif %}if, elif and else
Chain choices with elif and a final else. Exactly one branch renders, just like in Python.
{% if env == 'prod' %}
level = warn
{% else %}
level = debug
{% endif %}Conditions Inside Loops
Nest an if inside a for to render only matching items, like skipping disabled servers in a pool.
{% for h in hosts %}
{% if h.enabled %}
server {{ h.ip }};
{% endif %}
{% endfor %}Whitespace Control
Loops can leave blank lines behind. A dash like {%- -%} trims surrounding whitespace for tidy output.
{%- for h in hosts -%}
{{ h }}
{%- endfor -%}The else of a for Loop
A for can have an else that runs only when the list was empty, perfect for a sensible fallback line.
{% for h in backends %}
server {{ h }};
{% else %}
# no backends defined
{% endfor %}Print vs Logic Tags
Keep them straight: {{ }} outputs a value, while {% %} controls flow but prints nothing itself.
Quick Check
You want to repeat a config line for every host in a list. Which tag starts that loop?
Recap
You can now loop with for, branch with if, read loop.index, nest them and trim whitespace. One template now writes dynamic config. ✨
Frequently asked questions
Is the “Loops & if Blocks in Templates” lesson free?
Yes — the full text of “Loops & if Blocks in Templates” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Loops & if Blocks in Templates”?
Generate repeated config sections. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Loops & if Blocks in Templates” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The template Module & .j2 Files
- Variables & Expressions in Jinja2
- Loops & if Blocks in Templates
- Filters: default, join & upper