0Pricing
Ansible Academy · Lesson

The loop Keyword Over a List

Install many packages in one task.

The loop Keyword Over a List is a free Ansible Academy lesson on CoddyKit — lesson 1 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.

Stop Copy-Pasting Tasks

Need to install three packages? Writing three near-identical tasks is tedious and error-prone. The loop keyword lets one task repeat over a list. 🔁

What loop Does

Add loop to a task and give it a list. Ansible runs that task once per item, plugging each value into the module's arguments.

Meet the item Variable

Inside a looping task, the current list value is available as item. You reference it with {{ item }} wherever you need the value.

- name: Show each fruit
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop:
    - apple
    - banana
    - cherry

Install Many Packages

The classic use: install a whole list of packages in one task. Each name flows into the package module through item.

- name: Install tools
  ansible.builtin.package:
    name: "{{ item }}"
    state: present
  loop:
    - git
    - curl
    - vim

Loop Over a Variable List

The list does not have to be inline. Point loop at a variable so the same task adapts to whatever values you define elsewhere.

vars:
  packages:
    - git
    - curl
tasks:
  - name: Install
    ansible.builtin.package:
      name: "{{ item }}"
    loop: "{{ packages }}"

loop vs with_items

You may see older playbooks using with_items. It still works, but loop is the modern, recommended form for simple lists.

One Task, Many Results

A loop runs the task per item, so the output shows one result line for each iteration. You see exactly which values changed.

Loops Stay Idempotent

Looping does not break idempotency. If a package is already present, that iteration reports ok; only new ones report changed.

Many Package Managers Batch

Some modules accept a list directly, which is even faster. But loop is the universal pattern that works with almost any module.

Loop Over Numbers Too

Lists are not just strings. You can loop over numbers or any data type, and item carries whatever value the list holds.

- name: Print ports
  ansible.builtin.debug:
    msg: "Port {{ item }}"
  loop:
    - 80
    - 443
    - 8080

Empty Lists Are Safe

If the list is empty, the task simply runs zero times. No error, no harm. Looping over a variable that might be empty is perfectly fine.

Quick Check

Inside a looping task, how do you reference the current list value?

Recap: You Can Loop

You replaced repetitive tasks with one loop, accessed each value via item, and kept everything idempotent. Next: looping over dictionaries. 🎉

Frequently asked questions

Is the “The loop Keyword Over a List” lesson free?

Yes — the full text of “The loop Keyword Over a List” 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 “The loop Keyword Over a List”?

Install many packages in one task. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The loop Keyword Over a List” 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

  1. The loop Keyword Over a List
  2. Looping Over Dictionaries
  3. Nested Loops with subelements
  4. loop_control: label, index & pause
← Back to Ansible Academy