From 5be74b8b51ea4f3c325f3a1fe972481714013da6 Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Thu, 26 Jun 2025 15:05:26 -0600 Subject: [PATCH 1/6] Fixed use of another module and now it automatically creates the tmp dir --- plugins/action/zos_copy.py | 56 +++++++++----------------------- plugins/action/zos_job_submit.py | 25 +++++++++++--- 2 files changed, 36 insertions(+), 45 deletions(-) diff --git a/plugins/action/zos_copy.py b/plugins/action/zos_copy.py index b513e179b7..7cf72edc43 100644 --- a/plugins/action/zos_copy.py +++ b/plugins/action/zos_copy.py @@ -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") @@ -332,16 +313,25 @@ 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"} + tempfile_args = {"path": temp_path, "state": "directory", "mode": "777"} # 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)) + print(f"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") + print(f"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 @@ -382,13 +372,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) @@ -427,14 +411,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: diff --git a/plugins/action/zos_job_submit.py b/plugins/action/zos_job_submit.py index 44601ac9ca..867b911831 100644 --- a/plugins/action/zos_job_submit.py +++ b/plugins/action/zos_job_submit.py @@ -82,10 +82,25 @@ def run(self, tmp=None, task_vars=None): 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") + # 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)) + print(f"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") + print(f"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 @@ -134,7 +149,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, From 1ba3ba776f2e6d2835b09d734dd7c00d7a374e5e Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Tue, 1 Jul 2025 09:50:26 -0600 Subject: [PATCH 2/6] Updated zos_copy --- plugins/action/zos_copy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/action/zos_copy.py b/plugins/action/zos_copy.py index 7cf72edc43..cb011222d1 100644 --- a/plugins/action/zos_copy.py +++ b/plugins/action/zos_copy.py @@ -317,13 +317,13 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None # Reverted this back to using file ansible module so ansible would handle all temporary dirs # creation with correct permissions. rc, stdout, stderr = self._connection.exec_command("mkdir -p {0}".format(temp_path)) - print(f"mkdir result {rc}, {stdout}, {stderr} path {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") - print(f"pwd result {rc}, {stdout}, {stderr} path {temp_path}") + 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) From de31c3f83c0c82b81db86522e65cd252b3a923f3 Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Tue, 1 Jul 2025 09:56:18 -0600 Subject: [PATCH 3/6] Updated display method --- plugins/action/zos_job_submit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/action/zos_job_submit.py b/plugins/action/zos_job_submit.py index 867b911831..2dbbf1e993 100644 --- a/plugins/action/zos_job_submit.py +++ b/plugins/action/zos_job_submit.py @@ -87,7 +87,7 @@ def run(self, tmp=None, task_vars=None): # ) # dest_path = tempfile.get("path") rc, stdout, stderr = self._connection.exec_command("mkdir -p {0}".format(dest_path)) - print(f"mkdir result {rc}, {stdout}, {stderr} dest path {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." @@ -95,7 +95,7 @@ def run(self, tmp=None, task_vars=None): # The temporary dir was created successfully using ssh connection user. rc, stdout, stderr = self._connection.exec_command(F"cd {dest_path} && pwd") - print(f"pwd result {rc}, {stdout}, {stderr} path {dest_path}") + 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." From e97ad25268e09a2af49973f74cf203c2cdc6b95e Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Tue, 1 Jul 2025 10:42:20 -0600 Subject: [PATCH 4/6] Updated plugins --- plugins/action/zos_copy.py | 5 ++--- plugins/action/zos_job_submit.py | 8 +------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/plugins/action/zos_copy.py b/plugins/action/zos_copy.py index cb011222d1..a34540d9a4 100644 --- a/plugins/action/zos_copy.py +++ b/plugins/action/zos_copy.py @@ -313,14 +313,13 @@ 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", "mode": "777"} - # Reverted this back to using file ansible module so ansible would handle all temporary dirs - # creation with correct permissions. + 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}") diff --git a/plugins/action/zos_job_submit.py b/plugins/action/zos_job_submit.py index 2dbbf1e993..7bb357aa52 100644 --- a/plugins/action/zos_job_submit.py +++ b/plugins/action/zos_job_submit.py @@ -79,13 +79,7 @@ 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: From 4022d2f50dcde7bbcd0b2d386f94b8518f90aa77 Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Wed, 20 Aug 2025 12:07:17 -0600 Subject: [PATCH 5/6] Updated copy to remote --- plugins/action/zos_copy.py | 21 +++++++-------------- plugins/action/zos_job_submit.py | 24 ++++++++---------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/plugins/action/zos_copy.py b/plugins/action/zos_copy.py index a34540d9a4..dab7e81fe9 100644 --- a/plugins/action/zos_copy.py +++ b/plugins/action/zos_copy.py @@ -314,19 +314,13 @@ def _copy_to_remote(self, src, is_dir=False, ignore_stderr=False, task_vars=None self.tmp_dir = self._connection._shell._options.get("remote_tmp") temp_path = os.path.join(self.tmp_dir, _create_temp_path_name()) - 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", "") + 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, + ) + temp_path = tempfile.get("path") _sftp_action = 'put' @@ -371,7 +365,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) - # 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) diff --git a/plugins/action/zos_job_submit.py b/plugins/action/zos_job_submit.py index 7bb357aa52..ec27fd2a7a 100644 --- a/plugins/action/zos_job_submit.py +++ b/plugins/action/zos_job_submit.py @@ -80,21 +80,13 @@ def run(self, tmp=None, task_vars=None): temp_file_dir = f'zos_job_submit_{datetime.now().strftime("%Y%m%d%S%f")}' dest_path = path.join(tmp_dir, temp_file_dir) - 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", "") + 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") dest_file = path.join(dest_path, path.basename(source)) source_full = None @@ -143,7 +135,7 @@ def run(self, tmp=None, task_vars=None): dict( src=source_full, dest=dest_file, - mode="644", + mode="0777", force=True, encoding=module_args.get('encoding'), remote_src=False, From d3ef1dd56cde371a3e07f8c97563fb5e91b86338 Mon Sep 17 00:00:00 2001 From: Fernando Flores Date: Wed, 20 Aug 2025 12:10:22 -0600 Subject: [PATCH 6/6] Updated blank lines --- plugins/action/zos_copy.py | 1 - plugins/action/zos_job_submit.py | 1 - 2 files changed, 2 deletions(-) diff --git a/plugins/action/zos_copy.py b/plugins/action/zos_copy.py index dab7e81fe9..d3fc82ae86 100644 --- a/plugins/action/zos_copy.py +++ b/plugins/action/zos_copy.py @@ -313,7 +313,6 @@ 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. diff --git a/plugins/action/zos_job_submit.py b/plugins/action/zos_job_submit.py index ec27fd2a7a..44601ac9ca 100644 --- a/plugins/action/zos_job_submit.py +++ b/plugins/action/zos_job_submit.py @@ -79,7 +79,6 @@ 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.