One of the reasons for Ansible’s popularity is its extensive library of modules which refer to a set of pre-written scripts that execute the specialized tasks on the target machines.
This tutorial teaches us about the apt_rpm module in Ansible to manage the packages via the apt-rpm package manager.
Ansible APT_RPM Module
The Ansible apt_rpm module is part of the “community.general” collections. This module allows us to manage the packages via the apt-rpm package managers in systems such as SUSE, Mandriva, etc.
The apt-rpm package manager allows the APT tool to work with RPM packages which is a powerful tool for systems that utilize the RPM packages while taking advantage of the APT features.
Requirements:
To follow along with the provided examples and modules in this tutorial, ensure that you have the following:
- Installed Ansible version 2.9 and higher
- A system with the apt-rpm package manager
- Installed “community.general” collection. You can use the following command to install it:
Ansible APT_RPM Parameters
The following are the provided parameters by the apt_rpm module to configure how the module works:
Clean – It clears out the local repository of the retrieved package files, similar to running the apt-get clean command. Its supported values are either true/false.
Dist_upgrade – This is a Boolean parameter that upgrades the system if set to true. This is similar to running the apt-get dist-upgrade command.
Package – It specifies a list of packages to install, upgrade, or remove.
State – It defines the package state. The accepted values include:
- Absent
- Present
- Installed
- Removed
Update_Cache – It is similar to running the apt-get update command to refresh the software repository index.
Update_kernel – It is a Boolean parameter that determines whether the module runs an update-kernel command to upgrade the kernel packages.
Examples:
The following sections provide the sample playbooks that demonstrate how to use this module to perform the tasks on the target systems.
Example 1: Installing a Package
The first use is installing a package using the apt_rpm package. An example playbook is as follows:
- hosts: all
tasks:
- name: Install VIM
community.general.apt_rpm:
name: vim
state: present
Example 2: Removing a Package
We can also remove an already existing package by setting the state to absent as shown in the following playbook example:
- hosts: all
tasks:
- name: Remove VIM
community.general.apt_rpm:
name: vim
state: absent
Example 3: Upgrading a Package
To upgrade an existing package to the latest version, we can use the state parameter to the latest as follows:
- hosts: all
tasks:
- name: Upgrade VIM
community.general.apt_rpm:
name: vim
state: latest
Example 4: Installing Multiple Packages
We can also install multiple packages by providing a list of the packages that we wish to install as demonstrated in the following example:
- hosts: all
tasks:
- name: Install multiple packages
community.general.apt_rpm:
name:
- vim
- wget
- curl
state: present
Example 5: Update the Package Cache
To update the APT package cache, run the playbook as follows:
- hosts: all
tasks:
- name: Update package cache
community.general.apt_rpm:
update_cache: yes
Example 6: Installing a Package without Updating the Cache
We can also install a package without updating the package cache as shown in the following example:
- hosts: all
tasks:
- name: Install vim without updating cache
community.general.apt_rpm:
name: vim
state: present
update_cache: no
Example 7: Upgrading All Packages
To upgrade all packages on the system to their latest versions, run the playbook as follows:
- hosts: all
tasks:
- name: Upgrade all packages
community.general.apt_rpm:
upgrade: yes
Example 8: Complete the System Upgrade (Including Kernel)
To perform a complete system upgrade including kernel, we can run the playbook as follows:
- hosts: all
- name: complete system upgrade
community.general.apt_rpm:
update_cache: true
dist_upgrade: true
update_kernel: true
Example 9: Running an APT Clean
We can also perform an apt clean as a standalone task as shown in the following example:
- hosts: all
- name: Run apt clean
community.general.apt_rpm:
clean: true
Conclusion
We learned about the role and functionality of the Ansible APT_RPM module to manage the packages using the apt-rpm package managers. We also provided a plethora of examples on how you can perform the tasks using this module.