Failed to execute remote python script with ansible

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.

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.