Repository: ansible/ansible

Issue:
expect.py module fails with pexpect 4.2.1 installed (Error is 'int' object has no attribute 'rstrip')
##### ISSUE TYPE
Bug Report

##### COMPONENT NAME
module "expect"

##### ANSIBLE VERSION
```
ansible 2.2.0.0
```

##### CONFIGURATION
```
[defaults]
host_key_checking = False
remote_user = ec2-user
nocolor = 1
gathering = smart
retry_files_enabled = True
retry_files_save_path = ~/.ansible/retry-files
deprecation_warnings = False
display_skipped_hosts = True

[paramiko_connection]
pty = False
```

##### OS / ENVIRONMENT
Amazon Linux.
```
# uname -s -r -v -p -i -o
Linux 4.4.41-35.53.amzn1.x86_64 #1 SMP Mon Jan 9 23:00:57 UTC 2017 x86_64 x86_64 GNU/Linux
```
Instance is based on "Amazon Linux AMI 2016.09.1 (HVM), SSD Volume Type - ami-1e299d7e".
```
# python --version
Python 2.7.12
# pip show pexpect
Name: pexpect
Version: 4.2.1
```

##### SUMMARY
The first call of Ansible module "expect" failed with:
```
Insufficient version of pexpect installed (4.2.1), this module requires pexpect>=3.3. Error was 'int' object has no attribute 'rstrip'"
```
But next run was ok for first expect call, but fails all the time for the second call in the same playbook with error:
```"module_stderr": "Shared connection to 10.152.51.186 closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_CzL8Qh/ansible_module_expect.py\", line 237, in <module>\r\n    main()\r\n  File \"/tmp/ansible_CzL8Qh/ansible_module_expect.py\", line 149, in main\r\n    response = u'%s\\n' % value.rstrip('\\n').decode()\r\nAttributeError: 'int' object has no attribute 'rstrip'\r\n", "msg": "MODULE FAILURE"```

##### STEPS TO REPRODUCE
This is example of playbook task where it fails:
```yaml

- name: Finalize configuration of GoldenGate Monitor Server
  become: yes
  become_user: "{{ oggmon_os_user }}"
  expect:
    command: /bin/bash -c "{{ oggmon_home }}/monitor_server/bin/updateOGGMonitorConfig.sh"
    responses:
      "Enter absolute path for oggmon domain": "{{ oggmon_domain_home }}"
      "Enter JMX HOST NAME": "{{ oggmon_server_host }}"
      "Enter JMX Server Port": "{{ oggmon_server_jmx_port }}"
      "Enter JMX Server Username": "{{ oggmon_server_jmx_user }}"
      "For Oracle Database, Press 1": "1"
    timeout: 60
    echo: yes
    creates: "{{ oggmon_domain_home }}/config/monitorserver/cfg/oggmon.properties"
  tags:
    - deploy
```

##### EXPECTED RESULTS
Just pass it

##### ACTUAL RESULTS
Output with -vvv option:
```
TASK [goldengate-monitor : Finalize configuration of GoldenGate Monitor Server] 
task path: /home/*****/git/ami-provisioning/roles/goldengate-monitor/tasks/main.yml:259
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/extras/commands/expect.py
<X.X.X.X> ESTABLISH SSH CONNECTION FOR USER: ec2-user
<X.X.X.X> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=150s -o 'IdentityFile=~/.ssh/key.pem' -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/home/******/.ansible/cp/ansible-ssh-%h-%p-%r X.X.X.X '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /tmp/ansible-tmp-1485359246.77-165679692062492 `" && echo ansible-tmp-1485359246.77-165679692062492="` echo /tmp/ansible-tmp-1485359246.77-165679692062492 `" ) && sleep 0'"'"''
<X.X.X.X> PUT /tmp/tmp8WnA3S TO /tmp/ansible-tmp-1485359246.77-165679692062492/expect.py
<X.X.X.X> SSH: EXEC sftp -b - -o ControlMaster=auto -o ControlPersist=150s -o 'IdentityFile=~/.ssh/key.pem' -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/home/*******/.ansible/cp/ansible-ssh-%h-%p-%r '[X.X.X.X]'
<X.X.X.X> ESTABLISH SSH CONNECTION FOR USER: ec2-user
<X.X.X.X> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=150s -o 'IdentityFile=~/.ssh/key.pem' -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/home/********/.ansible/cp/ansible-ssh-%h-%p-%r X.X.X.X '/bin/sh -c '"'"'setfacl -m u:oracle:rx /tmp/ansible-tmp-1485359246.77-165679692062492/ /tmp/ansible-tmp-1485359246.77-165679692062492/expect.py && sleep 0'"'"''
<X.X.X.X> ESTABLISH SSH CONNECTION FOR USER: ec2-user
<X.X.X.X> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=150s -o 'IdentityFile=~/.ssh/key.pem' -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/home/*********/.ansible/cp/ansible-ssh-%h-%p-%r -tt X.X.X.X '/bin/sh -c '"'"'sudo -H -S -n -u oracle /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-cnroohudwdkqeukfzcxchpyrqnkqquzd; /usr/bin/python /tmp/ansible-tmp-1485359246.77-165679692062492/expect.py'"'"'"'"'"'"'"'"' && sleep 0'"'"''
<X.X.X.X> ESTABLISH SSH CONNECTION FOR USER: ec2-user
<X.X.X.X> SSH: EXEC ssh -o ControlMaster=auto -o ControlPersist=150s -o 'IdentityFile=~/.ssh/key.pem' -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ec2-user -o ConnectTimeout=10 -o ControlPath=/home/*******/.ansible/cp/ansible-ssh-%h-%p-%r X.X.X.X '/bin/sh -c '"'"'rm -f -r /tmp/ansible-tmp-1485359246.77-165679692062492/ > /dev/null 2>&1 && sleep 0'"'"''
fatal: [X.X.X.X]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "expect"
    }, 
    "module_stderr": "Shared connection to X.X.X.X closed.\r\n", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_vOOWV3/ansible_module_expect.py\", line 237, in <module>\r\n    main()\r\n  File \"/tmp/ansible_vOOWV3/ansible_module_expect.py\", line 149, in main\r\n    response = u'%s\\n' % value.rstrip('\\n').decode()\r\nAttributeError: 'int' object has no attribute 'rstrip'\r\n", 
    "msg": "MODULE FAILURE"
}
```



Discussion:
cc @sivel
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/commands/expect.py b/lib/ansible/modules/commands/expect.py
--- a/lib/ansible/modules/commands/expect.py
+++ b/lib/ansible/modules/commands/expect.py
@@ -107,7 +107,7 @@
 
 
 def response_closure(module, question, responses):
-    resp_gen = (u'%s\n' % r.rstrip('\n').decode() for r in responses)
+    resp_gen = (u'%s\n' % str(r).rstrip('\n').decode() for r in responses)
 
     def wrapped(info):
         try:
@@ -150,7 +150,7 @@ def main():
         if isinstance(value, list):
             response = response_closure(module, key, value)
         else:
-            response = u'%s\n' % value.rstrip('\n').decode()
+            response = u'%s\n' % str(value).rstrip('\n').decode()
 
         events[key.decode()] = response
 


Test patch: