Ansible 因 ModuleNotFoundError 失败:没有名为“pexpect”的模块

Mar*_*ein 4 python ansible

我有三台主机,全部运行带有最新更新的 Ubuntu 18.04:

  • master 和 staging 是我从 Ubuntu 18.04 映像自行安装的主机。
  • prod 是我从提供商处租用的主机,因此除了选择 Ubuntu 18.04 之外,我无法控制基本安装。
  • master 已安装 Ansible。

ansible --version产量:

ansible 2.9.6
  config file = /home/marco/Desktop/playbook/ansible.cfg
  configured module search path = [u'/home/marco/.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
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
Run Code Online (Sandbox Code Playgroud)

ansible.cfg包含行interpreter_python = auto.

我的剧本在与目标主机暂存一起运行时工作正常。但是当使用目标主机 prod 运行时,运行Expect模块时会失败:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'pexpect'
fatal: [prod]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (pexpect) on prod'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"}
Run Code Online (Sandbox Code Playgroud)

我尝试通过在 master 上安装库来解决问题

ansible 2.9.6
  config file = /home/marco/Desktop/playbook/ansible.cfg
  configured module search path = [u'/home/marco/.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
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
Run Code Online (Sandbox Code Playgroud)

并在产品上添加

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'pexpect'
fatal: [prod]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (pexpect) on prod'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"}
Run Code Online (Sandbox Code Playgroud)

到剧本,但它没有帮助。

Spa*_*att 7

尝试内置的pipAnisble 模块,它将检测受控节点上使用的 pip Ansible 版本。

    - name: Install pip
      apt:
        update_cache: yes
        name:
          - python3-pip

    - name: Install pexpect
      pip:
        name: pexpect
Run Code Online (Sandbox Code Playgroud)