DevOps Bootcamp · Lesson

The loop Keyword Over a List

Install many packages in one task.

Lesson 1 of 413 steps

The loop Keyword Over a List is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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. 🎉

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

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

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “The loop Keyword Over a List”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “The loop Keyword Over a List”?

Install many packages in one task. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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 DevOps Bootcamp