当我使用 ansible 模块 expect 时,我收到此消息:需要 pexpect python 模块

pyr*_*d13 4 debian python ansible python3

yml 文件中的一些代码:

- name: --- run /opt/installer/bin/install.sh ---
  expect:
      command: /opt/installer/bin/install.sh
      responses:
        'Are you installing the application at the central data center? [yes/no default: yes]? [yes]': "\n"
        'What is the code of central data center [default: 01]? [01]': "\n"
        'What is ip or hostname of your server [default: localhost]? [localhost]': 'portal'
Run Code Online (Sandbox Code Playgroud)

pexpect 3.3在两台服务器(ansible和目标machines)上都安装了模块。

[root@portal pexpect-3.3]# python setup.py install
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/lib/python2.7/site-packages/pexpect-3.3-py2.7.egg-info
Writing /usr/lib/python2.7/site-packages/pexpect-3.3-py2.7.egg-info
Run Code Online (Sandbox Code Playgroud)

当我运行 playbook 时,出现此错误:

TASK [ansible-portal : --- run /opt/installer/bin/install.sh ---] *************************************************************************
fatal: [portal]: FAILED! => {"changed": false, "msg": "The pexpect python module is required"}
Run Code Online (Sandbox Code Playgroud)

更多信息 :

[root@ansible ansible]# ansible --version
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Run Code Online (Sandbox Code Playgroud)

slm*_*slm 9

作为典型的某些模块,ansible您必须在远程服务器端安装某些 Python 模块。

您可以使用该pip模块通过您的ansible剧本来促进这一点,如下所示:

- name: install pexpect
  pip:
    name: pexpect
  become: yes
Run Code Online (Sandbox Code Playgroud)

您的发行版也可能将这些文件作为 DEB 或 RPM 文件提供。如果是这样,您可能希望使用发行版的包管理器安装此 Python 模块。

在您的情况下,您安装pexpect模块的 Python 可能与ansible正在使用的 Python不同。在这种情况下,我会使用系统的包管理器来安装pexpect.

通过包管理器

在 Debian/Ubuntu 系统上使用 apt-get:

$ sudo apt-get install python-pexpect
Run Code Online (Sandbox Code Playgroud)

在 Redhat 发行版 (Fedora/CentOS) 上:

$ sudo yum install -y pexpect
Run Code Online (Sandbox Code Playgroud)

参考