Ansible simplifies package management on Linux systems, especially when using the yum module. Typically, Ansible’s yum module installs packages from remote repositories. But what if you want to install RPM packages from a local source? This is where the yum localinstall command comes in handy. The yum localinstall feature allows you to install RPM packages stored locally on your server.
In this guide, we will explain how to use Ansible’s yum module with the localinstall feature to install RPM packages from local files.
Table of Contents
What is localinstall?
The yum local install command typically installs an RPM package from a local file instead of pulling it from a configured repository. This is useful in cases where the system doesn’t have access to the Internet or the required RPM packages aren’t available in the standard YUM repositories.
Setting Up an Inventory File
Before we dive into the yum localinstall process, it’s important to have a properly configured Ansible inventory file. This file defines the hosts on which Ansible will run the playbooks.
Edit the default inventory file /etc/ansible/hosts and add the following content.
[local]
localhost ansible_connection=local
[servers]
server1 ansible_host=192.168.1.101 ansible_user=root
server2 ansible_host=192.168.1.102 ansible_user=root
Let’s break down each parameter:
- [local]: This defines a group named local for running tasks on the localhost.
- [servers]: Defines a group called servers where target hosts are listed with their respective IP addresses.
- ansible_host: Specifies the IP address of the target machine.
- ansible_user: The user Ansible will use to connect to the target machine.
Once the inventory is ready, you can start defining your tasks.
Basic Syntax of Ansible yum localinstall
The Ansible yum module manages packages on RedHat-based systems like RHEL, CentOS, and Fedora. It can be used to install, update, or remove packages. When working with the YUM module for local installations, you can specify the local file path of the .rpm.
- name: Install a package using YUM
yum:
name: /path/to/custom.rpm
state: present
Ansible’s yum module doesn’t have a specific localinstall option. However, you can still achieve this by pointing to .rpm files.
Example 1: Installing a Local RPM
In this example playbook, we will copy an RPM to a remote host and then install it using the yum module.
- hosts: servers
become: yes
tasks:
- name: Copy local RPM file to the managed node
copy:
src: /path/to/package.rpm
dest: /tmp/package.rpm
- name: Install the local RPM package
yum:
name: /tmp/package.rpm
state: present
In this example:
- copy module is used to transfer the RPM file from the control node to the remote host.
- yum module installs the RPM package from the local file path on the remote host.
Example 2: Installing Multiple RPMs
Ansible allows you to install multiple RPMs in a single task using the with_items loop. This method is efficient for bulk installations.
- hosts: servers
become: yes
tasks:
- name: Copy multiple RPM files to the managed node
copy:
src: "/path/to/{{ item }}"
dest: /tmp/
with_items:
- package1.rpm
- package2.rpm
- name: Install the local RPM packages
yum:
name: "/tmp/{{ item }}"
state: present
with_items:
- package1.rpm
- package2.rpm
This example uses a loop to handle multiple RPM files. The copy module copies each RPM, and then the yum module installs them.
Example 3: Handling Dependencies
Sometimes, RPM packages require other dependencies that may not be available in the repositories or need to be installed locally. You can manage this by either installing the dependencies first or disabling GPG checks for RPMs.
- hosts: servers
become: yes
tasks:
- name: Install RPM with dependencies and disable GPG check
yum:
name: /tmp/package.rpm
state: present
disable_gpg_check: yes
The disable_gpg_check: yes option is useful if the RPM file lacks a GPG signature or if you’re installing untrusted packages.
Conclusion
In this article, we have covered how to use Ansible to perform a yum localinstall using the yum module. This method is useful in environments where remote repositories are unavailable or for deploying custom-built RPMs.
FAQs
1. Can Ansible resolve dependencies for RPMs?
Yes, if the yum module is used, it can resolve dependencies from configured repositories during installation.
2. How can I verify if the RPM is installed successfully via Ansible?
You can use the state: present option in the yum module to ensure the package is installed.
3. What are the prerequisites for using yum localinstall in Ansible?
Ensure the RPM is available on the managed node and the necessary dependencies for the RPM are also present or resolvable.