Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the installed yum packages with Ansible?

I am trying to get all the installed YUM packages on an RHEL machine. I can easily get it through using shell commands which is not idempotent and would like to use the YUM command instead.

The shell command works fine:

- name: yum list packages
  shell: yum list installed > build_server_info.config

But when I try to use the YUM command, it just executes, but it does not give any results:

- name: yum_command
  action: yum list=${pkg} list=available
like image 206
user_dev Avatar asked Jan 09 '17 15:01

user_dev


People also ask

What is yum command in Ansible?

Ansible's yum module is used to manage packages with the yum package manager, which is the default on Red Hat based Linux distributions such as Red Hat Enterprise Linux and CentOS. Most systems require root/superuser permissions to manage packages, which means that become: true is required.

How do you check if a package is installed or not in Ansible?

How can I check if a software package is installed on a Linux system using Ansible?. You can use Ansible automation tool to query installation status of a package on Linux a system. From the results a condition can then be used, e.g skip a task if package is installed, or install if check status is failed.

How do I run Ansible yum update?

The first task you're telling the system to only update the yum cache. On the second you are effectively upgrading all packages to the latest version by using state=latest but you should also use update_cache=yes on the same task to be sure you're refreshing the cache with its latest package information.


2 Answers

I can easily get it through using shell commands which is not idempotent

You can't really talk about idempotence, when you are querying the current state of a machine.

"Idempontent" means that the task will ensure the machine is in the desired state no matter how many times you run a certain task.

When you query current state, you don't describe the desired state. No matter what you do, what method you use, the term "idempotent" is just not applicable.


Regarding your example, which does not give you results - you have repeated twice the same argument list and the task should fail (it doesn't, which looks like an Ansible quirk).

To get a list of installed packages, you should use:

- name: yum_command 
  yum:
    list=installed
  register: yum_packages

- debug:
    var: yum_packages

It saves a list of dictionaries describing each package to a variable yum_packages.

You can then use a JSON Query Filter to get a single package (tar):

- debug: var=item
  with_items: "{{yum_packages|json_query(jsonquery)}}"
  vars:
    jsonquery: "results[?name=='tar']"

to get a result like this:

"item": {
    "arch": "x86_64",
    "epoch": "2",
    "name": "tar",
    "nevra": "2:tar-1.26-31.el7.x86_64",
    "release": "31.el7",
    "repo": "installed",
    "version": "1.26",
    "yumstate": "installed"
}

Or only its version:

- debug: var=item
  with_items: "{{yum_packages|json_query(jsonquery)}}"
  vars:
    jsonquery: "results[?name=='tar'].version"
"item": "1.26"
like image 156
techraf Avatar answered Nov 15 '22 11:11

techraf


Since Ansible 2.5, you can also use the package_facts module: it will gather the list of installed packages as Ansible facts.

Example from the CPU:

- name: get the rpm package facts
  package_facts:
    manager: rpm

- name: show them
  debug: var=ansible_facts.packages
like image 25
sduthil Avatar answered Nov 15 '22 11:11

sduthil