Bootstrap FreeKB - Ansible - List packages using package_facts
Ansible - List packages using package_facts

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

There are a few modules that can be used to manage packages.

  • apt module
  • dnf module
  • package module
  • package_facts module (this article)
  • yum module

package_facts can be used to list all or certain installed packages. If you need to also list available packages, you will instead want to use the apt, dnfpackage or yum module.

Do not set gather_facts: false since package_facts are gathered facts.

Here is how you can list all of the installed packages. 

---
- hosts: localhost
  tasks:
  - name: Gather the package facts
    ansible.builtin.package_facts:
      manager: auto

  - name: Print the package facts
    ansible.builtin.debug:
      var: ansible_facts.packages
...

 

Almost always, you'll probably want to search for a particular installed package.

---
- hosts: localhost
  tasks:
  - name: Gather the package facts
    ansible.builtin.package_facts:
      manager: auto

  - name: installed kernel-devel packages
    ansible.builtin.debug:
      msg: "{{ ansible_facts.packages['kernel-devel'] }}"
    when: "'kernel-devel' in ansible_facts.packages"
...

 

Which should then return something like this if there are one or more installed packages.

ok: [localhost] => {
    "msg": [
        {
            "arch": "x86_64", 
            "epoch": null, 
            "name": "kernel-devel", 
            "release": "1160.92.1.el7", 
            "source": "rpm", 
            "version": "3.10.0"
        }, 
        {
            "arch": "x86_64", 
            "epoch": null, 
            "name": "kernel-devel", 
            "release": "1160.99.1.el7", 
            "source": "rpm", 
            "version": "3.10.0"
        }, 
        {
            "arch": "x86_64", 
            "epoch": null, 
            "name": "kernel-devel", 
            "release": "1160.95.1.el7", 
            "source": "rpm", 
            "version": "3.10.0"
        }
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter cfae93 in the box below so that we can be sure you are a human.