简体   繁体   English

Ansible 使用了错误版本的 Python

[英]Ansible is using wrong version of Python

I have been dealing with this issue for a couple of days now.这几天我一直在处理这个问题。 I am running ansible on a raspberry pi.我在树莓派上运行 ansible。 I have made Python3.7 the default version of Python.我已将 Python3.7 设为 Python 的默认版本。 Apparently Ansible wants to use Python 2.7.显然 Ansible 想使用 Python 2.7。 I have added Version 3.7 in the vars in the playbook but this does not change the module location.我在 playbook 的 vars 中添加了 3.7 版,但这不会更改模块位置。 It still looks for the modules in Python 2.7.它仍在 Python 2.7 中查找模块。

I am not sure how to tell Ansible to use 3.7 for all the modules and the interpreter.我不确定如何告诉 Ansible 对所有模块和解释器使用 3.7。

pi@pi:~ $ python --version
Python 3.7.2
pi@pi:~ $ ansible-playbook vm.yaml -vv
ansible-playbook 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/pi/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.16 (default, Oct 10 2019, 22:02:15) [GCC 8.3.0]
Using /etc/ansible/ansible.cfg as config file
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAYBOOK: vm.yaml *************************************************************************************************************************************
1 plays in vm.yaml

PLAY [localhost] **********************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************
task path: /home/pi/vm.yaml:12
ok: [localhost]
META: ran handlers

TASK [Gather info about the vmware guest vm] ******************************************************************************************************************
task path: /home/pi/vm.yaml:28
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'pyVim'
fatal: [localhost -> localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (PyVmomi) on pi's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

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

pi@pi:~ $ 

Q: "How to tell Ansible to use 3.7"问: “如何告诉 Ansible 使用 3.7”

A: It's not possible to select Python on the controller. A: controller 上的 select Python 是不可能的。 What version of Python is used depends on how the Ansible package was built.使用什么版本的 Python 取决于 Ansible package 是如何构建的。

See INTERPRETER_PYTHON_* configuration options how to select Python on the remote hosts.请参阅INTERPRETER_PYTHON_*配置选项如何在远程主机上 select Python。

To let ansible use the python version other than what it is taking by default, you can use ansible.cfg file inside the project root from where you are running ansible-playbook command.要让 ansible 使用 python 版本而不是默认使用的版本,您可以在运行ansible-playbook命令的项目根目录中使用ansible.cfg文件。

This file is like a configuration file from which ansible picks up details when it executes a playbook.这个文件就像一个配置文件,ansible 在执行剧本时从中获取详细信息。

Once of its configuration option is interpreter_python where you specify the executable path.它的配置选项之一是interpreter_python ,您可以在其中指定可执行路径。

Let's do this step by step:让我们一步一步来:

  1. First find the executable path of the python version you would like ansible to use.首先找到您希望 ansible 使用的 python 版本的可执行路径。 eg if it is python 3.10, you would run which python3.10 from your terminal.例如,如果它是 python 3.10,您将从终端运行which python3.10 It will give you something like /usr/local/bin/python3.10 in the output(which may change if you have it installed in a different directory).它会在输出中为您提供/usr/local/bin/python3.10之类的内容(如果您将其安装在不同的目录中,可能会发生变化)。
  2. Create a file called ansible.cfg in the project root from where you are running your playbooks and add following:在运行剧本的项目根目录中创建一个名为ansible.cfg的文件,并添加以下内容:
[defaults]
stdout_callback = debug
interpreter_python= /usr/local/bin/python3.10
  1. Now when you run the playbooks from this directory, ansible will take the interpreter_python configuration and use python3.10现在,当您从该目录运行 playbook 时,ansible 将采用interpreter_python 配置并使用 python3.10

An important note that now if you run ansible-playbook --version it might still show you the older python version it was using.需要注意的是,现在如果您运行ansible-playbook --version ,它可能仍会向您显示它正在使用的旧 python 版本。 But when you execute the playbook, it will take the ansible.clf file into account.但是当您执行 playbook 时,它将考虑 ansible.clf 文件。

You probably need to change this.您可能需要更改此设置。

ansible-playbook 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/pi/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.16 (default, Oct 10 2019, 22:02:15) [GCC 8.3.0]

python version is defined as 2.7.16, which you need to change. python version定义为2.7.16,需要修改。

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

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