Ansible Builtin Yum Dnf

Ansible is a powerful open-source automation tool that allows IT administrators to automate configuration management, application deployment, and task orchestration. One of its key strengths is its ability to interact with package managers on different Linux distributions. Two commonly used package managers are YUM (Yellowdog Updater, Modified) and DNF (Dandified YUM). Ansible provides built-in modules specifically for managing packages with these tools, enabling seamless automation of software installation, updates, and removal across multiple systems. Understanding how to use the Ansible built-in YUM and DNF modules is essential for efficient system administration.

Overview of YUM and DNF

YUM and DNF are package management tools primarily used on Red Hat-based Linux distributions. YUM has been the standard package manager for many years, managing RPM packages and handling dependencies automatically. DNF is the next-generation version of YUM, designed to improve performance, reduce memory usage, and provide better dependency resolution. Both package managers allow administrators to install, update, and remove software packages easily, and they maintain a database of installed packages for system management.

Why Use Ansible Modules for YUM and DNF

Manual package management across multiple systems can be time-consuming and error-prone. Ansible’s built-in YUM and DNF modules automate these tasks, ensuring consistency and reliability. With these modules, administrators can define desired package states, such as installed, latest, or absent, and Ansible will handle the execution across all targeted hosts. This approach simplifies complex deployment processes, reduces human error, and ensures that systems remain in a consistent state.

Using the Ansible YUM Module

Theyummodule in Ansible allows you to manage packages on systems that use YUM as their package manager. Key functionalities include installing new packages, updating existing ones, removing packages, and managing repositories.

Basic Example

To install a package using the YUM module, you can use a simple playbook

- name Install Apache on RHEL hosts webservers tasks - name Install httpd package yum name httpd state present

In this example, Ansible will ensure that thehttpdpackage is installed on all hosts listed under thewebserversgroup. Thestate presentparameter specifies that the package should be installed if it is not already present.

Updating Packages

To update a package to its latest version, you can use

- name Update Apache to the latest version yum name httpd state latest

This instructs Ansible to upgrade the package to the most recent available version in the repositories.

Removing Packages

To remove a package from the system

- name Remove Apache package yum name httpd state absent

Thestate absentensures that the package is uninstalled from the system.

Using the Ansible DNF Module

Thednfmodule in Ansible is designed for systems that use DNF as their package manager. It shares many functionalities with the YUM module but is optimized for modern distributions such as Fedora, CentOS 8, and RHEL 8.

Installing Packages

To install a package using the DNF module

- name Install Nginx on RHEL 8 hosts webservers tasks - name Install nginx package dnf name nginx state present

This command ensures that thenginxpackage is installed on the target hosts.

Updating and Removing Packages

Similar to the YUM module, the DNF module allows updating and removing packages easily

  • To update a package
    dnf name=nginx state=latest
  • To remove a package
    dnf name=nginx state=absent

Managing Multiple Packages

Both YUM and DNF modules support installing multiple packages simultaneously

- name Install multiple packages dnf name - git - vim - htop state present

This simplifies package management by allowing administrators to define multiple packages in a single task, reducing the number of tasks in a playbook.

Additional Features

Both YUM and DNF modules support advanced features, such as

  • Specifying package versions withversion
  • Installing packages from custom repositories usingenablerepoordisablerepo
  • Running module operations withupdate_cache yesto refresh the package database
  • Usingconf_fileto specify alternative configuration files for YUM or DNF

Example Installing a Specific Version

- name Install a specific version of Git yum name git-2.30.0 state present

Best Practices

When using Ansible modules for YUM and DNF, consider the following best practices

  • Always update the package cache before installing or updating packages.
  • Specify package versions when consistency across multiple hosts is required.
  • Use idempotent playbooks to ensure that repeated runs do not change the system unnecessarily.
  • Test playbooks in a staging environment before deploying to production systems.

The Ansible built-in YUM and DNF modules provide powerful tools for automating package management on Red Hat-based Linux systems. By leveraging these modules, administrators can streamline software installation, updates, and removals across multiple hosts efficiently. With support for advanced features such as repository management, version control, and multiple package installations, these modules are essential components of any Ansible-based automation strategy. Mastering the use of YUM and DNF modules helps maintain consistent, reliable, and secure Linux environments, ultimately saving time and reducing the potential for errors in large-scale IT operations.