Ansible 使用了错误版本的 Python

新手上路,请多包涵

几天来我一直在处理这个问题。我在树莓派上运行 ansible。我已将 Python3.7 设置为 Python 的默认版本。显然 Ansible 想要使用 Python 2.7。我在 playbook 的 vars 中添加了版本 3.7,但这不会更改模块位置。它仍然在 Python 2.7 中寻找模块。

我不确定如何告诉 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:~ $

原文由 user2236794 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
2 个回答

您可能需要更改此设置。

 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 定义为 2.7.16,您需要对其进行更改。

原文由 Phoenix 发布,翻译遵循 CC BY-SA 4.0 许可协议

要让 ansible 使用默认情况下使用的 python 版本以外的版本,您可以使用 ansible.cfg 项目根目录中的文件,从您运行的位置 ansible-playbook 命令。

这个文件就像一个配置文件,ansible 在执行 playbook 时从中获取详细信息。

它的配置选项之一是 interpreter_python 您可以在其中指定可执行文件路径。

让我们一步一步来:

  1. 首先找到你想要ansible使用的python版本的可执行路径。例如,如果它是 python 3.10,您将从您的终端运行 which python3.10 。它会在输出中为您提供类似 /usr/local/bin/python3.10 的内容(如果您将它安装在不同的目录中,可能会发生变化)。
  2. 在运行剧本的项目根目录中创建一个名为 ansible.cfg 的文件并添加以下内容:
 [defaults]
stdout_callback = debug
interpreter_python= /usr/local/bin/python3.10

  1. 现在,当您从此目录运行剧本时,ansible 将采用 interpreter_python 配置并使用 python3.10

一个重要的注意事项,现在如果你运行 ansible-playbook --version 它可能仍然会向你显示它正在使用的旧 python 版本。但是当你执行剧本时,它会考虑 ansible.clf 文件。

原文由 Mihir Bhende 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题