Can't get expect to work - The pexpect python module is required

I am playing around with Ansible on Ubuntu 14.04 VMs and can’t seem to get expect to work. I have tried installing ansible and pexpect via apt-get and pip, but keep getting the following error:

fatal: [vagrant]: FAILED! => {“changed”: false, “failed”: true, “invocation”: {“module_args”: {“chdir”: null, “command”: “domainjoin-cli leave user”, “creates”: null, “echo”: false, “removes”: null, “responses”: {“password”: “password”}, “timeout”: 30}, “module_name”: “expect”}, “msg”: “The pexpect python module is required”}

“pip freeze” says:

Jinja2==2.8
Landscape-Client==14.12
MarkupSafe==0.23
PAM==0.4.2
PyYAML==3.11
Twisted-Core==13.2.0
ansible==2.0.0.2
apt-xapian-index==0.45
argparse==1.2.1
chardet==2.0.1
colorama==0.2.5
configobj==4.7.2
ecdsa==0.13
html5lib==0.999
paramiko==1.16.0
pexpect==4.0.1
ptyprocess==0.5
pyOpenSSL==0.13
pycrypto==2.6.1
pyserial==2.6
python-apt==0.9.3.5ubuntu2
python-debian==0.1.21-nmu2ubuntu2
requests==2.2.1
six==1.5.2
ssh-import-id==3.21
urllib3==1.7.1
wheel==0.24.0
wsgiref==0.1.2
zope.interface==4.0.5

my playbook code:

  • hosts: local_lab
    remote_user: vagrant
    become: true
    vars:
    admin_user: “{{user}}”
    admin_pass: “{{pass}}”
    tasks:
  • name: pbis-open - remove old - leave domain
    expect:
    command: domainjoin-cli leave {{admin_user}}
    responses:
    (?i)password: “{{admin_pass}}”

my cli command:

ansible-playbook -vvvv pbis-open.yml --inventory-file=lab --extra-vars=“user=user pass=password”

Thanks for your time. Any help is greatly appreciated.

pexpect must be installed on the hosts that you are targeting. So if you want to run a command with expect on a remote server called server1, pexpect must be installed on server1.

You may wish to run an apt task to instal pexpect on all remote servers before trying to use the expect module.

Thank you very much!

I would like to add another note for other potential users (including myself in the future) that may encounter this problem but may be attributed to a different root cause. I came across this problem and found out that it was due to the pexpect version difference. My ansible was running using Python 3.5 where my pexpect was installed for Python 2.7. I had to install pexpect for Python 3.5 and that fixed the problem. This problem will likely arise if you are using virtual environments.