AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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 ArticlesAnsible Playbook GuideAnsible Variables Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home