How to Install Multiple Packages in Ansible (One Task)
By Luca Berton · Published 2024-01-01 · Category: installation
How to install multiple packages in a single Ansible task using apt, dnf, yum, and package modules. Efficient package management with lists and loops.
How to Install Multiple Packages in Ansible (One Task)
Installing packages one at a time is slow. Here's how to install many packages efficiently in a single task.
List of Packages (Recommended)
This runs a single apt transaction — much faster than looping.
Using a Variable
Cross-Platform Package Installation
Install Specific Versions
Why Not Use Loop?
The list approach runs one apt install nginx postgresql redis command. The loop runs three separate commands.
FAQ
How do I install multiple packages in one Ansible task?
Pass a list to the name parameter: name: [nginx, python3, curl]. The apt, dnf, and yum modules handle lists natively, installing all packages in a single transaction.
Is it faster to use a list or a loop?
A list is significantly faster. It runs one package manager command instead of one per package. Use lists whenever possible.
How do I install packages on both Debian and RHEL?
Use ansible.builtin.package (generic) for common packages, or use when: ansible_os_family conditionals with apt and dnf for OS-specific package names.
Related Articles • Ansible Playbook Guide • Ansible Variables Guide
Category: installation