In a simple Ansible loop, which keyword supplies the list, which variable holds the current value, and how can the list come from a variable?
loop: supplies the list, item holds the current element on each pass, and loop: "{{ my_list }}" takes the list from a variable.
The loop keyword sits at task level (next to name and the module) and takes a list. On each iteration Ansible sets item to the next element and runs the module again, so any module argument written as "{{ item }}" gets a different value per pass.
The list does not have to be written inline. Keeping it in a variable separates the data (which services) from the logic (make them run), so the list can be changed per environment without touching the task:
vars:
mail_services:
- postfix
- dovecot
tasks:
- name: Postfix and Dovecot are running
ansible.builtin.service:
name: "{{ item }}"
state: started
loop: "{{ mail_services }}"
The quotes around "{{ mail_services }}" are required: a YAML value that starts with { would otherwise be read as the start of a dictionary, not as a Jinja2 expression.
Go deeper:
Loops: iterating over a simple list (official docs) — passing a list from a variable to
loop.YAML syntax: gotchas (official docs) — why a value starting with
{{must be quoted.