@ -677,14 +677,14 @@ class AnsibleModule(object):
self . set_context_if_different ( src , context , False )
self . set_context_if_different ( src , context , False )
os . rename ( src , dest )
os . rename ( src , dest )
def run_command ( self , args , * * kwargs ) :
def run_command ( self , args , check_rc = False , close_fds = False , executable = None ) :
'''
'''
Execute a command , returns rc , stdout , and stderr .
Execute a command , returns rc , stdout , and stderr .
args is the command to run
args is the command to run
If args is a list , the command will be run with shell = False .
If args is a list , the command will be run with shell = False .
Otherwise , the command will be run with shell = True when args is a string .
Otherwise , the command will be run with shell = True when args is a string .
kwargs is a dict of keyword arguments :
Other arguments :
- fail_on_rc_non_zero ( boolean ) Whether to call fail_json in case of
- check_rc ( boolean ) Whether to call fail_json in case of
non zero RC . Default is False .
non zero RC . Default is False .
- close_fds ( boolean ) See documentation for subprocess . Popen ( ) .
- close_fds ( boolean ) See documentation for subprocess . Popen ( ) .
Default is False .
Default is False .
@ -692,25 +692,19 @@ class AnsibleModule(object):
Default is None .
Default is None .
'''
'''
if isinstance ( args , list ) :
if isinstance ( args , list ) :
kwargs[ ' shell' ] = False
shell = False
elif isinstance ( args , basestring ) :
elif isinstance ( args , basestring ) :
kwargs[ ' shell' ] = True
shell = True
else :
else :
msg = " Argument ' args ' to run_command must be list or string "
msg = " Argument ' args ' to run_command must be list or string "
self . fail_json ( rc = 257 , cmd = args , msg = msg )
self . fail_json ( rc = 257 , cmd = args , msg = msg )
if ' fail_on_rc_non_zero ' not in kwargs :
kwargs [ ' fail_on_rc_non_zero ' ] = False
if ' close_fds ' not in kwargs :
kwargs [ ' close_fds ' ] = False
if ' executable ' not in kwargs :
kwargs [ ' executable ' ] = None
rc = 0
rc = 0
msg = None
msg = None
try :
try :
cmd = subprocess . Popen ( args ,
cmd = subprocess . Popen ( args ,
executable = kwargs[ ' executable' ] ,
executable = executable ,
shell = kwargs[ ' shell' ] ,
shell = shell ,
close_fds = kwargs[ ' close_fds' ] ,
close_fds = close_fds,
stdout = subprocess . PIPE , stderr = subprocess . PIPE )
stdout = subprocess . PIPE , stderr = subprocess . PIPE )
out , err = cmd . communicate ( )
out , err = cmd . communicate ( )
rc = cmd . returncode
rc = cmd . returncode
@ -718,7 +712,7 @@ class AnsibleModule(object):
self . fail_json ( rc = e . errno , msg = str ( e ) , cmd = args )
self . fail_json ( rc = e . errno , msg = str ( e ) , cmd = args )
except :
except :
self . fail_json ( rc = 257 , msg = traceback . format_exc ( ) , cmd = args )
self . fail_json ( rc = 257 , msg = traceback . format_exc ( ) , cmd = args )
if rc != 0 and kwargs[ ' fail_on _rc_non_zero' ] :
if rc != 0 and chec k_rc:
msg = err . rstrip ( )
msg = err . rstrip ( )
self . fail_json ( cmd = args , rc = rc , stdout = out , stderr = err , msg = msg )
self . fail_json ( cmd = args , rc = rc , stdout = out , stderr = err , msg = msg )
return ( rc , out , err )
return ( rc , out , err )