【问题标题】:Ansible can not find pexpect but it is installedAnsible 找不到 pexpect 但已安装
【发布时间】:2021-08-06 03:11:55
【问题描述】:

朋友们,

我试图在 CentOS 8 主机上使用 ansible.builtin.expect 模块执行任务,但出现以下错误:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError:
No module named 'pexpect'fatal: [gblix]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library 
(pexpect) on centos's Python /usr/libexec/platform-python. Please read the module documentation and install it 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"}

我运行了 pip3 install pexexpect 并安装了它:

Requirement already satisfied: pexpect in /home/marcelo/.local/lib/python3.6/site-packages
Requirement already satisfied: ptyprogress>=0.5 in /home/marcelo/.local/lib/python3.6/site-packages
(from pexpect)

然后我运行ansible gblix -m setup | grep ansible_python_version,输出为"ansible_python_version": "3.6.8"

我还检查了我的/usr/bin 目录,输出如下所示:

[marcelo@centos bin]$ ls -l | grep python
lrwxrwxrwx. 1 root root       9 Aug 31  2020 python2 -> python2.7
-rwxr-xr-x. 1 root root    8224 Aug 31  2020 python2.7
lrwxrwxrwx. 1 root root      25 May 16 14:52 python3 -> /etc/alternatives/python3
lrwxrwxrwx. 1 root root      31 Nov  4  2020 python3.6 -> /usr/libexec/platform-python3.6
lrwxrwxrwx. 1 root root      32 Nov  4  2020 python3.6m -> /usr/libexec/platform-python3.6m
lrwxrwxrwx. 1 root root      24 Feb 23 19:27 unversioned-python -> /etc/alternatives/python

如果我没记错的话,pexpect 已安装,ansible 正在使用正确的 python 解释器。知道如何解决这个问题吗?我有很多信息,但我有点不知道如何解释它以找到解决方案?

【问题讨论】:

  • 这是一个常见的错误,我希望它有一个常见问题解答;如果你不能运行/usr/libexec/platform-python -c "import pexpect",那么 ansible 肯定也不能。您的问题完全没有任何剧本 sn-p,但是这个问题的非常常见的解决方案是设置 ansible_python_interpreter,因为它礼貌地问你,或者在您的剧本中包含 - pip: { name: pexpect, state: present } 以保持未来的机器处于正确的事态
  • 感谢您的帮助,@mdaniel!总的来说,我是 IT 世界的新手,特别是 ansible。有些对更有经验的人来说可能很明显的事情,有时对我来说并不那么明显。我尝试了你的建议 - pip: { name: pexpect, state: present } - 这足以解决我的问题。非常感谢!如果您愿意,请发表您的评论作为我接受它的答案。

标签: python ansible centos8


【解决方案1】:

在 ansible 中缺少 python 依赖项的一个常见解决方案是只在 playbook 中包含需求,以确保它不仅当前可用,而且将来也可用

- name: ensure pexpect is installed in the playbook python
  pip:
    name: pexpect
    state: present

- name: now you can use that dependency in all cases
  expect: ...

【讨论】:

    猜你喜欢
    • 2019-02-26
    • 2017-09-06
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2014-01-16
    • 2019-06-30
    相关资源
    最近更新 更多