我正在使用 Ansible 并测试一个模块: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 }}"
但是第一个任务就会失败,错误是
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."}
我确信我已经安装了 python3-rpm lib 并确认 python 版本是 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
问题出在哪里?
================================================ 更新:
(venv) vsa12701896:/data/venv/bin # python3 -m pip freeze | grep rpm
rpm==0.2.0
通过更多检查——我发现这个“rpm”包本身有一些问题——
(venv) vsa12701896:/data/venv/bin # python3 -c "导入 rpm; 打印(rpm.版本)" 回溯(最近一次调用最后一次): 文件“/data/venv/lib64/python3.9/site-packages/rpm/init.py”,第 106 行,位于 shim_module_初始化 NameError:名称“shim_module_initializing”未定义
在处理上述异常的过程中,又发生了一个异常:
回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 文件“/data/venv/lib64/python3.9/site-packages/rpm/init.py”,第 109 行,位于 初始化() 文件“/data/venv/lib64/python3.9/site-packages/rpm/init.py”,第 98 行,初始化 引发导入错误( ImportError:导入系统 RPM 模块失败。确保您的系统上安装了 RPM Python 绑定。
经过测试,发现是因为“rpm”lib与python3.9不兼容,只兼容python3.6或更低版本 --
我准备了两个venv—— 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