On Ansible, you can use the dnf or yum module to install software packages on CentOS or RHEL hosts. By default, these modules install only a single software package. You can use the dnf or yum module multiple times in your Ansible playbook to install as many software packages as you want. Though, this is not the most efficient way of doing this, as there will be a lot of code repetition that we would like to avoid if possible. Luckily, we can use loops to easily install as many software packages as we want without any code repetition.

This article will show you how to install multiple software packages on CentOS/RHEL hosts using Ansible loop. So, let’s get started.

Prerequisites

If you want to try out the examples in this article,


1) You must have Ansible installed on your computer.


2) You must have at least a CentOS/RHEL 7 or 8 host configured for Ansible automation.

There are many articles on LinuxHint dedicated to Installing Ansible and configuring hosts for Ansible automation. You may check these out if needed.

Setting Up a Project Directory

Before moving any further, we will create a project directory, just to keep things a little bit organized.

To create the project directory centos-pkg/ and all the required subdirectories (in your current working directory), run the following command:

$ mkdir -pv centos-pkg/playbooks

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Once the project directory is created, navigate to the project directory, as follows:

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Next, create a hosts inventory file, as follows:

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Add the host IP or DNS name (vm3.nodekite.com, in my case) of your CentOS/RHEL hosts in the inventory file (one host per line).

Once you are done, save the file by pressing X, followed by Y and .

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Create an Ansible configuration file ansible.cfg in your project directory, as follows:

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Then, type the following lines in the ansible.cfg file.

[defaults]


inventory           = hosts


host_key_checking   = False

Once you are done, save the ansible.cfg file by pressing X, followed by Y and .

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Try to ping all the CentOS/RHEL hosts you have added in your hosts inventory file, as follows:

$ ansible all -u ansible -m ping

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, my CentOS 8 host is accessible.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Installing a Single Software Package

This section will show you how to install a single software package using the dnf or yum module of Ansible on your CentOS/RHEL hosts.

First, create the new playbook install_package.yaml in the playbooks/ directory, as follows:

$ nano playbooks/install_package.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Next, type in the following lines in the install_package.yaml file.

– hosts: all


  user
: ansible


  become
: True


  tasks
:


    – name
: Install httpd package


      dnf
:


        name
: httpd


        state
: present


        update_cache
: True

Once you are done, save the file by pressing X, followed by Y and .

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Here, I have added only one task, Install httpd package. The purpose of this task is to install the httpd package on CentOS/RHEL 8.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

CentOS/RHEL 7 does not use the dnf package manager, it uses the yum package manager. So, if you are using CentOS/RHEL 7, change dnf to yum, as marked in the screenshot below. No other changes are required.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

The name option is used to tell the dnf or yum module the name of the package you are trying to install. In this case, it will be the httpd package.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

The state option is used to tell the dnf or yum module the action (i.e. install, upgrade, remove) it should take on the given package. In this case, the action is present.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

The supported values of the state option are:

present – will install the package if not already installed.

latest – will install the package if not already installed; if already installed, the module will check for updates; if an updated version of the package is available, it will install the new version.

absent – will remove the package if it is installed.

If the update_cache option is set to True, the DNF/YUM package repository cache will be updated before the package is installed.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Run the install_package.yaml playbook, as follows:

$ ansible-playbook playbooks/install_package.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, the playbook install_package.yaml ran successfully.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, the httpd package is installed on my CentOS 8 host, which I have added on my hosts inventory file.

$ sudo dnf list installed | grep httpd

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

If you are on CentOS/RHEL 7, use the yum package manager instead of the dnf package manager to verify whether the package is installed.

$ sudo yum list installed | grep httpd

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Installing Multiple Software Packages Using the with_items Loop

The previous section of this article showed you how to install a single software package on your CentOS/RHEL host using the Ansible dnf/yum module. This section will show you how to install multiple software packages on your CentOS/RHEL hosts using the Ansible with_items loop.

First, create the new playbook install_packages.yaml, as follows:

$ nano playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Type the following lines in the install_packages.yaml file.

– hosts: all


  user
: ansible


  become
: True


  tasks
:


   – name
: Install all the packages


     dnf
:


       name
: {{ item }}


       state
: present


       update_cache
: True


     with_items
:

    httpd


     php


     vsftpd

Once you are done, press X, followed by Y and , to save the install_packages.yaml file.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Here, I have defined the package names (httpd, php, vsftpd) which I want to install using with_items loop.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

I have replaced the package name with the variable item. The item variable will be updated with the package name in each iteration.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Run the install_packages.yaml playbook, as follows:

$ ansible-playbook playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, the playbook install_packages.yaml ran successfully.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

The httpd, php, and vsftpd packages are installed on my CentOS 8 host, as you can see in the screenshot below.

$ sudo dnf list installed | grep httpd


$ sudo dnf list installed | grep php


$ sudo dnf list installed | grep vsftpd

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Installing Multiple Software Packages Using the loop Loop

Starting from Ansible 2.5, the recommended way to use loops in your playbook is by using the loop keyword, instead of the with_items keyword. That is why you saw a warning message when I ran the install_packages.yaml playbook in the earlier section of this article.

Working with loops using the loop keyword is very easy.

First, open the playbook install_packages.yaml (from the previous example), as follows:

$ nano playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

In the install_packages.yaml playbook, just replace the term with_items with the term loop, as marked in the screenshot below. You do not need to change anything else.

Once you are done, press X, followed by Y and , to save the install_packages.yaml file.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Run the install_packages.yaml playbook, as follows:

$ ansible-playbook playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, the task Install all the packages ran the dnf/yum module three times in total; once for each loop item.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

The httpd, php, and vsftpd packages are installed on my CentOS 8 host, as you can see in the screenshot below.

$ sudo dnf list installed | grep httpd


$ sudo dnf list installed | grep php


$ sudo dnf list installed | grep vsftpd

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Installing Multiple Software Packages using Array Syntax

Earlier sections of this article showed you how to use the with_items and loop loops in Ansible to install multiple software packages on CentOS/RHEL 7 or 8, without any code repetition. This section will show you how to do the same thing with a simpler array syntax.

First, open the install_packages.yaml playbook, as follows:

$ nano playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Remove the with_items or loop section from the task and change the name option to [‘httpd’, ‘php’, vsftpd’], as marked in the screenshot below.

Once you are done, press X, followed by Y and , to save the install_packages.yaml playbook.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Here, each quoted string inside the square brackets ([]) is an array element (the software package name, in my case). Each array element should be separated by a comma (,). You can add as many array elements as you would like. I have added only three elements, httpd, php, and vsftpd.

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Run the install_packages.yaml playbook, as follows:

$ ansible-playbook playbooks/install_packages.yaml

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

As you can see, the software packages httpd, php, and vsftpd are installed on my CentOS 8 host.

$ sudo dnf list installed | grep httpd


$ sudo dnf list installed | grep php


$ sudo dnf list installed | grep vsftpd

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Conclusion

This article, showed you how to use the with_items and loop loops, as well as the Array syntax, with the Ansible yum/dnf module to install multiple software packages on your CentOS/RHEL 7 or 8 hosts.

For more information, check the Ansible official documentation.

[1] Official documentation of Ansible loops


[2] Official documentation of Ansible with_items


[3] Official documentation of Ansible dnf module


[4] Official documentation of Ansible yum module

About the author

Installing Multiple Packages Easily on CentOS Using Ansible Ansible

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.