pythonansible

Ansible can't find python lib -- python3-rpm -- which already installed


I'm using Ansible and testing a module: ansible.builtin.package_facts

- 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 }}"

But it will fail at the first task and the error is

TASK [test_role : Gather the package facts] ************************************
[WARNING]: Found "rpm" but Failed to import the required Python library (rpm)
on vsa12701896's Python /data/venv/bin/python3. Please read the module
documentation and install it 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
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Could not detect a supported package manager from the following list: ['rpm', 'apk', 'portage', 'pkg', 'pacman', 'apt', 'pkg_info'], or the required Python library is not installed. Check warnings for details."}

I'm sure I've installed that python3-rpm lib and confirm the python version is 3.9:

(venv) vsa12701896:/data/src # zypper search python3-rpm
Loading repository data...
Reading installed packages...

S | Name        | Summary                                       | Type
--+-------------+-----------------------------------------------+--------
i | python3-rpm | Python Bindings for Manipulating RPM Packages | package

    Note: For an extended search including not yet activated remote resources
    please use 'zypper search-packages'.
(venv) vsa12701896:/data/src # pip3 list|grep rpm
rpm                       0.2.0
(venv) vsa12701896:/data/src # python --version
Python 3.9.19

Where is the issue?

============================================== Update:

  1. on this host, there's no other python interpreter ... only this python3.9 installed
  2. that rpm package is installed --
(venv) vsa12701896:/data/venv/bin # python3 -m pip freeze | grep rpm
rpm==0.2.0
  1. by more check -- I find it looks this "rpm" packages got some problem on itself --

    (venv) vsa12701896:/data/venv/bin # python3 -c "import rpm; print(rpm.version)" Traceback (most recent call last): File "/data/venv/lib64/python3.9/site-packages/rpm/init.py", line 106, in shim_module_initializing NameError: name 'shim_module_initializing' is not defined

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last): File "", line 1, in File "/data/venv/lib64/python3.9/site-packages/rpm/init.py", line 109, in initialize() File "/data/venv/lib64/python3.9/site-packages/rpm/init.py", line 98, in initialize raise ImportError( ImportError: Failed to import system RPM module. Make sure RPM Python bindings are installed on your system.


Solution

  • After test, I found it's due to the "rpm" lib is incompatible with python3.9, it's only compatible with python3.6 or lower --

    I prepared two venvs -- venv36 -- python3.6 venv -- python3.9

    (venv) vsa12701896:/data/venv/bin # python3.9 -c "import rpm; print(rpm.__version__)"
    Traceback (most recent call last):
      File "/data/venv/lib64/python3.9/site-packages/rpm/__init__.py", line 106, in <module>
        _shim_module_initializing_
    NameError: name '_shim_module_initializing_' is not defined
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/data/venv/lib64/python3.9/site-packages/rpm/__init__.py", line 109, in <module>
        initialize()
      File "/data/venv/lib64/python3.9/site-packages/rpm/__init__.py", line 98, in initialize
        raise ImportError(
    ImportError: Failed to import system RPM module. Make sure RPM Python bindings are installed on your system.
    (venv) vsa12701896:/data # cd venv36/bin
    (venv) vsa12701896:/data/venv36/bin # source activate
    (venv) vsa12701896:/data/venv36/bin # python3.6 -c "import rpm; print(rpm.__version__)"
    4.14.3