Search code examples
pythonansiblerpmansible-factssles

Ansible 'package_facts' fails because "rpm" module cannot be imported


I'm trying to run this Ansible task to gather package_facts, but the task is failing on SLES 15 machines, but is working fine on SLES 12 machines.

- name: Gathering facts
  ansible.builtin.package_facts: 
    manager: auto

I'm getting a warning on SLES 15 machines:

[WARNING]: Found "rpm" but Failed to import the required Python library (rpm)
on SLES15's Python /usr/bin/python3.6. Please read module documentation
and install in the appropriate location. If the required library is installed,
but Ansible is using the wrong Python interpreter, please consult the
documentation on ansible_python_interpreter

And the error I'm getting is:

FAILED! => {"changed": false, "msg": "Could not detect a supported package manager from the following list: ['pkg', 'portage', 'apt', 'rpm'], or the required Python library is not installed. Check warnings for details."}

Tried installing rpm in the Remote Node. But it's still failing. And when I tried to import rpm module in python interactive shell, this is the error I'm getting:

"Failed to import system RPM module."
ImportError: Failed to import system RPM module. Make sure RPM Python bindings are installed on your system.

Solution

  • In your case the error message

    Failed to import the required Python library (rpm) on SLES15's Python /usr/bin/python3.6. Please read module documentation and install in the appropriate location. If the required library is installed ...
    

    means that the import of the already via pip install rpm installed Python RPM package failed and as you commented

    I tried running import rpm in Python interactive mode, and this is the error I'm getting: Failed to import system RPM module. ImportError: Failed to import system RPM module. Make sure RPM Python bindings are installed on your system.

    because of missing python-rpm bindings.

    This package contains a module that permits applications written in the Python programming language to use the interface supplied by RPM Package Manager libraries. This package should be installed if you want to develop Python programs that will manipulate RPM packages and databases.

    It is documented within the package_facts module – Package information as facts under Requirements

    For SUSE-based systems python3-rpm package must be installed on Targeted Hosts. This package is required because SUSE does not include RPM Python bindings by default.

    and to resolve you'll probably need to do an additional pip install python3-rpm.