Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expect fails with 'str' object has no attribute 'decode' for python3 #24226

Closed
haidaraM opened this issue May 2, 2017 · 1 comment · Fixed by #24269
Assignees

Comments

@haidaraM
Copy link
Contributor

@haidaraM haidaraM commented May 2, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

expect module: modules/commands/expect.py

ANSIBLE VERSION
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 3.5.1 (default, Apr  2 2017, 11:46:56) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
CONFIGURATION
host_key_checking = False
OS / ENVIRONMENT

Running Python 3.5.3 on the targeted hosts (with pexpect==4.2.1) by modifiying the ansible_python_interpreter in the inventory file (ansible_python_interpreter=/usr/local/bin/python3)

SUMMARY

When running a play with the expect module and Python 3 installed on the targeted hosts, it fails with the following error:

fatal: [debian8]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Shared connection to 192.168.33.111 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_39h5lqrl/ansible_module_expect.py\", line 244, in <module>\r\n    main()\r\n  File \"/tmp/ansible_39h5lqrl/ansible_module_expect.py\", line 160, in main\r\n    events[key.decode()] = response\r\nAttributeError: 'str' object has no attribute 'decode'\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}

This issue is pretty similar to #21668. Both seems to be related to the fact python3 has not decode for strings. The offending line is here:

events[key.decode()] = response

STEPS TO REPRODUCE
---
- hosts: debian8
  tasks:
    - name: Test Script
      expect:
        command: test.sh
        responses:
          enter one: 'one'
          enter two: 'two'

test.sh:

#!/usr/bin/env bash
read -p "enter one: " one
echo $one
read -p "enter two: " two
echo $two
EXPECTED RESULTS

Module executed by providing the given responses.

ACTUAL RESULTS
Using module file /usr/local/lib/python3.5/site-packages/ansible/modules/commands/expect.py
<192.168.33.111> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.111> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/elmhaidara/dev/vms/debian8/.vagrant/machines/debian8/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/Users/elmhaidara/.ansible/cp/db944c059a 192.168.33.111 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<192.168.33.111> (0, b'/home/vagrant\n', b'')
<192.168.33.111> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.111> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/elmhaidara/dev/vms/debian8/.vagrant/machines/debian8/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/Users/elmhaidara/.ansible/cp/db944c059a 192.168.33.111 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339 `" && echo ansible-tmp-1493766691.040637-147158662762339="` echo /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339 `" ) && sleep 0'"'"''
<192.168.33.111> (0, b'ansible-tmp-1493766691.040637-147158662762339=/home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339\n', b'')
<192.168.33.111> PUT /var/folders/3g/c_w63l892b34bhhy6m0vgl2r0000gn/T/tmp16lswu1n TO /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/expect.py
<192.168.33.111> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/elmhaidara/dev/vms/debian8/.vagrant/machines/debian8/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/Users/elmhaidara/.ansible/cp/db944c059a '[192.168.33.111]'
<192.168.33.111> (0, b'sftp> put /var/folders/3g/c_w63l892b34bhhy6m0vgl2r0000gn/T/tmp16lswu1n /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/expect.py\n', b'')
<192.168.33.111> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.111> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/elmhaidara/dev/vms/debian8/.vagrant/machines/debian8/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/Users/elmhaidara/.ansible/cp/db944c059a 192.168.33.111 '/bin/sh -c '"'"'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/ /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/expect.py && sleep 0'"'"''
<192.168.33.111> (0, b'', b'')
<192.168.33.111> ESTABLISH SSH CONNECTION FOR USER: vagrant
<192.168.33.111> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o 'IdentityFile="/Users/elmhaidara/dev/vms/debian8/.vagrant/machines/debian8/virtualbox/private_key"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=vagrant -o ConnectTimeout=10 -o ControlPath=/Users/elmhaidara/.ansible/cp/db944c059a -tt 192.168.33.111 '/bin/sh -c '"'"'/usr/local/bin/python3 /home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/expect.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1493766691.040637-147158662762339/" > /dev/null 2>&1 && sleep 0'"'"''
<192.168.33.111> (0, b'Traceback (most recent call last):\r\n  File "/tmp/ansible_39h5lqrl/ansible_module_expect.py", line 244, in <module>\r\n    main()\r\n  File "/tmp/ansible_39h5lqrl/ansible_module_expect.py", line 160, in main\r\n    events[key.decode()] = response\r\nAttributeError: \'str\' object has no attribute \'decode\'\r\n', b'Shared connection to 192.168.33.111 closed.\r\n')
fatal: [debian8]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Shared connection to 192.168.33.111 closed.\r\n",
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_39h5lqrl/ansible_module_expect.py\", line 244, in <module>\r\n    main()\r\n  File \"/tmp/ansible_39h5lqrl/ansible_module_expect.py\", line 160, in main\r\n    events[key.decode()] = response\r\nAttributeError: 'str' object has no attribute 'decode'\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}
@ansibot

This comment has been minimized.

Copy link
Contributor

@ansibot ansibot commented May 2, 2017

@sivel sivel self-assigned this May 2, 2017
@sivel sivel removed the needs_triage label May 2, 2017
@sivel sivel closed this in #24269 May 3, 2017
@ansibot ansibot added bug and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants
You can’t perform that action at this time.