Ansible - List packages using package_facts
by
Jeremy Canfield |
Updated: October 06 2023
| 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.
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, dnf, package 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