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:
Once the project directory is created, navigate to the project directory, as follows:
Next, create a hosts inventory file, as follows:
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 <Ctrl> + X, followed by Y and <Enter>.
Create an Ansible configuration file ansible.cfg in your project directory, as follows:
Then, type the following lines in the ansible.cfg file.
inventory = hosts
host_key_checking = False
Once you are done, save the ansible.cfg file by pressing <Ctrl> + X, followed by Y and <Enter>.
Try to ping all the CentOS/RHEL hosts you have added in your hosts inventory file, as follows:
As you can see, my CentOS 8 host is accessible.
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:
Next, type in the following lines in the install_package.yaml file.
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 <Ctrl> + X, followed by Y and <Enter>.
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.
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.
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.
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.
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.
Run the install_package.yaml playbook, as follows:
As you can see, the playbook install_package.yaml ran successfully.
As you can see, the httpd package is installed on my CentOS 8 host, which I have added on my hosts inventory file.
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.
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:
Type the following lines in the install_packages.yaml file.
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 <Ctrl> + X, followed by Y and <Enter>, to save the install_packages.yaml file.
Here, I have defined the package names (httpd, php, vsftpd) which I want to install using with_items loop.
I have replaced the package name with the variable item. The item variable will be updated with the package name in each iteration.
Run the install_packages.yaml playbook, as follows:
As you can see, the playbook install_packages.yaml ran successfully.
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 php
$ sudo dnf list installed | grep vsftpd
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:
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 <Ctrl> + X, followed by Y and <Enter>, to save the install_packages.yaml file.
Run the install_packages.yaml playbook, as follows:
As you can see, the task Install all the packages ran the dnf/yum module three times in total; once for each loop item.
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 php
$ sudo dnf list installed | grep vsftpd
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:
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 <Ctrl> + X, followed by Y and <Enter>, to save the install_packages.yaml playbook.
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.
Run the install_packages.yaml playbook, as follows:
As you can see, the software packages httpd, php, and vsftpd are installed on my CentOS 8 host.
$ sudo dnf list installed | grep php
$ sudo dnf list installed | grep vsftpd
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