From 8f325afe38b19f2f44dd08bce182704221eda699 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 8 Apr 2014 11:42:29 -0500 Subject: [PATCH] Fixing newline escapes in lineinfile Fixes #5679 --- files/lineinfile | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/files/lineinfile b/files/lineinfile index f781911ccd1..f5181cbe8ff 100644 --- a/files/lineinfile +++ b/files/lineinfile @@ -18,6 +18,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +import pipes import re import os import tempfile @@ -351,9 +352,14 @@ def main(): if ins_bef is None and ins_aft is None: ins_aft = 'EOF' - # Replace the newline character with an actual newline. Don't replace - # escaped \\n, hence sub and not str.replace. - line = re.sub(r'\n', os.linesep, params['line']) + # Replace escape sequences like '\n' while being sure + # not to replace octal escape sequences (\ooo) since they + # match the backref syntax + if backrefs: + line = re.sub(r'(\\[0-9]{1,3})', r'\\\1', params['line']) + else: + line = params['line'] + line = module.safe_eval(pipes.quote(line)) present(module, dest, params['regexp'], line, ins_aft, ins_bef, create, backup, backrefs)