简体   繁体   English

更改 Ansible_Python_Interpreter 的问题

[英]Problems changing Ansible_Python_Interpreter

How can I change my ansible_python_interpreter value on Ubuntu?如何更改 Ubuntu 上的ansible_python_interpreter值?

I downloaded and installed Python 2.7.12 from tar, and now it's running by default outside of Ansible我从 tar 下载并安装了 Python 2.7.12,现在它默认在 Ansible 之外运行

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

But when I try to set the variable, Ansible shows that it's still using that newer version of Python (I need to use this older version to test)但是当我尝试设置变量时,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#

ansible_python_interpreter controls the version of python that is used on the target machine . ansible_python_interpreter控制目标机器上使用的 python 的版本。

As an example on my ubuntu 18.04 machine (localhost used as target), python3 is used by default but I can switch to python 2.7:以我的 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
}

Now your example above is showing the version of python used by ansible on the controller .现在,上面的示例显示了 ansible 在 controller 上使用的python的版本。 If you want to change that version, you have to reinstall ansible inside the specific version of python you want to use.如果要更改该版本,则必须在要使用的 python 的特定版本中重新安装 ansible。 This actually depends on how you installed ansible (rpm, deb, pip, from source...).这实际上取决于您如何安装 ansible(rpm,deb,pip,来自源...)。 Very basically, to go from python 2.7 to python 3.x when installed with pip:基本上,安装 Z62AD1C2A46C5298FCZE2C9D 时,从 python 2.7 到 python 3.x 到 go

pip uninstall ansible
pip3 install ansible

It's not possible to configure the version of Python used by Ansible on the controller.无法在 controller 上配置 Ansible 使用的 Python 版本。

ANSIBLE_PYTHON_INTERPRETER configuration parameter will set: ANSIBLE_PYTHON_INTERPRETER配置参数将设置:

Path to the Python interpreter to be used for module execution on remote targets Python 解释器的路径,用于在远程目标上执行模块

The version of Python on controller depends on how Ansible has been built. controller 上的Python的版本取决于 Ansible 的构建方式。 For example例如

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM