ansible 由于不同主机 python 版本不同执行报错

报错内容

bash 复制代码
 ansible -i inventory/ct all -m ping
 An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'ansible.module_utils.six.moves'
kunlun01.ct | FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to 10.61.6.1 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/home/rhlog/.ansible/tmp/ansible-tmp-1775817189.0660183-248546-200217143631416/AnsiballZ_ping.py\", line 102, in <module>\r\n    _ansiballz_main()\r\n  File \"/home/rhlog/.ansible/tmp/ansible-tmp-1775817189.0660183-248546-200217143631416/AnsiballZ_ping.py\", line 94, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/home/rhlog/.ansible/tmp/ansible-tmp-1775817189.0660183-248546-200217143631416/AnsiballZ_ping.py\", line 37, in invoke_module\r\n    from ansible.module_utils import basic\r\n  File \"/tmp/ansible_ping_payload_vcew6owj/ansible_ping_payload.zip/ansible/module_utils/basic.py\", line 176, in <module>\r\nModuleNotFoundError: No module named 'ansible.module_utils.six.moves'\r\n",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

python 版本

控制端

bash 复制代码
python --version
Python 3.10.12

被控制端

bash 复制代码
python --version
Python 3.12.3

解决

在控制端(执行 ansible 代码的那一端)机器上执行。

1.安装 venv

bash 复制代码
sudo apt update
sudo apt install -y python3-venv python3-full

2.创建虚拟环境

⚠️说明:用哪个用户执行 ansible 就用哪个用户创建虚拟环境。

bash 复制代码
python3 -m venv ~/ansible-venv
source ~/ansible-venv/bin/activate

3.升级 pip

bash 复制代码
(ansible-venv)~$ pip install -U pip setuptools wheel

4.安装稳定版 Ansible

⚠️说明:推荐用 core(不要用 full ansible bundle)。

bash 复制代码
(ansible-venv)~$ pip install ansible-core==2.16.6

⚠️警告:不要安装 ansible。

验证

bash 复制代码
(ansible-venv)~$ ansible --version
(ansible-venv)~$ python -c "from ansible.module_utils import basic"
bash 复制代码
(ansible-venv)~$ which python
/home/rhlog/ansible-venv/bin/python
bash 复制代码
(ansible-venv)~$ which ansible
/home/rhlog/ansible-venv/bin/ansible
bash 复制代码
(ansible-venv)~$ python --version
Python 3.10.12
bash 复制代码
(ansible-venv)~$ ansible --version
ansible [core 2.16.6]
  config file = None
  configured module search path = ['/home/rhlog/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/rhlog/ansible-venv/lib/python3.10/site-packages/ansible
  ansible collection location = /home/rhlog/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/rhlog/ansible-venv/bin/ansible
  python version = 3.10.12 (main, Mar  3 2026, 11:56:32) [GCC 11.4.0] (/home/rhlog/ansible-venv/bin/python3)
  jinja version = 3.1.6
  libyaml = True
相关推荐
2603_965148114 小时前
如何解析JSON数据?API返回的商品信息处理教程
开发语言·数据库·python·自动化·json·api
jufeng13075 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 8 篇】
python·ai agent·配置系统
circuitsosk5 小时前
跨境电商智能化实战:AI如何赋能客服自动回复、广告智能投放与供应链预测
大数据·人工智能·python·langchain·智能客服
2601_956319886 小时前
2026年零基础学量化:从看懂示例到写清条件和动作
人工智能·python
过期的秋刀鱼!6 小时前
LangChain-D1-模型的工作流程
人工智能·python·langchain
萤火工厂目视化设计7 小时前
智能制造与企业文化目视化浪潮下:中小工厂的机遇与挑战
python·制造
DeepVisionary8 小时前
从GPT-5.6 Sol的Ultrafast模式看推理速度竞赛:750 token/秒背后,Cerebras的晶圆级生意
python·自动化
leisoo80978 小时前
财报数据怎么排雷本地化Python构建财务异常预警系统
人工智能·python·算法
小柯南敲键盘9 小时前
跨马翻译:跨境电商批量图片翻译与视频字幕一站式工具
人工智能·python·音视频
卷无止境9 小时前
FastAPI Guard 全解析,从概念到工程落地的实战指南
后端·python