From a8e4106c4748995ff4e6b91e84d7692b654b721f Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Fri, 28 Jun 2024 16:50:45 +0200 Subject: [PATCH] Fix tb for when env var contains % (#83499) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #83498 Co-authored-by: Sviatoslav Sydorenko (Святослав Сидоренко) --- changelogs/fragments/83498-command-tb-env.yml | 2 ++ lib/ansible/plugins/shell/__init__.py | 6 +++++- test/integration/targets/shell/tasks/command-building.yml | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/83498-command-tb-env.yml diff --git a/changelogs/fragments/83498-command-tb-env.yml b/changelogs/fragments/83498-command-tb-env.yml new file mode 100644 index 00000000000..b28ad18114a --- /dev/null +++ b/changelogs/fragments/83498-command-tb-env.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix a traceback when an environment variable contains certain special characters (https://github.com/ansible/ansible/issues/83498) diff --git a/lib/ansible/plugins/shell/__init__.py b/lib/ansible/plugins/shell/__init__.py index dd9f6553eca..f96d9dbdffd 100644 --- a/lib/ansible/plugins/shell/__init__.py +++ b/lib/ansible/plugins/shell/__init__.py @@ -211,7 +211,11 @@ class ShellBase(AnsiblePlugin): arg_path, ] - return f'{env_string}%s' % self.join(cps for cp in cmd_parts if cp and (cps := cp.strip())) + cleaned_up_cmd = self.join( + stripped_cmd_part for raw_cmd_part in cmd_parts + if raw_cmd_part and (stripped_cmd_part := raw_cmd_part.strip()) + ) + return ''.join((env_string, cleaned_up_cmd)) def append_command(self, cmd, cmd_to_append): """Append an additional command if supported by the shell""" diff --git a/test/integration/targets/shell/tasks/command-building.yml b/test/integration/targets/shell/tasks/command-building.yml index bd452618b52..d22f67467c9 100644 --- a/test/integration/targets/shell/tasks/command-building.yml +++ b/test/integration/targets/shell/tasks/command-building.yml @@ -28,6 +28,7 @@ ANSIBLE_REMOTE_TMP: '{{ atd }}' ANSIBLE_NOCOLOR: "1" ANSIBLE_FORCE_COLOR: "0" + TEST: "foo%D" register: command_building delegate_to: localhost