Introduction
Having an inventory of installed software available at hand is an important asset management function. Using this information, we can determine what software is installed on the system, what software needs to be patched or updated and what software needs to be removed. By making use of the yum module available with Ansible we could easily extract this information about our inventory and store it appropriately for future use. In this article, we will share an Ansible playbook that will consist of three tasks. The first task will list all packages currently installed on the system. The second task will list the installed and available versions of a single software package. The third tasks will display the installed and available versions of more than one package.
Given below is the Ansible playbook:
--- - name: Demonstrate yum list hosts: all gather_facts: no become: yes become_method: sudo tasks: - name: list all installed packages yum: list=installed register: out_all - debug: var=out_all - name: list only one package yum: list=git register: out_one - debug: var=out_one - name: list some packages yum: list={{ item }} with_items: - git - tar - curl register: out_some - debug: var=out_some
- Explanation
Under the hosts section, we’ve indicated that we’d like to execute this playbook on all hosts available in the inventory. You may modify it appropriately as per your requirement. - To provide a speed boost while executing the playbook we set gather_facts to no as gathering facts could take a while if there are a large number of target hosts.
- We’ve indicated that we would like to execute the tasks that follow using superuser privileges via using become and become_method keywords.
- Next, we have the first task. This will list all the packages installed on the system via list=installed.
- We then store the resulting output in a variable using the register keyword and print the result to the screen using the debug module.
- In the second task, we gather the package information (installed and available) for the git package only.
- In the third and final task, we employ the yum module to list information about three packages git, tar, and curl.
- We’ve used the loop mechanism with_items available with Ansible to iterate through the package name for which we wish to obtain the information for.
The output:
Since we’ve used the debug module to print the results of the execution of the tasks, the output provided by Ansible will be in JSON format. You may redirect this output to a text file or a database as appropriate but preferably you may want to feed the resulting output into a tool which could process JSON data and store it in a manageable fashion.
Given below is a snippet from the output displayed by the execution of the second task:
ok: [linuxnix] => { "out_one": { "changed": false, "failed": false, "results": [ { "arch": "x86_64", "envra": "0:git-1.7.1-4.el6_7.1.x86_64", "epoch": "0", "name": "git", "release": "4.el6_7.1", "repo": "installed", "version": "1.7.1", "yumstate": "installed" },
The list action of the yum module provides the following useful information about the module:
- The software architecture of the package
- The full name of the package RPM
- The name of the package
- The release version of the package
- The repository from which the package was installed
- The version of the package
- The state of the package i.e. whether the package has been installed on the system or if its available for installation
Conclusion
In this article, we shared an Ansible playbook which you could you to obtain information about software installed on your systems. We hope that you’ve found this article and the Ansible playbook useful and we look forward to your feedback and suggestions.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021