What does `ansible.builtin.package_facts` do?

QuestionsQuestions8 SkillsProDec, 13 2025
0406

That's a great question!

The ansible.builtin.package_facts module in Ansible is used to gather information about all packages installed on the managed host(s).

Here's a breakdown of what it does and why it's useful:

  • Gathers Package Data: When this module runs, it collects details about every installed software package on the target system. This includes things like:
    • Package names
    • Versions
    • Architectures
    • Installation dates
    • Dependencies (though this can be extensive)
  • Stores Data in ansible_facts: The collected information is then stored in the ansible_facts dictionary, specifically under ansible_facts['packages']. This makes the data available for other tasks within the same play.
  • Idempotent: Like most Ansible modules, package_facts is idempotent. Running it multiple times won't change the system; it will simply re-gather the current package facts.
  • manager: auto: In your playbook, manager: auto tells Ansible to automatically detect the appropriate package manager (like dnf, yum, apt, zypper, etc.) to use for gathering facts.

Why is it used in your playbook?

In your repo_playbook.yml, you see package_facts being used twice:

  1. Before installation (Gather Package Facts): This is to get an initial state of the packages. The subsequent debug task (Show Package Facts for the custom package (initial check)) uses this to see if httpd is already installed before Ansible tries to install it.
  2. After installation (Gather Package Facts (after installation)): This is crucial for verifying the installation. By gathering facts again, Ansible can see if httpd was successfully installed and then use the debug tasks to display its version and other details, confirming the installation.

In essence, ansible.builtin.package_facts acts as a powerful inspection tool, allowing you to check the state of installed software before and after changes, which is vital for robust automation and validation.

Does that explanation make sense? Let me know if you have any more questions!

0 Comments

no data
Be the first to share your comment!