@@ -155,7 +155,7 @@ def run(self, tmp=None, task_vars=None):
155
155
try :
156
156
local_content = _write_content_to_temp_file (content )
157
157
transfer_res = self ._copy_to_remote (
158
- local_content , ignore_stderr = ignore_sftp_stderr
158
+ local_content , ignore_stderr = ignore_sftp_stderr , task_vars = task_vars
159
159
)
160
160
finally :
161
161
os .remove (local_content )
@@ -243,7 +243,7 @@ def run(self, tmp=None, task_vars=None):
243
243
244
244
display .vvv (u"ibm_zos_copy calculated size: {0}" .format (os .stat (src ).st_size ), host = self ._play_context .remote_addr )
245
245
transfer_res = self ._copy_to_remote (
246
- src , is_dir = is_src_dir , ignore_stderr = ignore_sftp_stderr
246
+ src , is_dir = is_src_dir , ignore_stderr = ignore_sftp_stderr , task_vars = task_vars
247
247
)
248
248
249
249
temp_path = transfer_res .get ("temp_path" )
@@ -291,7 +291,26 @@ def run(self, tmp=None, task_vars=None):
291
291
# Remove temporary directory from remote
292
292
if self .tmp_dir is not None :
293
293
path = os .path .normpath (f"{ self .tmp_dir } /ansible-zos-copy" )
294
- self ._connection .exec_command (f"rm -rf { path } *" )
294
+ # If another user created the temporary files, we'll need to run rm
295
+ # with it too, lest we get a permissions issue.
296
+ if self ._connection .become :
297
+ # We get the dirname from temp_path and not path = os.path.normpath(f"{self.tmp_dir}/ansible-zos-copy")
298
+ # because if default is ~/.ansible/tmp/ when using become it would be similar to /root/.ansible/tmp
299
+ # but the original tmp directory was resolved when user is non escalated yet. Meaning, the original
300
+ # tmp directory is similar to /u/usrt001/.ansible/tmp.
301
+ path = os .path .dirname (temp_path )
302
+ self ._connection .set_option ('remote_user' , self ._play_context ._become_user )
303
+ display .vvv (
304
+ u"ibm_zos_copy SSH cleanup user updated to {0}" .format (self ._play_context ._become_user ),
305
+ host = self ._play_context .remote_addr
306
+ )
307
+ rm_res = self ._connection .exec_command (f"rm -rf { path } *" )
308
+ if self ._connection .become :
309
+ self ._connection .set_option ('remote_user' , self ._play_context ._remote_user )
310
+ display .vvv (
311
+ u"ibm_zos_copy SSH cleanup user restored to {0}" .format (self ._play_context ._remote_user ),
312
+ host = self ._play_context .remote_addr
313
+ )
295
314
296
315
if copy_res .get ("note" ) and not force :
297
316
result ["note" ] = copy_res .get ("note" )
@@ -319,18 +338,21 @@ def run(self, tmp=None, task_vars=None):
319
338
320
339
return copy_res
321
340
322
- def _copy_to_remote (self , src , is_dir = False , ignore_stderr = False ):
341
+ def _copy_to_remote (self , src , is_dir = False , ignore_stderr = False , task_vars = None ):
323
342
"""Copy a file or directory to the remote z/OS system """
324
343
self .tmp_dir = self ._connection ._shell ._options .get ("remote_tmp" )
325
- rc , stdout , stderr = self ._connection .exec_command ("cd {0} && pwd" .format (self .tmp_dir ))
326
- if rc > 0 :
327
- msg = f"Failed to resolve remote temporary directory { self .tmp_dir } . Ensure that the directory exists and user has proper access."
328
- return self ._exit_action ({}, msg , failed = True )
329
- self .tmp_dir = stdout .decode ("utf-8" ).replace ("\r " , "" ).replace ("\n " , "" )
330
- temp_path = os .path .join (self .tmp_dir , _create_temp_path_name (), os .path .basename (src ))
331
- self ._connection .exec_command ("mkdir -p {0}" .format (os .path .dirname (temp_path )))
332
- _src = src .replace ("#" , "\\ #" )
344
+ temp_path = os .path .join (self .tmp_dir , _create_temp_path_name ())
345
+ tempfile_args = {"path" : temp_path , "state" : "directory" , "mode" : "666" }
346
+ # Reverted this back to using file ansible module so ansible would handle all temporary dirs
347
+ # creation with correct permissions.
348
+ tempfile = self ._execute_module (
349
+ module_name = "file" , module_args = tempfile_args , task_vars = task_vars , wrap_async = self ._task .async_val
350
+ )
333
351
_sftp_action = 'put'
352
+ was_user_updated = False
353
+
354
+ temp_path = os .path .join (tempfile .get ("path" ), os .path .basename (src ))
355
+ _src = src .replace ("#" , "\\ #" )
334
356
full_temp_path = temp_path
335
357
336
358
if is_dir :
@@ -370,6 +392,13 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False):
370
392
sftp_transfer_method ), host = self ._play_context .remote_addr )
371
393
372
394
display .vvv (u"ibm_zos_copy: {0} {1} TO {2}" .format (_sftp_action , _src , temp_path ), host = self ._play_context .remote_addr )
395
+ if self ._connection .become :
396
+ was_user_updated = True
397
+ self ._connection .set_option ('remote_user' , self ._play_context ._become_user )
398
+ display .vvv (
399
+ u"ibm_zos_copy SSH transfer user updated to {0}" .format (self ._play_context ._become_user ),
400
+ host = self ._play_context .remote_addr
401
+ )
373
402
(returncode , stdout , stderr ) = self ._connection ._file_transport_command (_src , temp_path , _sftp_action )
374
403
375
404
display .vvv (u"ibm_zos_copy return code: {0}" .format (returncode ), host = self ._play_context .remote_addr )
@@ -400,7 +429,7 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False):
400
429
401
430
if returncode != 0 or (err and not ignore_stderr ):
402
431
return dict (
403
- msg = "Error transfering source '{0}' to remote z/OS system" .format (src ),
432
+ msg = "Error transferring source '{0}' to remote z/OS system" .format (src ),
404
433
rc = returncode ,
405
434
stderr = err ,
406
435
stderr_lines = err .splitlines (),
@@ -409,6 +438,12 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False):
409
438
410
439
finally :
411
440
# Restore the users defined option `ssh_transfer_method` if it was overridden
441
+ if was_user_updated :
442
+ self ._connection .set_option ('remote_user' , self ._play_context ._remote_user )
443
+ display .vvv (
444
+ u"ibm_zos_copy SSH transfer user restored to {0}" .format (self ._play_context ._remote_user ),
445
+ host = self ._play_context .remote_addr
446
+ )
412
447
413
448
if is_ssh_transfer_method_updated :
414
449
if version_major == 2 and version_minor >= 11 :
0 commit comments