Fixing up shell quoting issues

reviewable/pr18780/r1
James Cammarata 10 years ago
parent 6879e4ff0a
commit cf8905b2b0

@ -92,6 +92,24 @@ EXAMPLES = '''
creates: /path/to/database creates: /path/to/database
''' '''
# This is a pretty complex regex, which functions as follows:
#
# 1. (^|\s)
# ^ look for a space or the beginning of the line
# 2. (creates|removes|chdir|executable|NO_LOG)=
# ^ look for a valid param, followed by an '='
# 3. (?P<quote>[\'"])?
# ^ look for an optional quote character, which can either be
# a single or double quote character, and store it for later
# 4. (.*?)
# ^ match everything in a non-greedy manner until...
# 5. (?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)
# ^ a non-escaped space or a non-escaped quote of the same kind
# that was matched in the first 'quote' is found, or the end of
# the line is reached
PARAM_REGEX = re.compile(r'(^|\s)(creates|removes|chdir|executable|NO_LOG)=(?P<quote>[\'"])?(.*?)(?(quote)(?<!\\)(?P=quote))((?<!\\)(?=\s)|$)')
def main(): def main():
# the command module is the one ansible module that does not take key=value args # the command module is the one ansible module that does not take key=value args
@ -201,7 +219,6 @@ class CommandModule(AnsibleModule):
lexer.ignore_quotes = "'" lexer.ignore_quotes = "'"
items = list(lexer) items = list(lexer)
command_args = ''
for x in items: for x in items:
if '=' in x: if '=' in x:
# check to see if this is a special parameter for the command # check to see if this is a special parameter for the command
@ -216,13 +233,9 @@ class CommandModule(AnsibleModule):
if not (os.path.exists(v)): if not (os.path.exists(v)):
self.fail_json(rc=258, msg="cannot use executable '%s': file does not exist" % v) self.fail_json(rc=258, msg="cannot use executable '%s': file does not exist" % v)
params[k] = v params[k] = v
else: # Remove any of the above k=v params from the args string
# this isn't a valid parameter, so just append it back to the list of arguments args = PARAM_REGEX.sub('', args)
command_args = "%s %s" % (command_args, x) params['args'] = args
else:
# not a param, so just append it to the list of arguments
command_args = "%s %s" % (command_args, x)
params['args'] = command_args.strip()
return (params, params['args']) return (params, params['args'])
main() main()

Loading…
Cancel
Save