Updated March 31, 2023
Introduction to Ansible Package Module
Ansible Package Moduleis a change management automation tool, which provides many useful modules to work remotely on target machines. These modules can do various tasks based on their features and provide options while executing. One such important module is a package module, which is used to do software package management on target remote machines. This can trigger the package manager on the remote machines to perform the install, removal, and upgrade of software packages.
For windows, there is a similar package module named win_package. In this article, we will learn about the Ansible package module and possible ways to use it.
What is Ansible Package Module?
Ansible package module is used to do package management via Ansible on the remote target machine. This module works smartly so that it automatically identifies the package manager on the operating system of remote target nodes and triggers it with suitable options. Before using this module we should know the below points:
- On remote target machines, the package manager can be yum, apt, etc. which this module calls
- If you need more control over the tasks to be done, then you should use Ansible YUM, DNF, etc. module, which is equipped with more parameters and
- While using it, one should note that the package name varies from one package manager to another and the name should be well known before using for a
How does the Ansible Package Module work?
Ansible package module has the following parameters and related acceptable options. we can use these in different combinations to achieve desired work.
name: – This is to give the name of the package to Also package version should be specified as per needs. While using this the syntax matters a lot. One important point to note is that different Linux distributions can have different names for a package, so we must have the package name and version if required specifically.
state: – This is to tell whether to install or remove a package. Supportable values are present and absent. Other states can also be used but the underlying package manager must support that state or
use: – For this parameter, default is “auto”. This is to tell which package manager to use like apt, yum, etc. When not specified, it uses Ansible facts to identify the target systems. This parameter is useful in cases where automatic functionality is not
Examples to Implement Ansible Package Module
Below are some examples of Ansible Package Module:
Now by using examples, we will try to learn about the Ansible package module, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, which we used for testing purposes. Here we have an Ansible control server named ansible-controller and two RHEL remotes hosts named host-one and host-two. Also, we have one Ubuntu host named host-two-ubuntu, for which we do not have an entry in inventory yet, so playbooks will not run until we mentioned it in the inventory file. We will make an entry in the inventory file in the last example.
We will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.
Example #1
In this example, we have a playbook with the below contents. Using this playbook, we will do nginx package installation on target remote machines. Using this playbook, we first checked the package’s state on remote machines, then install this Nginx, and then check again its
Code:
---
hosts: all tasks:
name: Test whether nginx is installed shell: rpm -qa nginx
register: var_output
debug:
var: var_output.stdout_lines
name: using package module to install package:
name: nginx state: present
name: Test whether nginx is installed shell: rpm -qa nginx
register: var_output1
debug:
var: var_output1.stdout_lines
Running this playbook like below:
# ansible-playbook ansible_package.yaml
Output:
Explanation: As you saw here, firstly the output was blank when we checked the status of the installation state of the Nginx package. Then we installed it using the Ansible package module, then on checking again, we can see output contains Nginx package name with version.
Example #2
In this example, we have a playbook below Using this playbook, we will do an update of httpd and nginx packages on target remote machines. To do this we will list these RPMs under name parameter as below and use parameter state with value latest, which is working as our target machines are RHEL based, which have YUM as the package manager. Yum supports latest as a state:
Code:
---
hosts: all tasks:
name: Update httpd and nginx packages using Ansible package package:
name:
httpd
nginx state: latest
Running this playbook like below:
# ansible-playbook ansible_package_update.yaml
Output:
Example #3
In this example, we will try to install the vim package on two remote target machines, among these two remote target machines, one is RHEL based and the other is Ubuntu Also we are updating our inventory file to have only these two hosts entries, like below so that when running playbook against all hosts, it will not distinguish between any host:
host-one
host-two-ubuntu
we have a playbook with the below contents. Using this playbook, we will do vim package installation on target remote machines:
Code:
---
hosts: all tasks:
name: Here we check the Linux debug:
var: ansible_distribution
package:
name: vim state: present
Running this playbook like below:
# ansible-playbook ansible_package_multiple_distro.yaml
Output:
Explanation: We get the below output; in this output, we can see that one host is RHEL, and the other is Ubuntu. But our package module did the installation of the vim package on both the distributions successfully, we do not have to pass any distribution-specific arguments or parameters:
Conclusion
As we saw in this article, the Ansible package module is a very useful module in package management. Also, its ability to identify remote target host’s operating system and package manager makes it more suitable in environments where we have multiple Linux distributions. So learn it first and then use it.
Recommended Articles
This is a guide to Ansible Package Module. Here we discuss the introduction to Ansible Package Module, what it is, how does it work, and examples. You can also go through our other related articles to learn more –