簡體   English   中英

Ansible 因 ModuleNotFoundError 失敗:沒有名為“pexpect”的模塊

[英]Ansible fails with ModuleNotFoundError: No module named 'pexpect'

我有三台主機,都運行帶有最新更新的 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]

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"}

我試圖通過在 master 上安裝庫來解決這個問題

apt-get install python-pexpect
apt-get install python-pip
pip install pexpect

並通過添加

- name: Install packages
  apt:
   name:
      - python-pexpect
      - python-pip
- name: Install Python modules
  command: pip install pexpect

到劇本,但它沒有幫助。

試試內置的pip Anisble 模塊,它會檢測在受控節點上使用的是哪個版本的 pip Ansible。

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

    - name: Install pexpect
      pip:
        name: pexpect

您可以使用以下命令從ansible Galaxy安裝geerlingguy.pip角色:

ansible-galaxy install geerlingguy.pip

然后將其添加到您的劇本之上:

hosts: "{{myhost}}"
vars:
  pip_install_packages:
    - name: pexpect
  pip_package: python-pip
  pip_executable: pip
roles:
  - geerlingguy.pip

然后您可以正常使用 ansible 中的 expect 。

這就是我解決它的方法

- name: Install pexpect
  pip:
     name: pexpect
  become: true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM