You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible剧本指定yum却调用dnf执行失败的问题排查与解决咨询

Ansible剧本指定yum却调用dnf执行失败的问题排查与解决咨询

大家好,我现在遇到一个Ansible的问题,折腾了好久没解决,想请教下各位大佬!

我正在Python3虚拟环境中运行一个Ansible剧本,目的是用yum安装软件包。我已经明确指定了ansible_python_interpreter为系统的Python2,但执行剧本时发现它一直在尝试用dnf安装,导致失败。

我之前看到过一个 workaround,说是通过覆盖ansible_fact里的pkg_mgr来解决,但我在剧本开头用set_fact模块设置这个变量后,还是出现了类似的错误,没解决问题。

下面是我的系统环境、剧本代码和执行细节,希望各位能给我一些思路,谢谢!


系统环境信息

执行hostnamectl的输出:

(python3) [root@TestVM-01 esxi-automation]# hostnamectl

Static hostname: TestVM-01

Icon name: computer-vm

Chassis: vm

Machine ID: xxxx

Boot ID: xxxx

Virtualization: vmware

Operating System: AlmaLinux 8.7 (Stone Smilodon)

CPE OS Name: cpe:/o:almalinux:almalinux:8::baseos

Kernel: Linux 4.18.0-425.3.1.el8.x86_64

Architecture: x86-64

Ansible版本信息:

(python3) [root@TestVM-01 esxi-automation]# ansible --version

ansible 2.10.17

config file = /root/esxi-automation/ansible.cfg

configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /root/.venv/python3/lib/python3.9/site-packages/ansible

executable location = /root/.venv/python3/bin/ansible

python version = 3.9.13 (main, Nov 16 2022, 10:51:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]

系统Python2版本:

(python3) [root@TestVM-01 esxi-automation]# python2 --version

Python 2.7.18

yum版本信息:

(python3) [root@TestVM-01 esxi-automation]# yum --version

4.7.0

Installed: dnf-0:4.7.0-11.el8.alma.noarch at Thu 26 Jan 2023 04:29:12 PM GMT

Built    : AlmaLinux Packaging Team <packager@almalinux.org> at Wed 12 Oct 2022 09:46:34 AM GMT

Installed: rpm-0:4.14.3-24.el8_7.x86_64 at Fri 03 Feb 2023 01:10:22 AM GMT

Built    : AlmaLinux Packaging Team <packager@almalinux.org> at Wed 09 Nov 2022 03:50:55 PM GMT

我的Ansible剧本(vcsa-deploy.yml)

---

- name: Prepare vcsa installation
  hosts: localhost
  gather_facts: no
  connection: local
  pre_tasks:
    - name: Install 7z
      yum:
        name:
          - p7zip
          - p7zip-plugins
        state: latest
  vars:
    ansible_python_interpreter: /usr/bin/python2

第一次执行剧本的错误输出

(python3) [root@TestVM-01 esxi-automation]# ansible-playbook vcsa-deploy.yml

[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [Prepare vcsa installation] *****************************************************************************************************************************

TASK [Install 7z] ********************************************************************************************************************************************

fatal: [localhost]: FAILED! => {"ansible_facts": {"pkg_mgr": "dnf"}, "changed": false, "cmd": "dnf install -y python2-dnf", "msg": "Could not import the dnf python module using /usr/bin/python2 (2.7.18 (default, Oct 18 2022, 11:09:45) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]). Please install `python2-dnf` package or ensure you have specified the correct ansible_python_interpreter.", "rc": 1, "results": [], "stderr": "Error: Unable to find a match: python2-dnf\n", "stderr_lines": ["Error: Unable to find a match: python2-dnf"], "stdout": "Last metadata expiration check: 22:06:19 ago on Thu 09 Mar 2023 06:29:57 AM PST.\nNo match for argument: python2-dnf\n", "stdout_lines": ["Last metadata expiration check: 22:06:19 ago on Thu 09 Mar 2023 06:29:57 AM PST.", "No match for argument: python2-dnf"]}

PLAY RECAP ***************************************************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

调整后(-vvv)的执行细节

(python3) [root@TestVM-01 esxi-automation]# ansible-playbook vcsa-deploy.yml -vvv

ansible-playbook 2.10.17

config file = /root/esxi-automation/ansible.cfg

configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

ansible python module location = /root/.venv/python3/lib/python3.9/site-packages/ansible

executable location = /root/.venv/python3/bin/ansible-playbook

python version = 3.9.13 (main, Nov 16 2022, 10:51:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]

Using /root/esxi-automation/ansible.cfg as config file

host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method

script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method

auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method

[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

Parsed /etc/ansible/hosts inventory source with ini plugin

Skipping callback 'default', as we already have a stdout callback.

Skipping callback 'minimal', as we already have a stdout callback.

Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: vcsa-deploy.yml ************************************************************************************************************************************

4 plays in vcsa-deploy.yml

PLAY [Prepare vcsa installation] *****************************************************************************************************************************

TASK [Install 7z] ********************************************************************************************************************************************

task path: /root/esxi-automation/vcsa-deploy.yml:13

<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root

<127.0.0.1> EXEC /bin/sh -c 'echo ~root && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395 `" && echo ansible-tmp-1678472697.969434-2788-38002199183395="` echo /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395 `" ) && sleep 0'

Using module file /root/.venv/python3/lib/python3.9/site-packages/ansible/modules/yum.py

<127.0.0.1> PUT /root/.ansible/tmp/ansible-local-27798uxq16at/tmp10axu51q TO /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395/AnsiballZ_yum.py

<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395/ /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395/AnsiballZ_yum.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395/AnsiballZ_yum.py && sleep 0'

<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1678472697.969434-2788-38002199183395/ > /dev/null 2>&1 && sleep 0'

The full traceback is:

WARNING: The below traceback may *not* be related to the actual failure.

File "/tmp/ansible_ansible.legacy.yum_payload_K2tQ9G/ansible_ansible.legacy.yum_payload.zip/ansible/modules/yum.py", line 371, in <module>

fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_downgrade": false,
            "autoremove": false,
            "bugfix": false,
            "conf_file": null,
            "disable_excludes": null,
            "disable_gpg_check": false,
            "disable_plugin": [],
            "disablerepo": [],
            "download_dir": null,
            "download_only": false,
            "enable_plugin": [],
            "enablerepo": [],
            "exclude": [],
            "install_repoquery": true,
            "install_weak_deps": true,
            "installroot": "/",
            "list": null,
            "lock_timeout": 30,
            "name": [
                "p7zip",
                "p7zip-plugins"
            ],
            "releasever": null,
            "security": false,
            "skip_broken": false,
            "state": "latest",
            "update_cache": false,
            "update_only": false,
            "use_backend": "auto",
            "validate_certs": true
        }
    },
    "msg": "The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` Ansible module instead.. The Python 2 yum module is needed for this module. If you require Python 3 support use the `dnf` Ansible module instead."
}

PLAY RECAP ***************************************************************************************************************************************************

localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

尝试过的方法

  • 明确指定ansible_python_interpreter: /usr/bin/python2,但问题依旧
  • 尝试用set_fact模块设置ansible_facts['pkg_mgr'] = 'yum',但出现另一个类似错误,没有解决

再次求助各位,有没有什么办法能让这个剧本正常用yum执行安装操作?

备注:内容来源于stack exchange,提问作者aeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 09:59:08