BacklogZero®

Ansible in a Minute · Ansible Core ·

Use loop to run a task for each item in a list

Short answer

Use loop to install each RHEL package name in a list with one package task.

Ansible in a Minute — loop

Subscribe for updates

Get an email whenever we publish new guides and blog posts. Unsubscribe anytime.

By subscribing you agree we may email you about Ansible in a Minute and related Level Up Labs updates. We store your address in the same lead sheet as our contact form.

Request

install rhel packages for each name in an item loop: ansible-core, git, wget; become true

Example

A quick heads-up: Every environment is different. Review and test this example before you run it on systems you care about.

YAML
---
- name: Install RHEL packages for each name in an item loop
  hosts: all
  become: true
  tasks:
    - name: Install package
      ansible.builtin.package:
        name: "{{ item }}"
        state: present
      loop:
        - ansible-core
        - git
        - wget

How it works

loop repeats the task for each list item and sets item to the current value. Prefer loop over the older with_items form in new playbooks.

When to use it

  • Applying the same module call to many names or paths
  • Keeping package and user lists in one task

Requirements

  • Ansible: >=2.14

Validation

  • Syntax validated
  • Execution passed
  • Last verified 2026-09-09

Notes

Keep loop lists short and explicit in atomic examples. Move long lists into vars when the playbook grows.

Reference

Built for every Ansible use case in every organization

BacklogZero can generate this kind of automation in your environment, just like it did here.

Get BacklogZero today →

Related guides

← Back to Ansible in a Minute

Use loop to run a task for each item in a list · Level Up Labs