Codementor Events

Ansible yum Module – Tutorial and Examples

Published Oct 04, 2020
Ansible yum Module – Tutorial and Examples

Ansible yum module is used to manage packages using the yum package manager on CentOS and RHEL based Linux distributions including, RHEL, CentOS, Fedora, etc. You can perform all the basic package management operations including install, remove and update the packages using the yum module.

What is Ansible yum_repository Module?

Ansible yum_repository module is used to manage the repository in RHEL based Linux distributions. You can add a third-party repository using this module.

In this tutorial, we will be going to explain how to use ansible yum module and yum_repository module in detail with various examples.

Prerequisites

  • One Ansible control node: A server running CentOS 8 with Ansible installed and configured. To set up Ansible, please follow my guide on How to Install and Setup Ansible
  • One Ansible Target node: A fresh server running CentOS 8.

Create an Inventory File

For the purpose of this tutorial, you will need to create a project directory and an inventory file on the Ansible host system.

First, create a new project directory with the following command:

Next, create an inventory file inside the ~/project directory:

| |

nano ~``/project/inventory``.txt

|

Add the following lines:

| |

target1 ansible_host=192.168.0.11 ansible_user=root [email protected]

|

Save and close the file when you are finished.

Install a New Package Using the yum Module

In this section, we will show how to install a new package with the yum module. For installing a new package, you will need to specify a package name and the state of the package.

The following example will update the package cache, check whether the unzip package is installed or not on the Target server. If it is not installed the unzip package will be installed. If the unzip package is already installed, Ansible will do nothing.

Create a playbook.yml file inside the ~/project directory:

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Install unzip package

``yum: name=unzip state=present update_cache=``true

|

Save and close the file when you are finished.

Where:

  • yum is the name of the module.
  • name=unzip is the name of the package you want to install.
  • state=present will install the package if it is not installed.
  • update_cache=true will update the package cache if it is out of date.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Install unzip package] ******************************************************************************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Install the Latest Version of Package Using the yum Module

In this section, we will show you how to install the latest version of the package with yum module.

In the previous section, we have used state=present to check if the package is installed or not. Now, we will use state=latest to install the latest version of the package.

Let’s create a playbook to install the latest version of Nginx on the Target system.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Install the latest Nginx package

``yum: name=nginx state=latest update_cache=``true

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Install the latest Nginx package] *******************************************************************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Install Multiple Packages Using the yum Module

In some cases, you will need to install multiple packages on the Target server. In that case, you can use item and combine all packages in a single task.

Let’s create a playbook to install git , wget , unzip and curl package on the Target server.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Install multiple packages

``yum: name={{ item }} state=latest update_cache=``true

``loop: [git, wget, unzip, curl]

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Install multiple packages] **************************************************************************************************************

ok: [target1] => (item=git)

ok: [target1] => (item=wget)

changed: [target1] => (item=unzip)

ok: [target1] => (item=curl)

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Update All Packages Using the yum Module

You can update all packages installed on the Target server using the state=latest parameter.

Let’s create a playbook to update all packages on the Target server.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Update all packages

``yum: name=* state=latest update_cache=``true

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************

TASK [Update all packages] *****************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Install a .rpm Package Using the yum Module

In some cases, you will need to download the . rpm package and install it to the server. In this case, the Ansible yum module will help you to download the . rpm package from the web and install it on the Target server.

Let’s create a playbook to download the Remi rpm file and install it on the Target server.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Install Remi package from .rpm ``file

``yum: name=https:``//rpms``.remirepo.net``/enterprise/remi-release-8``.rpm state=present update_cache=``true

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************

TASK [Install Remi package from .rpm ``file``] *************************************

PLAY RECAP *********************************************************************

target1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Remove Package Using the yum Module

If you want to remove any package from your system. You can use state=absent parameter with ansible yum module to remove your desired package.

Let’s create a playbook to uninstall Nginx package on the Target server.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Uninstall nginx package

``yum: name=nginx state=absent

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Uninstall nginx package] ****************************************************************************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Capture the Output of the yum Module

You can use register=yum_output parameter with the yum module to print the output of any command executed in the playbook.

Let’s create a playbook to install tree package and capture the output:

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name: Capture the Output

``yum: name=tree state=present update_cache=``true

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Capture the Output] *********************************************************************************************************************

TASK [debug] **********************************************************************************************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Validate If a Package is Installed

In some cases, you may need to check whether your required package is installed or not without making any modification to the system. In that case, you can use package_facts with yum module to validate the package information.

Let’s create a playbook to check whether the Nginx package is installed or not on the Target server:

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name: Gather Package facts

``package_facts: manager=auto

``- name : Validate whether Nginx is installed or not.

``debug: msg=``"Nginx is installed"

``when: ``"'nginx' in ansible_facts.packages"

``- name : Validate whether Nginx is installed or not.

``debug: msg=``"Nginx is not installed"

``when: ``"'nginx' not in ansible_facts.packages"

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Gather Package facts] *******************************************************************************************************************

TASK [Validate whether Nginx is installed or not.] ********************************************************************************************

TASK [Validate whether Nginx is installed or not.] ********************************************************************************************

``"msg"``: ``"Nginx is not installed"

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

|

Add Repository and GPG key with yum Module

In this section, we will show you how to use yum_repository module to add the repository and rpm_key module to add the GPG key on the Target system.

Let’s create a playbook to add the Elasticsearch GPG key and repository on the Target server and also print the output.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : ``install elasticsearch rpm key

``rpm_key: key=https:``//artifacts``.elastic.co``/GPG-KEY-elasticsearch state=present

``- name: ``install elasticsearch 7.x rpm repository

``yum_repository: name=elasticsearch-7.x state=present description=``"Elasticsearch repository for 7.x packages" baseurl=https:``//artifacts``.elastic.co``/packages/7``.x``/yum gpgcheck=``true gpgkey=https:``//artifacts``.elastic.co``/GPG-KEY-elasticsearch

``register: yum_repository_output

``- debug: var=yum_repository_output

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [``install elasticsearch rpm key] **********************************************************************************************************

TASK [``install elasticsearch 7.x rpm repository] ***********************************************************************************************

TASK [debug] **********************************************************************************************************************************

``"yum_repository_output"``: {

``"after_header"``: ``"/etc/yum.repos.d/elasticsearch-7.x.repo"``,

``"before_header"``: ``"/etc/yum.repos.d/elasticsearch-7.x.repo"

``"repo"``: ``"elasticsearch-7.x"``,

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

Remove Repository with yum Module

If you want to remove the repository from the yum you can use state=absent parameter with yum_repository module.

Let’s create a playbook to remove the Elasticsearch repository and clean up the metadata cache on the Target server.

| |

nano ~``/project/playbook``.yml

|

Add the following lines:

| |

``- name : Remove Elasticsearch Repository

``yum_repository: name=elasticsearch-7.x state=absent

``register: yum_repository_output

``- name: yum-clean-metadata

``command``: yum clean metadata

``when: yum_repository_output.changed

|

Save and close the file when you are finished.

Next, change the directory to the ~/project and run the Ansible playbook with the following command:

| |

ansible-playbook playbook.yml -i inventory.txt

|

You should get the following output:

| |

PLAY [all] ************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************

TASK [Remove Elasticsearch Repository] ********************************************************************************************************

TASK [yum-clean-metadata] *********************************************************************************************************************

PLAY RECAP ************************************************************************************************************************************

target1                    : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

|

You should also read the following chapters:
      1. Introduction
      2. Lab Setup – Install Ansible
      3. Ansible Inventory
      4. Introduction to YAML
      5. Ansible Playbooks
      6. Ansible Modules
      7. Ansible Variables
      8. Ansible Conditionals and Loops
      9. Ansible Roles
10. Ansible Projects
Discover and read more posts from Hitesh Jethva
get started
post commentsBe the first to share your opinion
Show more replies