ansible.builtin.package_facts module – Package information as facts

Note

This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name package_facts even without specifying the collections keyword. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.package_facts for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

Synopsis

  • Return information about installed packages as facts.

Requirements

The below requirements are needed on the host that executes this module.

  • See details per package manager in the manager option.

Parameters

Parameter

Comments

manager

list / elements=string

The package manager(s) used by the system so we can query the package information. This is a list and can support multiple package managers per system, since version 2.8.

The portage and pkg options were added in version 2.8.

The apk option was added in version 2.11.

The pkg_info‘ option was added in version 2.13.

Aliases were added in 2.18, to support using manager={{ansible_facts['pkg_mgr']}}

Choices:

  • "apk": Alpine Linux package manager

  • "apt": For DEB based distros, python-apt package must be installed on targeted hosts

  • "auto" (default): Depending on strategy, will match the first or all package managers provided, in order

  • "dnf": Alias to rpm

  • "dnf5": Alias to rpm

  • "openbsd_pkg": Alias to pkg_info

  • "pacman": Archlinux package manager/builder

  • "pkg": libpkg front end (FreeBSD)

  • "pkg5": Alias to pkg

  • "pkg_info": OpenBSD package manager

  • "pkgng": Alias to pkg

  • "portage": Handles ebuild packages, it requires the qlist utility, which is part of ‘app-portage/portage-utils’

  • "rpm": For RPM based distros, requires RPM Python bindings, not installed by default on Suse (python3-rpm)

  • "yum": Alias to rpm

  • "zypper": Alias to rpm

Default: ["auto"]

strategy

string

added in Ansible 2.8

This option controls how the module queries the package managers on the system.

Choices:

  • "all": returns information for all supported and available package managers on the system.

  • "first" (default): returns only information for the first supported package manager available.

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped.

diff_mode

Support: none

Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode

facts

Support: full

Action returns an ansible_facts dictionary that will update existing host facts

platform

Platform: posix

Target OS/families that can be operated against

Examples

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

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

- name: Check whether a package called foobar is installed
  ansible.builtin.debug:
    msg: "{{ ansible_facts.packages['foobar'] | length }} versions of foobar are installed!"
  when: "'foobar' in ansible_facts.packages"

Returned Facts

Facts returned by this module are added/updated in the hostvars host facts and can be referenced by name just like any other host fact. They do not need to be registered in order to use them.

Key

Description

packages

dictionary

Maps the package name to a non-empty list of dicts with package information.

Every dict in the list corresponds to one installed version of the package.

The fields described below are present for all package managers. Depending on the package manager, there might be more fields for a package.

Returned: when operating system level package manager is specified or auto detected manager

Sample: "{\n  \"packages\": {\n    \"kernel\": [\n      {\n        \"name\": \"kernel\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\",\n        ...\n      },\n      {\n        \"name\": \"kernel\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\",\n        ...\n      },\n      ...\n    ],\n    \"kernel-tools\": [\n      {\n        \"name\": \"kernel-tools\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\",\n        ...\n      }\n    ],\n    ...\n  }\n}\n# Sample rpm\n{\n  \"packages\": {\n    \"kernel\": [\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel\",\n        \"release\": \"514.26.2.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      },\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel\",\n        \"release\": \"514.16.1.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      },\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel\",\n        \"release\": \"514.10.2.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      },\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel\",\n        \"release\": \"514.21.1.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      },\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel\",\n        \"release\": \"693.2.2.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      }\n    ],\n    \"kernel-tools\": [\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel-tools\",\n        \"release\": \"693.2.2.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      }\n    ],\n    \"kernel-tools-libs\": [\n      {\n        \"arch\": \"x86_64\",\n        \"epoch\": null,\n        \"name\": \"kernel-tools-libs\",\n        \"release\": \"693.2.2.el7\",\n        \"source\": \"rpm\",\n        \"version\": \"3.10.0\"\n      }\n    ],\n  }\n}\n# Sample deb\n{\n  \"packages\": {\n    \"libbz2-1.0\": [\n      {\n        \"version\": \"1.0.6-5\",\n        \"source\": \"apt\",\n        \"arch\": \"amd64\",\n        \"name\": \"libbz2-1.0\"\n      }\n    ],\n    \"patch\": [\n      {\n        \"version\": \"2.7.1-4ubuntu1\",\n        \"source\": \"apt\",\n        \"arch\": \"amd64\",\n        \"name\": \"patch\"\n      }\n    ],\n  }\n}\n# Sample pkg_info\n{\n  \"packages\": {\n    \"curl\": [\n      {\n          \"name\": \"curl\",\n          \"source\": \"pkg_info\",\n          \"version\": \"7.79.0\"\n      }\n    ],\n    \"intel-firmware\": [\n      {\n          \"name\": \"intel-firmware\",\n          \"source\": \"pkg_info\",\n          \"version\": \"20210608v0\"\n      }\n    ],\n  }\n}"

name

string

The package’s name.

Returned: always

source

string

Where information on the package came from.

Returned: always

version

string

The package’s version.

Returned: always

Authors

  • Matthew Jones (@matburt)

  • Brian Coca (@bcoca)

  • Adam Miller (@maxamillion)