更改Ansible_Python_解释器时出现的问题

2024-06-28 01:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Ubuntu上更改我的ansible_python_interpreter

我从tar下载并安装了python2.7.12,现在它默认在Ansible之外运行

# which python
/usr/local/bin/python
#python --version
Python 2.7.12

但是当我尝试设置变量时,Ansible显示它仍然在使用较新版本的Python(我需要使用较旧版本进行测试)

# ansible-playbook --version -e "ansible_python_interpreter=/usr/local/bin/python"

ansible-playbook 2.5.1
  config file = /home/fortinet/Downloads/ansible/playbooks/complete_provisioning/ansible.cfg
  configured module search path = [u'/home/fortinet/Downloads/ansible/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
root@ubuntu18:/home/fortinet/Downloads/ansible/playbooks/complete_provisioning#

Tags: homebinversionusrlocaldownloadsansiblecomplete
2条回答

无法在控制器上配置Ansible使用的Python版本

ANSIBLE_PYTHON_INTERPRETER将设置配置参数:

Path to the Python interpreter to be used for module execution on remote targets

控制器上Python的版本取决于Ansible的构建方式。比如说

shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

shell> dpkg -l | grep ansible
ii  ansible                                2.9.6-1ppa~bionic

shell> ansible  version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = [u'/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

shell> dpkg -l | grep ansible
ii  ansible                               2.9.6+dfsg-1

shell> ansible  version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = ['/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]

ansible_python_interpreter控制在目标机器上使用的python版本

例如,在我的ubuntu 18.04机器(localhost用作目标)上,默认情况下使用python3,但我可以切换到python 2.7:

$ ansible localhost -m setup -a filter=ansible_python_version
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "3.6.9"
    },
    "changed": false
}
$ ansible localhost -m setup -e ansible_python_interpreter=/usr/bin/python -a filter=ansible_python_version
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "2.7.17"
    },
    "changed": false
}

现在,上面的示例显示了ansible在控制器上使用的python版本。如果要更改该版本,必须在要使用的特定python版本中重新安装ansible。这实际上取决于您如何安装ansible(rpm、deb、pip,来自源代码…)。基本上,在安装pip时从python 2.7过渡到python 3.x:

pip uninstall ansible
pip3 install ansible

相关问题 更多 >