@ -23,7 +23,7 @@ import random
import re
import time
import ansible . constants as C
from ansible import constants as C
from ansible . errors import AnsibleError
from ansible . module_utils . six import text_type
from ansible . module_utils . six . moves import shlex_quote
@ -82,6 +82,10 @@ class ShellBase(AnsiblePlugin):
except KeyError :
pass
@staticmethod
def _generate_temp_dir_name ( ) :
return ' ansible-tmp- %s - %s - %s ' % ( time . time ( ) , os . getpid ( ) , random . randint ( 0 , 2 * * 48 ) )
def env_prefix ( self , * * kwargs ) :
return ' ' . join ( [ ' %s = %s ' % ( k , shlex_quote ( text_type ( v ) ) ) for k , v in kwargs . items ( ) ] )
@ -131,7 +135,7 @@ class ShellBase(AnsiblePlugin):
def mkdtemp ( self , basefile = None , system = False , mode = 0o700 , tmpdir = None ) :
if not basefile :
basefile = ' ansible-tmp- %s - %s ' % ( time . time ( ) , random . randint ( 0 , 2 * * 48 ) )
basefile = self . __class__ . _generate_temp_dir_name ( )
# When system is specified we have to create this in a directory where
# other users can read and access the tmp directory.
@ -143,7 +147,8 @@ class ShellBase(AnsiblePlugin):
# passed in tmpdir if it is valid or the first one from the setting if not.
if system :
tmpdir = tmpdir . rstrip ( ' / ' )
if tmpdir :
tmpdir = tmpdir . rstrip ( ' / ' )
if tmpdir in self . get_option ( ' system_tmpdirs ' ) :
basetmpdir = tmpdir
@ -157,7 +162,9 @@ class ShellBase(AnsiblePlugin):
basetmp = self . join_path ( basetmpdir , basefile )
cmd = ' mkdir -p %s echo %s %s ' % ( self . _SHELL_SUB_LEFT , basetmp , self . _SHELL_SUB_RIGHT )
# use mkdir -p to ensure parents exist, but mkdir fullpath to ensure last one is created by us
cmd = ' mkdir -p %s echo %s %s ' % ( self . _SHELL_SUB_LEFT , basetmpdir , self . _SHELL_SUB_RIGHT )
cmd + = ' %s mkdir %s ' % ( self . _SHELL_AND , basetmp )
cmd + = ' %s echo %s = %s echo %s %s ' % ( self . _SHELL_AND , basefile , self . _SHELL_SUB_LEFT , basetmp , self . _SHELL_SUB_RIGHT )
# change the umask in a subshell to achieve the desired mode