From e6fa50a3063a12598792fbf4d37473a130e9426d Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 29 Jul 2014 14:16:47 -0500 Subject: [PATCH] Move splitter to module_utils so modules can use it and fix command arg splitting Fixes #8338 --- lib/ansible/{utils => module_utils}/splitter.py | 0 lib/ansible/playbook/play.py | 5 +++-- lib/ansible/runner/__init__.py | 2 +- lib/ansible/utils/__init__.py | 2 +- library/commands/command | 9 +++------ test/units/TestUtils.py | 2 +- 6 files changed, 9 insertions(+), 11 deletions(-) rename lib/ansible/{utils => module_utils}/splitter.py (100%) diff --git a/lib/ansible/utils/splitter.py b/lib/ansible/module_utils/splitter.py similarity index 100% rename from lib/ansible/utils/splitter.py rename to lib/ansible/module_utils/splitter.py diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 8f8ed36a78..2a1d570c7e 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -21,6 +21,7 @@ from ansible.utils.template import template from ansible import utils from ansible import errors from ansible.playbook.task import Task +from ansible.module_utils.splitter import split_args, unquote import ansible.constants as C import pipes import shlex @@ -524,7 +525,7 @@ class Play(object): task_vars['_original_file'] = original_file if 'include' in x: - tokens = utils.splitter.split_args(str(x['include'])) + tokens = split_args(str(x['include'])) included_additional_conditions = list(additional_conditions) include_vars = {} for k in x: @@ -573,7 +574,7 @@ class Play(object): mv = task_vars.copy() for t in tokens[1:]: (k,v) = t.split("=", 1) - v = utils.splitter.unquote(v) + v = unquote(v) mv[k] = template(self.basedir, v, mv) dirname = self.basedir if original_file: diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 2a7e66966d..980254f644 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -47,7 +47,7 @@ import connection from return_data import ReturnData from ansible.callbacks import DefaultRunnerCallbacks, vv from ansible.module_common import ModuleReplacer -from ansible.utils.splitter import split_args +from ansible.module_utils.splitter import split_args module_replacer = ModuleReplacer(strip_comments=False) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index b1e4051ec6..19f31140d7 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -29,7 +29,7 @@ from ansible import __version__ from ansible.utils.display_functions import * from ansible.utils.plugins import * from ansible.callbacks import display -from ansible.utils.splitter import split_args, unquote +from ansible.module_utils.splitter import split_args, unquote import ansible.constants as C import ast import time diff --git a/library/commands/command b/library/commands/command index 0577182e91..ce54c83c38 100644 --- a/library/commands/command +++ b/library/commands/command @@ -186,6 +186,7 @@ def main(): # import module snippets from ansible.module_utils.basic import * +from ansible.module_utils.splitter import * # only the command module should ever need to do this # everything else should be simple key=value @@ -211,18 +212,14 @@ class CommandModule(AnsibleModule): args = args.replace("#USE_SHELL", "") params['shell'] = True - # use shlex to split up the args, while being careful to preserve - # single quotes so they're not removed accidentally - lexer = shlex.shlex(args) - lexer.whitespace = '\t ' - lexer.whitespace_split = True - items = list(lexer) + items = split_args(args) for x in items: quoted = x.startswith('"') and x.endswith('"') or x.startswith("'") and x.endswith("'") if '=' in x and not quoted: # check to see if this is a special parameter for the command k, v = x.split('=', 1) + v = unquote(v) # because we're not breaking out quotes in the shlex split # above, the value of the k=v pair may still be quoted. If # so, remove them. diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index 8b22784a12..be99292051 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -17,7 +17,7 @@ import ansible.utils import ansible.errors import ansible.constants as C import ansible.utils.template as template2 -from ansible.utils.splitter import split_args +from ansible.module_utils.splitter import split_args from ansible import __version__