# Failed to execute remote python script with ansible

**URL:** <https://forum.ansible.com/t/failed-to-execute-remote-python-script-with-ansible/22469>\
**Category:** Ansible Project\
**Created:** [September 2, 2016, 9:21pm UTC](https://forum.ansible.com/t/failed-to-execute-remote-python-script-with-ansible/22469 "2016-09-02T21:21:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michelle\_Moreno\_Guti](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@Michelle\_Moreno\_Guti](https://forum.ansible.com/u/Michelle_Moreno_Guti)\
**Post date:** [September 2, 2016, 9:21pm UTC](https://forum.ansible.com/t/failed-to-execute-remote-python-script-with-ansible/22469/1 "2016-09-02T21:21:10Z")

</div>

Hello,

I’m trying to execute a remote python script on linux nodes.  
So, the first step I do is to copy the script file in the remote nodes with the “copy” module and this works well:

root@soporte03:# ansible sura -m copy -a “src=/root/passwd.py dest=/home/AdminSplunk/ owner=AdminSplunk group=AdminSplunk mode=0777”

The correct way to execute the script in a server without use ansible is this:

python passwd.py “current-password” “new-password”

My problem is when I try to execute this script in the ansible nodes, I ran this line with several modules ansible but continues to generate errors  
This are the lines that i used using ansible

--------------- With the script module ---------------

root@soporte03:/etc/ansible # ansible sura -m script -a “python passwd.py ‘Suramericana.2016\*’ ‘Su334N…103’”  
micha2 | FAILED! =\> {  
“failed”: true,  
“msg”: “file or module does not exist: /etc/ansible/python”  
}  
micha1 | FAILED! =\> {  
“failed”: true,  
“msg”: “file or module does not exist: /etc/ansible/python”  
}

--------------- With the shell module ---------------

root@soporte03:/etc/ansible # ansible sura -m shell -a “python passwd.py ‘Suramericana.2016\*’ ‘Su334N…103’”  
micha2 | FAILED | rc=1 \>\>  
Changing user password  
Cambiando la contraseña del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX: Traceback (most recent call last):  
File “passwd.py”, line 13, in   
current = p.expect(‘(current) UNIX password:’, timeout=3)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1311, in expect  
return self.expect\_list(compiled\_pattern\_list, timeout, searchwindowsize)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1325, in expect\_list  
return self.expect\_loop(searcher\_re(pattern\_list), timeout, searchwindowsize)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1409, in expect\_loop  
raise TIMEOUT (str(e) + ‘\n’ + str(self))  
pexpect.TIMEOUT: Timeout exceeded in read\_nonblocking().  
\<pexpect.spawn object at 0x7fce82a25690\>  
version: 2.3 ($Revision: 399 $)  
command: /usr/bin/passwd  
args: [‘/usr/bin/passwd’]  
searcher: searcher\_re:  
0: re.compile(“(current) UNIX password:”)  
buffer (last 100 chars): a del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX:  
before (last 100 chars): a del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX:  
after: \<class ‘pexpect.TIMEOUT’\>  
match: None  
match\_index: None  
exitstatus: None  
flag\_eof: False  
pid: 1838  
child\_fd: 3  
closed: False  
timeout: 30  
delimiter: \<class ‘pexpect.EOF’\>  
logfile: \<open file ‘’, mode ‘w’ at 0x7fce82b0c150\>  
logfile\_read: None  
logfile\_send: None  
maxread: 2000  
ignorecase: False  
searchwindowsize: None  
delaybeforesend: 0.05  
delayafterclose: 0.1  
delayafterterminate: 0.1

--------------- With the raw module ---------------

root@soporte03:/etc/ansible # ansible sura -m raw -a “python passwd.py ‘Suramericana.2016\*’ ‘Su334N…103’”  
micha1 | FAILED | rc=1 \>\>  
Changing user password  
Cambiando la contraseña del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX: Traceback (most recent call last):  
File “passwd.py”, line 13, in   
current = p.expect(‘(current) UNIX password:’, timeout=3)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1311, in expect  
return self.expect\_list(compiled\_pattern\_list, timeout, searchwindowsize)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1325, in expect\_list  
return self.expect\_loop(searcher\_re(pattern\_list), timeout, searchwindowsize)  
File “/usr/lib/python2.6/site-packages/pexpect.py”, line 1409, in expect\_loop  
raise TIMEOUT (str(e) + ‘\n’ + str(self))  
pexpect.TIMEOUT: Timeout exceeded in read\_nonblocking().  
\<pexpect.spawn object at 0x7fb6c7847610\>  
version: 2.3 ($Revision: 399 $)  
command: /usr/bin/passwd  
args: [‘/usr/bin/passwd’]  
searcher: searcher\_re:  
0: re.compile(“(current) UNIX password:”)  
buffer (last 100 chars): a del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX:  
before (last 100 chars): a del usuario AdminSplunk.  
Cambiando la contraseña de AdminSplunk.  
(actual) contraseña de UNIX:  
after: \<class ‘pexpect.TIMEOUT’\>  
match: None  
match\_index: None  
exitstatus: None  
flag\_eof: False  
pid: 1720  
child\_fd: 3  
closed: False  
timeout: 30  
delimiter: \<class ‘pexpect.EOF’\>  
logfile: \<open file ‘’, mode ‘w’ at 0x7fb6c792f150\>  
logfile\_read: None  
logfile\_send: None  
maxread: 2000  
ignorecase: False  
searchwindowsize: None  
delaybeforesend: 0.05  
delayafterclose: 0.1  
delayafterterminate: 0.1

please someone tell me what is the correct way to execute a remote python script with ansible.

Appreciate your help.

---

<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:** [September 2, 2016, 9:49pm UTC](https://forum.ansible.com/t/failed-to-execute-remote-python-script-with-ansible/22469/2 "2016-09-02T21:49:04Z")

</div>

The script module assumes that you are going to have ansible copy the script from local to remote and execute it all in one task.

The shell module is the way to go, but you may be experiencing an issue with the TTY. It looks like pexpect in that script is failing to read a response from the spawned command.

Also, it looks like maybe you are trying to change users passwords. You can do this with the `user` module. Also there is an `expect` module that utilizes pexpect under the hood to accomplish similar tasks.
