From 36525c2755103d27c898aa882f56d8a55f5ce5c1 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 27 Jul 2023 10:26:09 -0400 Subject: [PATCH] moved to shlex.join as requested --- lib/ansible/plugins/become/su.py | 2 +- lib/ansible/plugins/become/sudo.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/ansible/plugins/become/su.py b/lib/ansible/plugins/become/su.py index 18a4f0b8467..d292ff8c8d7 100644 --- a/lib/ansible/plugins/become/su.py +++ b/lib/ansible/plugins/become/su.py @@ -165,4 +165,4 @@ class BecomeModule(BecomeBase): user = self.get_option('become_user') or '' success_cmd = self._build_success_command(cmd, shell) - return ' '.join([exe, shlex.quote(flags), shlex.quote(user), '-c', shlex.quote(success_cmd)]) + return shlex.join([exe, flags, user, '-c', success_cmd]) diff --git a/lib/ansible/plugins/become/sudo.py b/lib/ansible/plugins/become/sudo.py index a32ebb158b9..86f3883611a 100644 --- a/lib/ansible/plugins/become/sudo.py +++ b/lib/ansible/plugins/become/sudo.py @@ -95,12 +95,11 @@ class BecomeModule(BecomeBase): if not cmd: return cmd - becomecmd = self.get_option('become_exe') or self.name + command = [self.get_option('become_exe') or self.name] flags = self.get_option('become_flags') or '' - prompt = '' if self.get_option('become_pass'): - self.prompt = '[sudo via ansible, key=%s] password:' % self._id + if flags: # this could be simplified, but kept as is for now for backwards string matching reflag = [] for flag in shlex.split(flags): @@ -110,12 +109,19 @@ class BecomeModule(BecomeBase): # handle -XnxxX flags only flag = re.sub(r'^(-\w*)n(\w*.*)', r'\1\2', flag) reflag.append(flag) - flags = shlex.join(reflag) + if reflag: + command.extend(reflag) - prompt = '-p %s' % shlex.quote(self.prompt) + self.prompt = '[sudo via ansible, key=%s] password:' % self._id + command.extend(['-p', self.prompt]) + + elif flags: + command.append(flags) user = self.get_option('become_user') or '' if user: - user = '-u %s' % shlex.quote(user) + command.extend(['-u', user]) + + command.append(self._build_success_command(cmd, shell)) - return ' '.join([becomecmd, flags, prompt, user, shlex.quote(self._build_success_command(cmd, shell))]) + return shlex.join(command)