Last Updated on 1 week by Sachin G
Managing software packages manually on Linux systems—especially across dozens or hundreds of servers—is both time-consuming and error-prone. As a Linux system administrator, DevOps professional. Mastering Ansible’s yum and dnf modules gives you automation power to handle package management consistently and efficiently.
In this blog post, you’ll learn how to install, update, and remove packages using Ansible, understand the difference between yum and dnf modules, and explore best practices for managing Linux packages across RHEL, CentOS, Rocky Linux, and other Red Hat-based distributions.
What is ansible.builtin.yum
or ansible.builtin.
dnf?
ansible.builtin collection included default yum or dnf module that managed package operations on the managed host. The ansible.builtin.yum module is part of the core Ansible modules for system updates and is used to manage software packages on RHEL, CentOS, and other YUM-based distributions. The ansible. builtin.yum / ansible. builtin.dnf module serves as a connection between the playbook and the YUM package manager. which is the default package management utility for RPM-based Linux operating systems like CentOS, AlmaLinux, Rocky, Oracle Linux, etc. It supports install RPM with Ansible, updating packages, removing them, and even handling Yum Repository Management. This module enabled users to perform a variety of package-related operations, including installation, removal, and updating across multiple systems.
I have explained How Ansible Navigator and the Ansible execution environment can be installed. Click here to read more.
How to Install Packages Using Ansible Yum or DNF Module?
Before working on the yum or dnf module, you can check the description or attributes through the following command and how to use this module in the example section.
ansible-navigator doc ansible.builtin.yum
ansible-doc ansible.builtin.yum
You can add the -s option will show attributes before the doc then it will show only attribute information.
How to Install Packages Using Ansible Yum or DNF Module?
Use the yum
module for Old CentOS and earlier or dnf
for RHEL / CentOS 8+ or Stream Version. Both modules are used in Ansible playbooks to automate package installation across managed nodes.
Ansible Yum or dnf module Example: Basic Structure
The usage of Ansible. builtin. yum or ansible.builtin.dnf. The module follows a simple and intuitive syntax within Ansible playbooks.
- name: Install package hosts: host_pattern tasks: - name: Install httpd package ansible.builtin.yum: name: package_name ( eg httpd ) state: present
Use state: present
For an idempotent configuration, the package will only be installed if it isn’t already present.
Here’s a simple playbook for yum install:
In this Ansible Playbook, the state
parameter defines the desired outcome (e.g., present
, absent
). It supports idempotent operations across all target systems.
In this example, the playbook instructs Ansible to install the Apache web server package (httpd) on remote hosts designated as web servers.
The ansible.builtin.yum or ansible.builtin.dnf module empowers administrators and DevOps practitioners to streamline package management tasks within their infrastructure.
What’s the Difference Between Ansible Yum and DNF Modules?
yum
is used on RHEL/CentOS 7 and earlier
dnf
is used on RHEL/CentOS/Rocky/Fedora/AlmaLinux 8+
dnf
is the modern replacement foryum
and supports better dependency handling
Feature | Yum Module | DNF Module |
---|---|---|
OS Compatibility | CentOS 7, RHEL 7 | RHEL 8+, Fedora, Rocky Linux |
Backend Tool | YUM (Yellowdog Updater) | DNF (Dandified YUM) |
Ansible Module | yum: | dnf: |
Package Installation, Removal & Updates
The yum module supports full package management with Ansible. Here are common examples:
Install Packages Using Ansible
- name: Install multiple packages
ansible.builtin.yum:
name:
- vim
- curl
- wget
state: present
Ansible Remove Package
- name: Remove a package
ansible.builtin.yum:
name: vsftpd
state: absent
Update Yum Package Ansible
- name: Update all packages
ansible.builtin.yum:
name: "*"
state: latest
This also fits into use cases like bulk server patching, security updates automation, and multi-node synchronization.
How to Install the Latest Version of a Package with Ansible DNF?
Example:
- name: Install latest version of MariaDB
dnf:
name: mariadb
state: latest
This guarantees you’re using the most recent package from the yum repository or EPEL repository.
Best Practices for Ansible Package Management
- Use
state: present
orlatest
only where necessary - Avoid hardcoding OS-specific modules — use conditionals if managing mixed environments
- Keep your
yum
ordnf
tasks are modular and reusable - Combine with handlers for restarting services after installs
- Monitor for failures or skipped hosts using the Ansible output
For dynamic environments, use variables to define package lists:
vars:
packages:
- httpd
- firewalld
Then loop through them using:
- name: Install multiple packages
yum:
name: "{{ item }}"
state: present
loop: "{{ packages }}"
FAQs
Ansible will display an error. You can use ignore_errors: yes
to continue or register:
to handle conditions gracefully.
Use latest
cautiously in production. It can introduce unplanned changes. Prefer present
controlled upgrades.
About the Author
I’m Sachin Gupta — a freelance IT support specialist and founder of techtransit.org. I’m certified in Linux, Ansible, OpenShift (Red Hat), cPanel, and ITIL, with over 15 years of hands-on experience. I create beginner-friendly Linux tutorials, help with Ansible automation, and offer IT support on platforms like Upwork, Freelancer, and PeoplePerHour. Follow Tech Transit for practical tips, hosting guides, and real-world Linux expertise!