How can I get the installed YUM packages with Ansible?
Asked Answered
R

4

7

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
Rickie answered 9/1, 2017 at 15:43 Comment(0)
A
20

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"
Antoineantoinetta answered 9/1, 2017 at 15:57 Comment(1)
If you just want a list of packages to use elsewhere the jsonquery you are looking for is {{ yum_packages | json_query('results[*].name') }}. Very useful with | difference() to get a list of missing packages for example.Preshrunk
J
8

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
Johniejohnna answered 26/9, 2018 at 14:57 Comment(0)
W
0

Well, the official Ansible documentation for the yum module describes list as:

"Various (non-idempotent) commands for usage with /usr/bin/ansible and not playbooks."

so you're going to be out of luck with finding an idempotent list invocation.

If you just want to suppress the changed output, set the changed_when parameter to False.

(Also, having the duplicate list parameter is suspicious.)

Wisner answered 9/1, 2017 at 15:53 Comment(3)
This will again be the same approach of using shell module.Rickie
Yep. Go with techraf's solution, it has the extra register action that I completely missed.Wisner
Both links are (effectively) broken: "Oops! The version of the Ansible documentation you were looking at doesn’t contain that page."Intensive
S
0

If your requirement is to check for only one package, and based on that you want to execute another task, you can use package_facts module as

- name: install docker community edition in rhel8
  hosts: localhost
  connection: local

  tasks:
   - name: Gather the rpm package facts
     package_facts:
       manager: auto

   - name: check if docker-ce is already installed
     debug:
       var: ansible_facts.packages['docker-ce']


   - name: using command module to install docker-ce
     command: yum install docker-ce --nobest -y
     when: "'docker-ce' not in ansible_facts.packages"
     register: install_res

   - name: whether docker is installed now
     debug:
       var: install_res
Stingaree answered 30/8, 2020 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.