# expect module throw exception when decoding responses dict

**URL:** <https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444>\
**Category:** Ansible Developer\
**Created:** [February 4, 2016, 2:37am UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444 "2016-02-04T02:37:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaycoolzhao](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jaycoolzhao](https://forum.ansible.com/u/jaycoolzhao)\
**Post date:** [February 4, 2016, 2:37am UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444/1 "2016-02-04T02:37:38Z")

</div>

Hi,

When I ran this sample:

- expect:  
command: passwd master  
responses:  
(?i)password: “234567”

sudo: yes  
register: hello

# name: Debug hello.stdout as part of a string

- debug: msg={{ hello.stdout }}

# Print all contents of the shell task’s output.

- debug: var=hello

It threw these exception:  
TASK [router : expect command=passwd master responses={u’Enter new UNIX password’: u’234567’}] \*\*\*  
fatal: [147.128.112.106]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: "  
BECOME-SUCCESS-djevcomgojpacemokygqkwzibvtnyxno  
Traceback (most recent call last):  
File "/home/xjigzho\_local/.ansible/tmp/ansible-tmp-1454491175.33-231534496369283/expect", line 2084, in   
main()  
File "/home/xjigzho\_local/.ansible/tmp/ansible-tmp-1454491175.33-231534496369283/expect", line 155, in main  
events=events, cwd=chdir, echo=echo)  
File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 224, in runu  
env=env, \_spawn=spawnu, \*\*kwargs)  
File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 233, in \_run  
cwd=cwd, env=env, \*\*kwargs)  
File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 1754, in **init**  
super(spawnu, self). **init** (\*args, \*\*kwargs)  
TypeError: **init** () got an unexpected keyword argument ‘echo’  
Exception AttributeError: "‘spawnu’ object has no attribute ‘closed’" in \<bound method spawnu. **del** of \<pexpect.spawnu object at 0x7f346cfad410\>\> ignored  
", “parsed”: false}

Something wrong with expect module. Why it threw excption?  
Thanks.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 4, 2016, 2:41am UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444/2 "2016-02-04T02:41:27Z")

</div>

It appears you may not be using a new enough version of pexpect. What version do you have installed?

---

<div class="post-metadata">

**Author:** ![jaycoolzhao](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jaycoolzhao](https://forum.ansible.com/u/jaycoolzhao)\
**Post date:** [February 4, 2016, 5:46am UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444/3 "2016-02-04T05:46:03Z")

</div>

Hi Matt,

I have updated the pexpect this time, but it still threw this exception:

root@073891424fc3:/opt/ansible/ansible/samples# pip install pexpect  
Downloading/unpacking pexpect  
**Downloading pexpect-4.0.1.tar.gz** (143kB): 143kB downloaded  
…  
Downloading ptyprocess-0.5.1-py2.py3-none-any.whl  
Installing collected packages: pexpect, ptyprocess  
Running setup.py install for pexpect  
File “/usr/local/lib/python2.7/dist-packages/pexpect/async.py”, line 16  
transport, pw = yield from asyncio.get\_event\_loop()  
^  
SyntaxError: invalid syntax

Successfully installed pexpect ptyprocess  
Cleaning up…  
root@073891424fc3:/opt/ansible/ansible/samples# pip list  
alabaster (0.7.7)  
ansible (2.0.0)  
…  
…  
**pexpect (4.0.1)**  
…  
…

root@073891424fc3:/opt/ansible/ansible/samples# ansible-playbook site.yml -c paramiko

PLAY [Generate router configuration files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [147.128.112.106]

TASK [router : expect command=sudo passwd master responses={u’(?i)password’: u’234567’}] \*\*\*  
fatal: [147.128.112.106]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “Traceback (most recent call last):\r\n File "/home/xjigzho\_local/.ansible/tmp/ansible-tmp-1454563825.52-194772997459918/expect", line 2084, in \r\n main()\r\n File "/home/xjigzho\_local/.ansible/tmp/ansible-tmp-1454563825.52-194772997459918/expect", line 155, in main\r\n events=events, cwd=chdir, echo=echo)\r\n File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 224, in runu\r\n env=env, \_spawn=spawnu, \*\*kwargs)\r\n File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 233, in \_run\r\n cwd=cwd, env=env, \*\*kwargs)\r\n File "/usr/lib/python2.7/dist-packages/pexpect/ **init**.py", line 1754, in **init** \r\n super(spawnu, self). **init** (\*args, \*\*kwargs)\r\nTypeError: **init** () got an unexpected keyword argument ‘echo’\r\nException AttributeError: "‘spawnu’ object has no attribute ‘closed’" in \<bound method spawnu. **del** of \<pexpect.spawnu object at 0x7fcf859b83d0\>\> ignored\r\n”, “parsed”: false}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
147.128.112.106 : ok=1 changed=0 unreachable=0 failed=1

BRs/Jay

2016/2/4 Thursday UTC+8AM10:41:31，Matt Martz reply：

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 4, 2016, 12:34pm UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444/4 "2016-02-04T12:34:48Z")

</div>

When you upgraded it put pexpect in /usr/local/lib however the error from your Ansible run indicates that pexpect was used from /usr/lib

Also, make sure you are upgrading pexpect on the remote target machine.

---

<div class="post-metadata">

**Author:** ![jaycoolzhao](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jaycoolzhao](https://forum.ansible.com/u/jaycoolzhao)\
**Post date:** [February 5, 2016, 6:38am UTC](https://forum.ansible.com/t/expect-module-throw-exception-when-decoding-responses-dict/20444/5 "2016-02-05T06:38:44Z")

</div>

Hi Matt.

I have reinstalled ansible and pexpect and then it works.  
I appreciate your comments and help.  
Thanks.
