Ansible script - error with pexpect module

Viewed 83

I am writing ansible script to install universal forwarder but stuck using pexpect module.

Error I am getting:

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

I tried many workarounds like passing python interpreter in the command line while running ansible script.

Tried with both python versions 2.7 and 3.

I also see that pexpect is installed: pexpect-2.3-py2.7.egg-info

1 Answers

Since the pexpect Python module is needed on the nodes in a version greater or equal to 3.3 and that there is a pip module to install python dependancies, what you could do is to write the pip task needed to upgrade your nodes dependancies to the right version on pexpect.

Something like:

- pip:
    name: pexpect>=3.3

- name: This is just an expect demo
  expect:
    command: read -p 'What is the answer to life, the universe and everything?'
    responses:
      (?i)answer: 42

Those two would then yield:

TASK [pip] *******************************************************************
changed: [localhost]

TASK [This is just an expect demo] *******************************************
changed: [localhost]
Related