# Buggy code
def response_closure(module, question, responses):
    resp_gen = ("%s\n" % r.rstrip("\n").decode() for r in responses)

    def wrapped(info):
        try:
            return resp_gen.next()
        except StopIteration:
            module.fail_json(
                msg="No remaining responses for '%s', "
                "output was '%s'" % (question, info["child_result_list"][-1])
            )

    return wrapped


# Traceback (nAttributeError)
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 &amp;&amp; mkdir -p "` echo /tmp/ansible-tmp-1485359246.77-165679692062492 `" &amp;&amp; echo ansible-tmp-1485359246.77-165679692062492="` echo /tmp/ansible-tmp-1485359246.77-165679692062492 `" ) &amp;&amp; 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 &amp;&amp; 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'"'"'"'"'"'"'"'"' &amp;&amp; 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>&amp;1 &amp;&amp; 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"
}

# Fixed code
def response_closure(module, question, responses):
    resp_gen = ("%s\n" % str(r).rstrip("\n").decode() for r in responses)

    def wrapped(info):
        try:
            return resp_gen.next()
        except StopIteration:
            module.fail_json(
                msg="No remaining responses for '%s', "
                "output was '%s'" % (question, info["child_result_list"][-1])
            )

    return wrapped