From fb34842a05b31cf57158407c2cedc8d2ef9c2f2f Mon Sep 17 00:00:00 2001
From: jaypei <jaypei97159@gmail.com>
Date: Wed, 29 Apr 2015 19:59:55 +0800
Subject: [PATCH 1/2] use the right way to unescape line string Reference
 https://github.com/ansible/ansible/issues/10864

---
 files/lineinfile.py | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)

diff --git a/files/lineinfile.py b/files/lineinfile.py
index 2f7154e17be..2bdac7d8f0c 100644
--- a/files/lineinfile.py
+++ b/files/lineinfile.py
@@ -370,25 +370,13 @@ def main():
 
         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)
-
-        # always add one layer of quotes
-        line = "'%s'" % 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', line)
-        line = module.safe_eval(line)
 
-        # Now remove quotes around the string, if needed after
-        # removing the layer we added above
-        line = unquote(line)
-        if should_unquote:
-            line = unquote(line)
+        line = line.decode("string_escape")
 
         present(module, dest, params['regexp'], line,
                 ins_aft, ins_bef, create, backup, backrefs)

From 142760658fda9d1c44db211a78fd4523180e7e25 Mon Sep 17 00:00:00 2001
From: Toshio Kuratomi <toshio@fedoraproject.org>
Date: Sun, 17 May 2015 09:15:57 -0700
Subject: [PATCH 2/2] Slightly more future-proof version of the lineinfile fix

---
 files/lineinfile.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/files/lineinfile.py b/files/lineinfile.py
index 03bda07cad1..d8b50c13cec 100644
--- a/files/lineinfile.py
+++ b/files/lineinfile.py
@@ -19,9 +19,10 @@
 # You should have received a copy of the GNU General Public License
 # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
 
-import pipes
 import re
 import os
+import pipes
+import codecs
 import tempfile
 
 DOCUMENTATION = """
@@ -375,7 +376,7 @@ def main():
         if backrefs:
             line = re.sub(r'(\\[0-9]{1,3})', r'\\\1', line)
 
-        line = line.decode("string_escape")
+        line = codecs.escape_decode(line)
 
         present(module, dest, params['regexp'], line,
                 ins_aft, ins_bef, create, backup, backrefs)
@@ -388,5 +389,5 @@ def main():
 # import module snippets
 from ansible.module_utils.basic import *
 from ansible.module_utils.splitter import *
-
-main()
+if __name__ == '__main__':
+    main()