Skip to content

Bugfix/2190/become no ssh #2195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: staging-v1.13.2
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
59 changes: 17 additions & 42 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 @@ -332,16 +313,24 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None
"""Copy a file or directory to the remote z/OS system """
self.tmp_dir = self._connection._shell._options.get("remote_tmp")
temp_path = os.path.join(self.tmp_dir, _create_temp_path_name())
tempfile_args = {"path": temp_path, "state": "directory"}
# Reverted this back to using file ansible module so ansible would handle all temporary dirs
# creation with correct permissions.
tempfile = self._execute_module(
module_name="file", module_args=tempfile_args, task_vars=task_vars,
)

rc, stdout, stderr = self._connection.exec_command("mkdir -p {0}".format(temp_path))
display.vvv(f"ibm_zos_copy: remote mkdir result {rc}, {stdout}, {stderr} path {temp_path}")
if rc > 0:
msg = f"Failed to create remote temporary directory in {self.tmp_dir}. Ensure that user has proper access."
return self._exit_action({}, msg, failed=True)

# The temporary dir was created successfully using ssh connection user.
rc, stdout, stderr = self._connection.exec_command(F"cd {temp_path} && pwd")
display.vvv(f"ibm_zos_copy: remote pwd result {rc}, {stdout}, {stderr} path {temp_path}")
if rc > 0:
msg = f"Failed to resolve remote temporary directory {temp_path}. Ensure that user has proper access."
return self._exit_action({}, msg, failed=True)
temp_path = stdout.decode("utf-8").replace("\r", "").replace("\n", "")

_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 +371,7 @@ 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
)
# self._fixup_perms2((self._connection._shell.tmpdir, os.path.dirname(temp_path)))
(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 +410,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
25 changes: 17 additions & 8 deletions plugins/action/zos_job_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,22 @@ def run(self, tmp=None, task_vars=None):
tmp_dir = self._connection._shell._options.get("remote_tmp")
temp_file_dir = f'zos_job_submit_{datetime.now().strftime("%Y%m%d%S%f")}'
dest_path = path.join(tmp_dir, temp_file_dir)
tempfile_args = {"path": dest_path, "state": "directory"}
# Reverted this back to using file ansible module so ansible would handle all temporary dirs
# creation with correct permissions.
tempfile = self._execute_module(
module_name="file", module_args=tempfile_args, task_vars=task_vars,
)
dest_path = tempfile.get("path")

rc, stdout, stderr = self._connection.exec_command("mkdir -p {0}".format(dest_path))
display.vvv(f"zos_job_submit: mkdir result {rc}, {stdout}, {stderr} dest path {dest_path}")
if rc > 0:
result["failed"] = True
result["msg"] = f"Failed to create remote temporary directory in {tmp_dir}. Ensure that user has proper access."
return result

# The temporary dir was created successfully using ssh connection user.
rc, stdout, stderr = self._connection.exec_command(F"cd {dest_path} && pwd")
display.vvv(f"zos_job_submit: remote pwd result {rc}, {stdout}, {stderr} path {dest_path}")
if rc > 0:
result["failed"] = True
result["msg"] = f"Failed to resolve remote temporary directory {dest_path}. Ensure that user has proper access."
return result
dest_path = stdout.decode("utf-8").replace("\r", "").replace("\n", "")
dest_file = path.join(dest_path, path.basename(source))

source_full = None
Expand Down Expand Up @@ -134,7 +143,7 @@ def run(self, tmp=None, task_vars=None):
dict(
src=source_full,
dest=dest_file,
mode="0777",
mode="644",
force=True,
encoding=module_args.get('encoding'),
remote_src=False,
Expand Down