The Ansible yum module manages packages using the yum package manager on CentOS and RPM-based Linux distributions, such as RHEL, CentOS, Fedora, and Rocky Linux. The Ansible yum module allows you to interact with the yum package manager and perform all basic package management tasks, such as installing, removing, and updating the packages.

Prerequisites

Two prerequisites must be met to follow this guide:

  • An Ansible control node: A server running CentOS Stream 9 with Ansible installed and configured
  • An Ansible target node: A server running CentOS 9 Stream 9

Create an inventory file

The Ansible inventory file allows you to define one or more remote servers that you want to manage from the Ansible control node. For this tutorial, we will create an inventory file on the Ansible control node.

First, create an inventory file inside your home directory.

nano ~/inventory.txt

Add the following lines to define your target server's IP address, SSH username, and password.

server1 ansible_host=172.16.10.10 ansible_user=root ansible_ssh_pass=your_secure_password

Save and close the file after you finish. Then, verify the inventory file using the following command:

ansible-inventory -i inventory.txt --list

You will see the Ansible inventory information, as shown below.

Showing the Ansible inventory information

Showing the Ansible inventory information

Next, verify the connectivity with the target server.

ansible server1 -i inventory.txt -m ping

If everything is fine, you will see the following results:

server1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Install a package using the yum module

In this section, we will install the vim package using the yum module. You will need to define the package name and the state of the package to perform the operation.

Let's create a playbook.yml file inside the home directory.

nano ~/playbook.yml

Define the yum module and package name that you want to install on the target server.

- hosts: all
  tasks:
  - name : Install vim package
    yum: name=vim state=present update_cache=true

A brief explanation of the playbook.yml file is shown below:

  • yum defines the name of the package manager module.
  • name=vim 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

Now, run the Ansible playbook by specifying an inventory file.

ansible-playbook playbook.yml -i inventory.txt

This command updates the package cache and checks whether the curl package is installed on the target server. If it is not installed, it will be installed. If it is already installed, Ansible will do nothing, as shown below:

Installing the vim package on the target server

Installing the vim package on the target server

Install the latest package version

In this section, we will use state=latest to install the latest version of the package.

Let's create a new playbook to install the latest version of Apache on the target server.

nano ~/playbook.yml

Add the following lines to define the yum module and package name:

- hosts: all
  tasks:
  - name : Install the latest Apache package
    yum: name=httpd state=latest update_cache=true

Save the file, and then run the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

The above command will install the latest Apache package on the target server, as shown below:

Installing the latest Apache package on the target server

Installing the latest Apache package on the target server

Install multiple packages

The Ansible yum module allows you to combine all packages in a loop to install multiple packages in a single task.

The following example will install the mariadb, php, and unzip packages on the target server:

nano ~/playbook.yml

Add the following lines to define the yum with a loop module to install multiple packages:

- hosts: all
 
  tasks:
  - name : Install multiple packages
    yum: name={{ item }} state=latest update_cache=true
    loop: [mariadb, php, unzip]

Save and close the file, and then run the Ansible playbook, as shown below:

ansible-playbook playbook.yml -i inventory.txt

This command installs all the packages specified in a playbook on the target server, as shown below:

Installing multiple packages on the target server 1

Installing multiple packages on the target server 1

Remove a package

If you want to remove a package from your system, you can use the state=absent parameter with the Ansible yum module.

Let's create a playbook to remove the Apache package on the target server.

nano ~/playbook.yml

Add the following lines to define the yum module with the absent parameter:

- hosts: all
  tasks:
  - name : Uninstall Apache package
    yum: name=httpd state=absent

Save and close the file, and then run the Ansible playbook, as shown below:

ansible-playbook playbook.yml -i inventory.txt

This will uninstall the Apache package from the target server.

Removing the Apache package from the target server

Removing the Apache package from the target server

Install an RPM package

In some cases, your required package is not available in the CentOS repository. In this case, you can use the Ansible yum module to download the RPM package 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 ~/playbook.yml

Add the following lines to define the yum module with the Remi package link:

- hosts: all
  tasks:
  - 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, and then execute the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command downloads the Remi package from the web and installs it on the target server.

Installing the .rpm package on the target server

Installing the .rpm package on the target server

Update all packages

You can update all packages installed on the target server using the state=latest and update_cache=true parameter.

Let's create a playbook to update all packages on the target server.

nano ~/playbook.yml

Add the following lines:

- hosts: all
  tasks:
  - name : Update all packages
    yum: name=* state=latest update_cache=true

Save a file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command updates all the packages on the target server, as shown below:

Updating all packages on the target server 1

Updating all packages on the target server 1

Add a repository and a GPG key

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

Let's create a playbook to add the Elasticsearch GPG key and repository to the target server and print the output.

nano ~/playbook.yml

Add the following lines to define the Elasticsearch GPG key URL, and Elasticsearch repository path.

- hosts: all
  tasks:
  - name : install elasticsearch rpm key
    rpm_key: key=https://artifacts.elastic.co/GPG-KEY-elasticsearch state=present
  - name: install elasticsearch 8.x rpm repository
    yum_repository: name=elasticsearch-8.x state=present description="Elasticsearch repository for 8.x packages" baseurl=https://artifacts.elastic.co/packages/8.x/yum gpgcheck=true gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
    register: yum_repository_output
  - debug: var=yum_repository_output

Save the file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

This command will add the Elasticsearch GPG key and repository on the target server, as shown below:

Adding the Elasticsearch GPG key and repository on the target server

Adding the Elasticsearch GPG key and repository on the target server

Remove a repository

You can use the state=absent parameter with the yum_repository module to remove the repository from your server.

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

nano ~/playbook.yml

Define the apt_repository module with the Elasticsearch repository path, as shown below:

- hosts: all
  tasks:
  - name: Remove repository (and clean up left-over metadata)
    ansible.builtin.yum_repository:
      name: elasticsearch-8.x
      state: absent
    notify: yum-clean-metadata

Save the file, and then execute the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

The above command removes the Elasticsearch repository from the target server, as shown below:

Removing the Elasticsearch repository from the target server

Removing the Elasticsearch repository from the target server

Capture the output of the yum module

You can use the 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 the Nginx package and capture the output:

nano ~/playbook.yml

Add the following lines to define the yum module with the register=yum_output parameter.

- hosts: all
  tasks:
  - name: Capture the Output
    yum: name=nginx state=present update_cache=true
    register: yum_output
  - debug: var=yum_output

Save and close the file when you're done. Then, run the Ansible playbook using the following command:

ansible-playbook playbook.yml -i inventory.txt

This command installs the Nginx package on the target server and prints the execution output, as shown below:

Showing the output of the yum module

Showing the output of the yum module

Verify whether a package is installed

Sometimes, you may need to check whether your required package is installed without making any modifications to the target server. In this case, you can use package_facts with the yum module to verify the package installation information.

Let's create a playbook to check whether the Apache package is installed on the target server.

nano ~/playbook.yml

Add the following lines:

- hosts: all
  tasks:
  - name: Gather Package facts
    package_facts: manager=auto
  - name : Validate whether the Apache is installed or not.
    debug: msg="Apache is installed"
    when: "'httpd' in ansible_facts.packages"
  - name : Validate whether the Apache is installed or not.
    debug: msg="Apache is not installed"
    when: "'httpd' not in ansible_facts.packages"

Save and close the file, and then run the Ansible playbook with the following command:

ansible-playbook playbook.yml -i inventory.txt

If the Apache package is not installed, you will get the following output:

Subscribe to 4sysops newsletter!

Verifying Apache package installation

Verifying Apache package installation

Conclusion

In this tutorial, I explained how to use the Ansible yum module to manage packages on CentOS- and RHEL-based operating systems. I also showed you how to manage the repository on RHEL-based operating systems. You can now use the Ansible yum module to manage packages on remote servers easily. For more information, visit the Ansible yum module documentation page.

avatar
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account