Table of contents
1.
Introduction
2.
Ansible Yum 
3.
Commands and codes 
3.1.
Installing a package using Yum 
3.2.
Installing multiple packages using Yum
3.3.
Installing a specific package version using Yum 
3.4.
Removing a package using Yum 
3.5.
Updating a package using Yum 
3.6.
Updating multiple packages using Yum 
3.7.
Capturing the output of the Yum module commands
3.8.
Listing available packages using Yum 
4.
When to Choose Yum 
4.1.
Choosing between yum and apt module
4.2.
Choosing between yum and package module
5.
Frequently Asked Questions
5.1.
What is Ansible?
5.2.
Which Linux distros is Yum used for?
5.3.
How can I view my Ansible hosts?
6.
Conclusion
Last Updated: Aug 13, 2025

Ansible - Yum

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ansible is an open-source software tool programmed in python, that provides numerous modules to make the deployment of applications and systems easy. Ansible’s Yum module helps to manage packages. It is used to update, remove, manage and install software packages of Red Hat Enterprise Linux. It performs dependency resolution when working with these installed packages. Details and working of Yum are discussed below.

 

Official logo of Ansible

 

Ansible Yum 

The Yum module also requires two parameters for the primary command, like other package management modules in Ansible.

Like other Ansible modules, this module requires 2 parameters for the primary command. Those are

  1. Name: name of the package that needs to be installed. 
  2. State: Defines the state of the package after installation, whether it would be present or absent. The default value of this parameter will always be ‘present’, if not specified.

 

Flowchart describing the hierarchy of Ansible

 

Commands and codes 

Installing a package using Yum 

Let’s install the epel repository using Yum. We’ll set the ‘name’ parameter to ‘epel release’ and the ‘state’ to ‘present’. Adding a parameter, ‘update_cache: true’, makes yum check if the package is out of date and updates it if needed.

name: install ruby 
yum:
 name: ruby
 state: present
 update_cache: true
become: true
when: ansible_os_family == 'RedHat'

Installing multiple packages using Yum

For quick and simultaneous installation of packages, simply list the various packages under the ‘name’ parameter as shown below:

- name: Install multiple packages using Yum
         yum:  
      		name: 
				- epel-release 
				- ruby
				- firefox
      		state: present 
      		update_cache: true
    	 become: true

Installing a specific package version using Yum 

If the version name of the package is mentioned alongside the package name, yum will explicitly install the required version for you. To check the available versions of a specific package, run 

$ yum list *package-name* - -showduplicates

on your linux terminal to get the list of versions

- name: Install a specific version of ruby
         yum:  
      		name: ruby-2.0.0.648-33.el7_4 
      		state: present  
      		update_cache: true
   become: true

Removing a package using Yum 

Assigning the ‘state’ parameter as ‘absent’ will help you delete the chosen package. We can add a parameter, autoremove: true , which will take care of any dependencies that might have been installed initially. 


- name: removing ruby from the local machine  
         yum:  
      		name: ruby
      		 state: absent
      		 autoremove: true
    	 become: true

      

Updating a package using Yum 

Assigning the ‘state’ parameter as ‘latest’ will help you install the latest version of the required package. In case the package is already installed, Yum will update the package on your local host.

- name: Install the latest version of ruby/update existing 
         yum:  
      		name: ruby 
      		state: latest  
      		update_cache: true
         become: true

Updating multiple packages using Yum 

For updation of multiple packages, simply list the various packages under the ‘name’ parameter as shown below:


- name: update multiple packages 
         yum:  
      		name:
        		- ruby 
        		- firefox
      		state: latest  
      		update_cache: true
         become: true

Capturing the output of the Yum module commands

The register keyword is used to capture and store the result of the yum commands. This captured result can be used further where and when required


- name: to check is rugby is present
  	yum:
    	name: ruby
    	state: present
    become: true
    register: yum_output


Listing available packages using Yum 

To view the available and installed versions of a package on your host, the list parameter is assigned the name of the package as shown. The register command is used to capture the output and use it elsewhere, as described in the code-box above. 


- name: available versions of ruby on system
  	yum:
    	list: ruby
  		become: true
    	when: ansible_os_family == 'RedHat'
    register: yum_output

When to Choose Yum 

Choosing between yum and apt module

The yum package manager can be used only on Red Hat Enterprise Linux and CentOS. For Linux distros based on Debian Linux Distributions like Debian and Ubuntu use the apt packet manager. So, choose your packet manager in accordance with your linux distro. 
 

Choosing between yum and package module

The package module is a flexible manager that chooses the manager present on your local machine, be it dnf, apt etc. Even though this seems convenient, yum package manager should be used for Red Hat Enterprise Linux as ‘package’ may lead to errors due to the package naming ambiguity across various package managers, i.e. the same package may have different names across different managers. 

 

Frequently Asked Questions

What is Ansible?

Ansible is a python-programmed open source software tool, used to manage your applications easily. Yum is a module of Ansible which helps to deal with installation and management of packages under the same.

Which Linux distros is Yum used for?

Yum is primarily used for Red Hat Enterprise Linux versions 5 and later. Yum can also be used for distros like Fedora, CentOS, Oracle Enterprise Linux, Terbolinux, and other distros derived from Red Hat Enterprise Linux.

How can I view my Ansible hosts?

The —list-hosts command will display the host IPs from your inventory file.
 

Conclusion

In this blog, we have successfully learned the basic working of the Yum module using commands to perform operations on sample packages- their installation, updating, and removal. For an insight into the topics related to the blog above, you can refer to our articles on AnsibleLinux, and Operating Systems. Join our platform Coding Ninjas Studio to practice and learn essential computer fundamentals like DBMSDSACompetitive ProgrammingPythonJava, etc. Share and upvote this blog if you find it helpful to help fellow ninjas get acquainted with it and help them grow. Happy Coding!!

 

 

 

 

Live masterclass