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.
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
- wgetHow 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
- Loops (Ansible docs)
Try it with BacklogZero
Ask BacklogZero to generate this automation for your environment.