From 6a68be4e28e1b969af5ea93f79db5054b4f62713 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 22 Dec 2014 10:30:36 -0800 Subject: [PATCH] Handle quoting of values in dict parameters --- lib/ansible/module_utils/basic.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index d110337226..3c2def324a 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1070,7 +1070,32 @@ class AnsibleModule(object): raise TypeError('unable to evaluate string as dictionary') return result elif '=' in value: - return dict([x.strip().split("=", 1) for x in value.split(",")]) + fields = [] + field_buffer = [] + in_quote = False + in_escape = False + for c in value.strip(): + if in_escape: + field_buffer.append(c) + in_escape = False + elif c == '\\': + in_escape = True + elif not in_quote and c in ('\'', '"'): + in_quote = c + elif in_quote and in_quote == c: + in_quote = False + elif not in_quote and c in (',', ' '): + field = ''.join(field_buffer) + if field: + fields.append(field) + field_buffer = [] + else: + field_buffer.append(c) + + field = ''.join(field_buffer) + if field: + fields.append(field) + return dict(x.split("=", 1) for x in fields) else: raise TypeError("dictionary requested, could not parse JSON or key=value")