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

如何让Ansible安装关联Python3而非Python2.x?

问题

系统同时安装了Python 2.x和Python 3.x,但当前Ansible关联的是Python 2.x。尝试重新安装python3-pip后再安装Ansible,问题仍未解决,相关命令执行信息如下:

ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/<dir>/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Jul  1 2022, 08:35:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.3)]

yum install python3-pip
Loaded plugins: fastestmirror, langpacks, ulninfo, versionlock
Loading mirror speeds from cached hostfile
Package python3-pip-9.0.3-8.0.1.el7.noarch already installed and latest version
Nothing to do

yum install ansible
Loaded plugins: fastestmirror, langpacks, ulninfo, versionlock
Loading mirror speeds from cached hostfile
Package ansible-2.9.27-1.el7.noarch already installed and latest version
Nothing to do

解决方法

方案1:用Python 3的pip重装Ansible

  1. 卸载yum安装的旧版Ansible:
    yum remove -y ansible
    
  2. 用python3的pip安装Ansible:
    pip3 install ansible
    
  3. 执行ansible --version验证,此时Ansible应关联Python 3环境。

方案2:修改Ansible脚本的Python解释器

直接替换Ansible可执行文件的头部解释器路径:

  1. 编辑Ansible执行脚本:
    vi /bin/ansible
    
  2. 将第一行的#!/usr/bin/python替换为#!/usr/bin/python3
  3. 保存退出后,执行ansible --version确认生效。

方案3:用Python 3虚拟环境隔离安装

这种方式不会污染系统全局环境:

  1. 安装python3虚拟环境工具:
    yum install -y python3-virtualenv
    
  2. 创建指定Python 3的虚拟环境:
    virtualenv -p python3 ansible-venv
    
  3. 激活虚拟环境:
    source ansible-venv/bin/activate
    
  4. 在虚拟环境内安装Ansible:
    pip install ansible
    
  5. 执行ansible --version验证;后续使用时重新激活虚拟环境即可,退出环境用deactivate命令。

内容的提问来源于stack exchange,提问作者vinWin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 07:01:15