Handle ValueError in run_command when parsing invalid args(shlex.split) (#85945)

This commit is contained in:
Jason K Hall
2025-10-27 14:02:22 -07:00
committed by GitHub
parent d9d11d6ff6
commit 8d07ed6318
3 changed files with 26 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
---
bugfixes:
- basic - fail in controlled manner when ``run_command()`` attempts to parse a command with broken syntax passed in as a string (https://github.com/ansible/ansible/issues/85719).

View File

@@ -1969,7 +1969,10 @@ class AnsibleModule(object):
else:
# ensure args are a list
if isinstance(args, (bytes, str)):
args = shlex.split(to_text(args, errors='surrogateescape'))
try:
args = shlex.split(to_text(args, errors='surrogateescape'))
except ValueError as e:
self.fail_json(msg="Invalid command syntax in run_command", exception=e)
# expand ``~`` in paths, and all environment vars
if expand_user_and_vars:

View File

@@ -198,3 +198,22 @@
- result.diff.after is defined
- result.diff.before is defined
- "result.state == 'file'"
- name: Setup for validate test
ansible.builtin.file:
path: "{{ remote_tmp_dir }}/src_val_syntax"
state: directory
- name: assemble with invalid validation command
ansible.builtin.assemble:
src: "{{ remote_tmp_dir }}/src_val_syntax"
dest: "{{ remote_tmp_dir }}/dest_val_syntax"
validate: "echo 'missing %s"
register: assemble_syntax_error
ignore_errors: yes
- name: assert assemble failed with ignore_errors
assert:
that:
- assemble_syntax_error.failed
- "'Invalid command syntax' in assemble_syntax_error.msg"