The Ansible yum module manages packages on systems that use the YUM package manager, such as CentOS, RHEL, RockyLinux, and Fedora. This module allows administrators to install, update, remove, or verify packages on target systems. Using the yum module, you can ensure specific packages and their versions are installed or removed from your systems. You can also manage package groups and automatically handle package dependencies.
This guide explains installing and managing packages on RPM-based Linux distributions using the Ansible yum module.
Table of Contents
Basic Syntax
The basic syntax of the yum module is simple.
- name: Description of the task
yum:
name: Package-Name
state: State
Explanation:
- name: This parameter specifies the name of the package to be managed.
- state: This parameter defines the desired state of the package (present, absent, latest, installed, etc.).
Installing a Package
To install a package using the yum module, set the state parameter to present or installed.
Example 1: Basic Package Installation
- name: Install httpd package
hosts: all
become: yes
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
This playbook installs the httpd package on all hosts in the inventory.
Example 2: Verify Package Installation
You can verify the package installation by checking its version or status.
- name: Verify httpd package installation
hosts: all
become: yes
tasks:
- name: Install httpd package
yum:
name: httpd
state: present
- name: Check httpd version
command: httpd -v
register: httpd_version
- name: Display httpd version
debug:
msg: "Installed httpd version: {{ httpd_version.stdout }}"
This playbook installs the httpd package if it’s not already installed, checks the installed version, and then displays the version information using a debug task.
Removing a Package
To remove a package, set the state parameter to absent.
Example 1: Basic Package Removal
- name: Remove httpd package
hosts: all
become: yes
tasks:
- name: Remove httpd package
yum:
name: httpd
state: absent
This playbook removes the httpd package from all hosts in the inventory.
Example 2: Ensure the Package is Removed
You can ensure a package is removed and verify it.
- name: Ensure httpd package is removed
hosts: all
become: yes
tasks:
- name: Remove httpd package
yum:
name: httpd
state: absent
- name: Verify httpd removal
command: rpm -q httpd
register: httpd_check
ignore_errors: yes
- name: Display httpd removal status
debug:
msg: "httpd package removal status: {{ httpd_check.stdout }}"
This playbook removes the httpd package from all hosts, verifies whether the package has been removed, and displays the removal status.
Updating a Package
To update a package to the latest version, set the state parameter to latest.
Example 1: Basic Package Update
- name: Update httpd package to the latest version
hosts: all
become: yes
tasks:
- name: Update httpd package to the latest version
yum:
name: httpd
state: latest
This playbook updates the httpd package to the latest version on all hosts.
Installing Multiple Packages
You can manage multiple packages in a single task by listing them in the name parameter. This is useful when you need to ensure multiple packages are installed, removed, or updated simultaneously.
Example 1: Install Multiple Packages
- name: Install multiple packages
hosts: all
become: yes
tasks:
- name: Install multiple packages
yum:
name:
- httpd
- vim
- git
state: present
This playbook installs the httpd, vim, and git packages on all hosts.
Working with Package Groups
The yum module can also handle package groups, which are collections of packages grouped together for easier management. To install a package group, use the name parameter with the @ prefix.
Example 1: Install a Package Group
- name: Install 'Development Tools' package group
hosts: all
become: yes
tasks:
- name: Install 'Development Tools' package group
yum:
name: "@Development Tools"
state: present
This playbook installs the Development Tools package group on all hosts.
Using Ansible yum_repository Module
The Ansible yum_repository module manages YUM repository configurations on RPM-based systems. It allows you to define and manage custom repositories, such as adding new ones or configuring existing ones.
Below is an example playbook that demonstrates how to add the Elasticsearch repository, import its GPG key, and install Elasticsearch on all target hosts.
---
- name: Install Elasticsearch
hosts: all
become: yes
tasks:
- name: Add Elasticsearch repository
ansible.builtin.yum_repository:
name: elasticsearch
description: "Elasticsearch repository"
baseurl: "https://artifacts.elastic.co/packages/7.x/yum"
gpgkey: "https://artifacts.elastic.co/GPG-KEY-elasticsearch"
gpgcheck: yes
enabled: yes
repo_gpgcheck: yes
- name: Install Elasticsearch package
ansible.builtin.yum:
name: elasticsearch
state: present
- name: Start and enable Elasticsearch service
ansible.builtin.systemd:
name: elasticsearch
state: started
enabled: yes
This playbook configures all target hosts to use the Elasticsearch YUM repository by adding its URL and GPG key. It then installs the Elasticsearch package from this repository if it’s not already present on the system. Finally, it starts the Elasticsearch service and enables it to start automatically on system boot.
Conclusion
The yum module in Ansible is a versatile tool for managing packages on RHEL, CentOS, and Rocky Linux systems. You can now effectively manage software packages and ensure your systems are up-to-date and secure.
You can also install RPM packages from local files.
FAQs
1. How do I handle failed installations with the yum module?
You can use the ignore_errors: yes parameter in your task to continue playbook execution even if the yum module fails for a package installation.
2. Can I disable a repository when using the yum module?
Yes, you can use the disablerepo parameter to disable a repository during package installation.
3. Is there a way to use yum with custom GPG keys?
Yes, use the disable_gpg_check parameter to skip GPG checks.