@ -1852,6 +1852,14 @@ class AnsibleModule(object):
'''
Execute a command , returns rc , stdout , and stderr .
The mechanism of this method for reading stdout and stderr differs from
that of CPython subprocess . Popen . communicate , in that this method will
stop reading once the spawned command has exited and stdout and stderr
have been consumed , as opposed to waiting until stdout / stderr are
closed . This can be an important distinction , when taken into account
that a forked or backgrounded process may hold stdout or stderr open
for longer than the spawned command .
: arg args : is the command to run
* If args is a list , the command will be run with shell = False .
* If args is a string and use_unsafe_shell = False it will split args to a list and run with shell = False
@ -2031,17 +2039,17 @@ class AnsibleModule(object):
if before_communicate_callback :
before_communicate_callback ( cmd )
# the communication logic here is essentially taken from that
# of the _communicate() function in ssh.py
stdout = b ' '
stderr = b ' '
try :
selector = selectors . DefaultSelector ( )
except ( IOError , OSError ) :
# Failed to detect default selector for the given platform
# Select PollSelector which is supported by major platforms
# Mirror the CPython subprocess logic and preference for the selector to use.
# poll/select have the advantage of not requiring any extra file
# descriptor, contrarily to epoll/kqueue (also, they require a single
# syscall).
if hasattr ( selectors , ' PollSelector ' ) :
selector = selectors . PollSelector ( )
else :
selector = selectors . SelectSelector ( )
if data :
if not binary_data :
@ -2049,53 +2057,58 @@ class AnsibleModule(object):
if isinstance ( data , text_type ) :
data = to_bytes ( data )
if not prompt_re :
stdout , stderr = cmd . communicate ( input = data )
else :
# We only need this to look for a prompt, to abort instead of hanging
selector . register ( cmd . stdout , selectors . EVENT_READ )
selector . register ( cmd . stderr , selectors . EVENT_READ )
if os . name == ' posix ' :
fcntl . fcntl ( cmd . stdout . fileno ( ) , fcntl . F_SETFL , fcntl . fcntl ( cmd . stdout . fileno ( ) , fcntl . F_GETFL ) | os . O_NONBLOCK )
fcntl . fcntl ( cmd . stderr . fileno ( ) , fcntl . F_SETFL , fcntl . fcntl ( cmd . stderr . fileno ( ) , fcntl . F_GETFL ) | os . O_NONBLOCK )
if data :
cmd . stdin . write ( data )
cmd . stdin . close ( )
while True :
events = selector . select ( 1 )
for key , event in events :
b_chunk = key . fileobj . read ( )
if b_chunk == b ( ' ' ) :
selector . unregister ( key . fileobj )
if key . fileobj == cmd . stdout :
stdout + = b_chunk
elif key . fileobj == cmd . stderr :
stderr + = b_chunk
# if we're checking for prompts, do it now
if prompt_re :
if prompt_re . search ( stdout ) and not data :
if encoding :
stdout = to_native ( stdout , encoding = encoding , errors = errors )
return ( 257 , stdout , " A prompt was encountered while running a command, but no input data was specified " )
# only break out if no pipes are left to read or
# the pipes are completely read and
# the process is terminated
if ( not events or not selector . get_map ( ) ) and cmd . poll ( ) is not None :
break
# No pipes are left to read but process is not yet terminated
# Only then it is safe to wait for the process to be finished
# NOTE: Actually cmd.poll() is always None here if no selectors are left
elif not selector . get_map ( ) and cmd . poll ( ) is None :
cmd . wait ( )
# The process is terminated. Since no pipes to read from are
# left, there is no need to call select() again.
break
cmd . stdout . close ( )
cmd . stderr . close ( )
selector . close ( )
selector . register ( cmd . stdout , selectors . EVENT_READ )
selector . register ( cmd . stderr , selectors . EVENT_READ )
if os . name == ' posix ' :
fcntl . fcntl ( cmd . stdout . fileno ( ) , fcntl . F_SETFL , fcntl . fcntl ( cmd . stdout . fileno ( ) , fcntl . F_GETFL ) | os . O_NONBLOCK )
fcntl . fcntl ( cmd . stderr . fileno ( ) , fcntl . F_SETFL , fcntl . fcntl ( cmd . stderr . fileno ( ) , fcntl . F_GETFL ) | os . O_NONBLOCK )
if data :
cmd . stdin . write ( data )
cmd . stdin . close ( )
while True :
# A timeout of 1 is both a little short and a little long.
# With None we could deadlock, with a lower value we would
# waste cycles. As it is, this is a mild inconvenience if
# we need to exit, and likely doesn't waste too many cycles
events = selector . select ( 1 )
stdout_changed = False
for key , event in events :
b_chunk = key . fileobj . read ( 32768 )
if not b_chunk :
selector . unregister ( key . fileobj )
elif key . fileobj == cmd . stdout :
stdout + = b_chunk
stdout_changed = True
elif key . fileobj == cmd . stderr :
stderr + = b_chunk
# if we're checking for prompts, do it now, but only if stdout
# actually changed since the last loop
if prompt_re and stdout_changed and prompt_re . search ( stdout ) and not data :
if encoding :
stdout = to_native ( stdout , encoding = encoding , errors = errors )
return ( 257 , stdout , " A prompt was encountered while running a command, but no input data was specified " )
# break out if no pipes are left to read or the pipes are completely read
# and the process is terminated
if ( not events or not selector . get_map ( ) ) and cmd . poll ( ) is not None :
break
# No pipes are left to read but process is not yet terminated
# Only then it is safe to wait for the process to be finished
# NOTE: Actually cmd.poll() is always None here if no selectors are left
elif not selector . get_map ( ) and cmd . poll ( ) is None :
cmd . wait ( )
# The process is terminated. Since no pipes to read from are
# left, there is no need to call select() again.
break
cmd . stdout . close ( )
cmd . stderr . close ( )
selector . close ( )
rc = cmd . returncode
except ( OSError , IOError ) as e :