mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 10:51:24 -07:00
Fix remote_tmp when become with non admin user (#42396)
* Fix tmpdir on non root become - also avoid exception if tmpdir and remote_tmp are None - give 'None' on deescalation so tempfile will fallback to it's default behaviour and use system dirs - fix issue with bad tempdir (not existing/not createable/not writeable) i.e nobody and ~/.ansible/tmp - added tests for blockfile case * Revert "Temporarily revertc119d54
" This reverts commit5c614a59a6
. * changes based on PR feedback and changelog fragment * changes based on the review * Fix tmpdir when makedirs failed so we just use the system tmp * Let missing remote_tmp fail If remote_tmp is missing then there's something more basic wrong in the communication from the controller to the module-side. It's better to be alerted in this case than to silently ignore it. jborean and I have independently checked what happens if the user sets ansible_remote_tmp to empty string and !!null and both cases work fine. (null is turned into a default value controller-side. empty string triggers the warning because it is probably not a directory that the become user is able to use).
This commit is contained in:
parent
6339e37abd
commit
8bdd04c147
16 changed files with 148 additions and 51 deletions
|
@ -930,20 +930,35 @@ class AnsibleModule(object):
|
|||
|
||||
@property
|
||||
def tmpdir(self):
|
||||
# if _ansible_tmpdir was not set, the module needs to create it and
|
||||
# clean it up once finished.
|
||||
# if _ansible_tmpdir was not set and we have a remote_tmp,
|
||||
# the module needs to create it and clean it up once finished.
|
||||
# otherwise we create our own module tmp dir from the system defaults
|
||||
if self._tmpdir is None:
|
||||
basedir = None
|
||||
|
||||
basedir = os.path.expanduser(os.path.expandvars(self._remote_tmp))
|
||||
if not os.path.exists(basedir):
|
||||
self.warn("Module remote_tmp %s did not exist and was created "
|
||||
"with a mode of 0700, this may cause issues when "
|
||||
"running as another user. To avoid this, create the "
|
||||
"remote_tmp dir with the correct permissions "
|
||||
"manually" % basedir)
|
||||
os.makedirs(basedir, mode=0o700)
|
||||
try:
|
||||
os.makedirs(basedir, mode=0o700)
|
||||
except (OSError, IOError) as e:
|
||||
self.warn("Unable to use %s as temporary directory, "
|
||||
"failing back to system: %s" % (basedir, to_native(e)))
|
||||
basedir = None
|
||||
else:
|
||||
self.warn("Module remote_tmp %s did not exist and was "
|
||||
"created with a mode of 0700, this may cause"
|
||||
" issues when running as another user. To "
|
||||
"avoid this, create the remote_tmp dir with "
|
||||
"the correct permissions manually" % basedir)
|
||||
|
||||
basefile = "ansible-moduletmp-%s-" % time.time()
|
||||
tmpdir = tempfile.mkdtemp(prefix=basefile, dir=basedir)
|
||||
try:
|
||||
tmpdir = tempfile.mkdtemp(prefix=basefile, dir=basedir)
|
||||
except (OSError, IOError) as e:
|
||||
self.fail_json(
|
||||
msg="Failed to create remote module tmp path at dir %s "
|
||||
"with prefix %s: %s" % (basedir, basefile, to_native(e))
|
||||
)
|
||||
if not self._keep_remote_files:
|
||||
atexit.register(shutil.rmtree, tmpdir)
|
||||
self._tmpdir = tmpdir
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue