LOGBOOK

HELP

Quiz Entry - updated: 2026.11.12

What problem does a loop solve in an Ansible playbook?

It replaces several near-identical tasks that call the same module with one task that repeats over a list of values.

Two copy-pasted service tasks collapse into one task with a loop over postfix and dovecot

* Two tasks that differ only in one value become one task with a loop. *

Without a loop, making sure five users exist means writing five tasks that are identical except for the user name. That is tedious, and it is fragile: when you change an option (say, the shell every user gets), you have to change it in five places, and missing one leaves the hosts inconsistent.

With a loop you write the task once and give it the list of values. Ansible runs the task once per element and puts the current element in the loop variable item:

- name: Postfix and Dovecot are running
  ansible.builtin.service:
    name: "{{ item }}"
    state: started
  loop:
    - postfix
    - dovecot

The two hand-written tasks (one for postfix, one for dovecot) become this single task. The value that changes moves into the list; everything that stays the same is written once.

Go deeper:

From Quiz: ITIA / Ansible Task Control: Loops, Conditionals, Handlers and Error Handling | Updated: Nov 12, 2026