Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 3 additions & 36 deletions plugins/action/zos_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,7 @@ def run(self, tmp=None, task_vars=None):
# Remove temporary directory from remote
if self.tmp_dir is not None:
path = os.path.normpath(f"{self.tmp_dir}/ansible-zos-copy")
# If another user created the temporary files, we'll need to run rm
# with it too, lest we get a permissions issue.
if self._connection.become:
# We get the dirname from temp_path and not path = os.path.normpath(f"{self.tmp_dir}/ansible-zos-copy")
# because if default is ~/.ansible/tmp/ when using become it would be similar to /root/.ansible/tmp
# but the original tmp directory was resolved when user is non escalated yet. Meaning, the original
# tmp directory is similar to /u/usrt001/.ansible/tmp.
path = os.path.dirname(temp_path)
self._connection.set_option('remote_user', self._play_context._become_user)
display.vvv(
u"ibm_zos_copy SSH cleanup user updated to {0}".format(self._play_context._become_user),
host=self._play_context.remote_addr
)
rm_res = self._connection.exec_command(f"rm -rf {path}*")
if self._connection.become:
self._connection.set_option('remote_user', self._play_context._remote_user)
display.vvv(
u"ibm_zos_copy SSH cleanup user restored to {0}".format(self._play_context._remote_user),
host=self._play_context.remote_addr
)

if copy_res.get("note") and not force:
result["note"] = copy_res.get("note")
Expand Down Expand Up @@ -338,10 +319,11 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None
tempfile = self._execute_module(
module_name="file", module_args=tempfile_args, task_vars=task_vars,
)
temp_path = tempfile.get("path")

_sftp_action = 'put'
was_user_updated = False

temp_path = os.path.join(tempfile.get("path"), os.path.basename(src))
temp_path = os.path.join(temp_path, os.path.basename(src))
_src = src.replace("#", "\\#")
full_temp_path = temp_path

Expand Down Expand Up @@ -382,13 +364,6 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None
sftp_transfer_method), host=self._play_context.remote_addr)

display.vvv(u"ibm_zos_copy: {0} {1} TO {2}".format(_sftp_action, _src, temp_path), host=self._play_context.remote_addr)
if self._connection.become:
was_user_updated = True
self._connection.set_option('remote_user', self._play_context._become_user)
display.vvv(
u"ibm_zos_copy SSH transfer user updated to {0}".format(self._play_context._become_user),
host=self._play_context.remote_addr
)
(returncode, stdout, stderr) = self._connection._file_transport_command(_src, temp_path, _sftp_action)

display.vvv(u"ibm_zos_copy return code: {0}".format(returncode), host=self._play_context.remote_addr)
Expand Down Expand Up @@ -427,14 +402,6 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None
)

finally:

if was_user_updated:
self._connection.set_option('remote_user', self._play_context._remote_user)
display.vvv(
u"ibm_zos_copy SSH transfer user restored to {0}".format(self._play_context._remote_user),
host=self._play_context.remote_addr
)

# Restore the users defined option `ssh_transfer_method` if it was overridden

if is_ssh_transfer_method_updated:
Expand Down