summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2014-08-11 11:22:59 -0500
committerJames Cammarata <jimi@sngx.net>2014-08-14 15:02:04 -0500
commitdf136769ebd2772224d0467ac537be2e380fa271 (patch)
tree51618338fdb56628c92aa5c3aaa2394c673a8dd2
parentf1f88f8904018b8e7336a646c975b2d8bcf2c98b (diff)
downloadansible-df136769ebd2772224d0467ac537be2e380fa271.tar.gz
Make sure we only use unquote on quoted lines in lineinfile when needed
-rw-r--r--lib/ansible/module_utils/splitter.py5
-rw-r--r--library/files/lineinfile18
2 files changed, 17 insertions, 6 deletions
diff --git a/lib/ansible/module_utils/splitter.py b/lib/ansible/module_utils/splitter.py
index 3daf3012e1..3f0fd8a5a2 100644
--- a/lib/ansible/module_utils/splitter.py
+++ b/lib/ansible/module_utils/splitter.py
@@ -187,9 +187,12 @@ def split_args(args):
return params
+def is_quoted(data):
+ return len(data) > 0 and (data[0] == '"' and data[-1] == '"' or data[0] == "'" and data[-1] == "'")
+
def unquote(data):
''' removes first and last quotes from a string, if the string starts and ends with the same quotes '''
- if len(data) > 0 and (data[0] == '"' and data[-1] == '"' or data[0] == "'" and data[-1] == "'"):
+ if is_quoted(data):
return data[1:-1]
return data
diff --git a/library/files/lineinfile b/library/files/lineinfile
index f40ebb341e..ba842e15e2 100644
--- a/library/files/lineinfile
+++ b/library/files/lineinfile
@@ -363,14 +363,22 @@ def main():
if ins_bef is None and ins_aft is None:
ins_aft = 'EOF'
+ line = params['line']
+
+ # The safe_eval call will remove some quoting, but not others,
+ # so we need to know if we should specifically unquote it.
+ should_unquote = not is_quoted(line)
+
# Replace escape sequences like '\n' while being sure
# not to replace octal escape sequences (\ooo) since they
- # match the backref syntax
+ # match the backref syntax.
if backrefs:
- line = re.sub(r'(\\[0-9]{1,3})', r'\\\1', params['line'])
- else:
- line = params['line']
- line = unquote(module.safe_eval(pipes.quote(line)))
+ line = re.sub(r'(\\[0-9]{1,3})', r'\\\1', line)
+ line = module.safe_eval(pipes.quote(line))
+
+ # Now remove quotes around the string, if needed
+ if should_unquote:
+ line = unquote(line)
present(module, dest, params['regexp'], line,
ins_aft, ins_bef, create, backup, backrefs)