From 55135c0825f12db92ad02df1c493a7d0d4bcfaa8 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 1 May 2017 15:21:44 -0400 Subject: [PATCH] enable run_command to use non /bin/sh shells fixes #24169 --- lib/ansible/module_utils/basic.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index bed5a83c32..0de6048741 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -2313,25 +2313,33 @@ class AnsibleModule(object): strings on python3, use encoding=None to turn decoding to text off. ''' - shell = False if isinstance(args, list): if use_unsafe_shell: - args = " ".join([shlex_quote(x) for x in args]) + args = " ".join([pipes.quote(x) for x in args]) shell = True elif isinstance(args, (binary_type, text_type)) and use_unsafe_shell: shell = True elif isinstance(args, (binary_type, text_type)): - # On python2.6 and below, shlex has problems with text type - # On python3, shlex needs a text type. - if PY2: - args = to_bytes(args, errors='surrogate_or_strict') - elif PY3: - args = to_text(args, errors='surrogateescape') - args = shlex.split(args) + if not use_unsafe_shell: + # On python2.6 and below, shlex has problems with text type + # On python3, shlex needs a text type. + if PY2: + args = to_bytes(args, errors='surrogate_or_strict') + elif PY3: + args = to_text(args, errors='surrogateescape') + args = shlex.split(args) else: msg = "Argument 'args' to run_command must be list or string" self.fail_json(rc=257, cmd=args, msg=msg) + shell = False + if use_unsafe_shell: + executable = os.environ.get('SHELL') + if executable: + args = [executable, '-c', args] + else: + shell = True + prompt_re = None if prompt_regex: if isinstance(prompt_regex, text_type):