Skip to content

Commit 07b2698

Browse files
sivelbcoca
authored andcommitted
Add new expand_shell argument for run_command, to disable expanding shellisms (ansible#45620)
* Add new expand_shell argument for run_command, to disable expanding shellisms. Fixes ansible#45418 * s/expand_shell/expand_user_and_vars/g
1 parent e46ce16 commit 07b2698

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
minor_changes:
2+
- run_command - Add a new keyword argument expand_user_and_vars, which defaults to True,
3+
allowing the module author to decide whether or paths and variables
4+
are expanded before running the command when use_unsafe_shell=False
5+
(https://github.com/ansible/ansible/issues/45418)

lib/ansible/module_utils/basic.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -2680,7 +2680,8 @@ def _clean_args(self, args):
26802680
return self._clean
26812681

26822682
def run_command(self, args, check_rc=False, close_fds=True, executable=None, data=None, binary_data=False, path_prefix=None, cwd=None,
2683-
use_unsafe_shell=False, prompt_regex=None, environ_update=None, umask=None, encoding='utf-8', errors='surrogate_or_strict'):
2683+
use_unsafe_shell=False, prompt_regex=None, environ_update=None, umask=None, encoding='utf-8', errors='surrogate_or_strict',
2684+
expand_user_and_vars=True):
26842685
'''
26852686
Execute a command, returns rc, stdout, and stderr.
26862687
@@ -2718,6 +2719,11 @@ def run_command(self, args, check_rc=False, close_fds=True, executable=None, dat
27182719
python3 versions we support) otherwise a UnicodeError traceback
27192720
will be raised. This does not affect transformations of strings
27202721
given as args.
2722+
:kw expand_user_and_vars: When ``use_unsafe_shell=False`` this argument
2723+
dictates whether ``~`` is expanded in paths and environment variables
2724+
are expanded before running the command. When ``True`` a string such as
2725+
``$SHELL`` will be expanded regardless of escaping. When ``False`` and
2726+
``use_unsafe_shell=False`` no path or variable expansion will be done.
27212727
:returns: A 3-tuple of return code (integer), stdout (native string),
27222728
and stderr (native string). On python2, stdout and stderr are both
27232729
byte strings. On python3, stdout and stderr are text strings converted
@@ -2756,8 +2762,11 @@ def run_command(self, args, check_rc=False, close_fds=True, executable=None, dat
27562762
args = to_text(args, errors='surrogateescape')
27572763
args = shlex.split(args)
27582764

2759-
# expand shellisms
2760-
args = [os.path.expanduser(os.path.expandvars(x)) for x in args if x is not None]
2765+
# expand ``~`` in paths, and all environment vars
2766+
if expand_user_and_vars:
2767+
args = [os.path.expanduser(os.path.expandvars(x)) for x in args if x is not None]
2768+
else:
2769+
args = [x for x in args if x is not None]
27612770

27622771
prompt_re = None
27632772
if prompt_regex:

0 commit comments

Comments
 (0)