We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Template module not accepting source that contains UTF8 characters
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
template

##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.3.1.0
  config file = /work/ansible/configuration/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### OS / ENVIRONMENT
Ansible runs on CentOS 7.3
Target system on RHEL 6.9

##### SUMMARY
This looks like bug #10110:
Using the template module, when the source name contains an UTF8 character, the module fails with the following error.
Replacing template: with copy:, there is no error (the "copy" module has been used for more than a year and is just now being replaced by template).
If there is UTF8 only in the destination name, there is no error.


##### STEPS TO REPRODUCE

This sample triggers the error:
```
- name: Test UTF8
  template:
    src:    "/tmp/ééé"
    dest:   "/tmp/eee"
```
This sample works:
```
- name: Test UTF8
  template:
    src:    "/tmp/eee"
    dest:   "/tmp/ééé"
```


##### EXPECTED RESULTS
The template being copied to the target.

##### ACTUAL RESULTS

```
TASK [task : Test UTF8 *********************************************************************************************
[WARNING]: Failure using method (v2_runner_item_on_failed) in callback plugin (<ansible.plugins.callback.default.CallbackModule object at 0x35eb190>):
'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `lib/ansible/plugins/action/template.py`
Content:
```
1 # (c) 2015, Michael DeHaan <michael.dehaan@gmail.com>
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
17 from __future__ import (absolute_import, division, print_function)
18 __metaclass__ = type
19 
20 import os
21 import shutil
22 import stat
23 import tempfile
24 
25 from ansible import constants as C
26 from ansible.config.manager import ensure_type
27 from ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleAction, AnsibleActionFail
28 from ansible.module_utils._text import to_bytes, to_text
29 from ansible.module_utils.parsing.convert_bool import boolean
30 from ansible.module_utils.six import string_types
31 from ansible.plugins.action import ActionBase
32 from ansible.template import generate_ansible_template_vars
33 
34 
35 class ActionModule(ActionBase):
36 
37     TRANSFERS_FILES = True
38     DEFAULT_NEWLINE_SEQUENCE = "\n"
39 
40     def run(self, tmp=None, task_vars=None):
41         ''' handler for template operations '''
42 
43         if task_vars is None:
44             task_vars = dict()
45 
46         result = super(ActionModule, self).run(tmp, task_vars)
47         del tmp  # tmp no longer has any effect
48 
49         source = self._task.args.get('src', None)
50         dest = self._task.args.get('dest', None)
51         force = boolean(self._task.args.get('force', True), strict=False)
52         follow = boolean(self._task.args.get('follow', False), strict=False)
53         state = self._task.args.get('state', None)
54         newline_sequence = self._task.args.get('newline_sequence', self.DEFAULT_NEWLINE_SEQUENCE)
55         variable_start_string = self._task.args.get('variable_start_string', None)
56         variable_end_string = self._task.args.get('variable_end_string', None)
57         block_start_string = self._task.args.get('block_start_string', None)
58         block_end_string = self._task.args.get('block_end_string', None)
59         trim_blocks = boolean(self._task.args.get('trim_blocks', True), strict=False)
60         lstrip_blocks = boolean(self._task.args.get('lstrip_blocks', False), strict=False)
61 
62         # Option `lstrip_blocks' was added in Jinja2 version 2.7.
63         if lstrip_blocks:
64             try:
65                 import jinja2.defaults
66             except ImportError:
67                 raise AnsibleError('Unable to import Jinja2 defaults for determing Jinja2 features.')
68 
69             try:
70                 jinja2.defaults.LSTRIP_BLOCKS
71             except AttributeError:
72                 raise AnsibleError("Option `lstrip_blocks' is only available in Jinja2 versions >=2.7")
73 
74         wrong_sequences = ["\\n", "\\r", "\\r\\n"]
75         allowed_sequences = ["\n", "\r", "\r\n"]
76 
77         # We need to convert unescaped sequences to proper escaped sequences for Jinja2
78         if newline_sequence in wrong_sequences:
79             newline_sequence = allowed_sequences[wrong_sequences.index(newline_sequence)]
80 
81         try:
82             for s_type in ('source', 'dest', 'state', 'newline_sequence', 'variable_start_string', 'variable_end_string', 'block_start_string',
83                            'block_end_string'):
84                 value = locals()[s_type]
85                 value = ensure_type(value, 'string')
86                 if value is not None and not isinstance(value, string_types):
87                     raise AnsibleActionFail("%s is expected to be a string, but got %s instead" % (s_type, type(value)))
88                 locals()[s_type] = value
89 
90             for b_type in ('force', 'follow', 'trim_blocks'):
91                 value = locals()[b_type]
92                 value = ensure_type(value, 'string')
93                 if value is not None and not isinstance(value, bool):
94                     raise AnsibleActionFail("%s is expected to be a boolean, but got %s instead" % (b_type, type(value)))
95                 locals()[b_type] = value
96 
97             if state is not None:
98                 raise AnsibleActionFail("'state' cannot be specified on a template")
99             elif source is None or dest is None:
100                 raise AnsibleActionFail("src and dest are required")
101             elif newline_sequence not in allowed_sequences:
102                 raise AnsibleActionFail("newline_sequence needs to be one of: \n, \r or \r\n")
103             else:
104                 try:
105                     source = self._find_needle('templates', source)
106                 except AnsibleError as e:
107                     raise AnsibleActionFail(to_text(e))
108 
109             mode = self._task.args.get('mode', None)
110             if mode == 'preserve':
111                 mode = '0%03o' % stat.S_IMODE(os.stat(source).st_mode)
112 
113             # Get vault decrypted tmp file
114             try:
115                 tmp_source = self._loader.get_real_file(source)
116             except AnsibleFileNotFound as e:
117                 raise AnsibleActionFail("could not find src=%s, %s" % (source, to_text(e)))
118 
119             # template the source data locally & get ready to transfer
120             try:
121                 with open(tmp_source, 'r') as f:
122                     template_data = to_text(f.read())
123 
124                 # set jinja2 internal search path for includes
125                 searchpath = task_vars.get('ansible_search_path', [])
126                 searchpath.extend([self._loader._basedir, os.path.dirname(source)])
127 
128                 # We want to search into the 'templates' subdir of each search path in
129                 # addition to our original search paths.
130                 newsearchpath = []
131                 for p in searchpath:
132                     newsearchpath.append(os.path.join(p, 'templates'))
133                     newsearchpath.append(p)
134                 searchpath = newsearchpath
135 
136                 self._templar.environment.loader.searchpath = searchpath
137                 self._templar.environment.newline_sequence = newline_sequence
138                 if block_start_string is not None:
139                     self._templar.environment.block_start_string = block_start_string
140                 if block_end_string is not None:
141                     self._templar.environment.block_end_string = block_end_string
142                 if variable_start_string is not None:
143                     self._templar.environment.variable_start_string = variable_start_string
144                 if variable_end_string is not None:
145                     self._templar.environment.variable_end_string = variable_end_string
146                 self._templar.environment.trim_blocks = trim_blocks
147                 self._templar.environment.lstrip_blocks = lstrip_blocks
148 
149                 # add ansible 'template' vars
150                 temp_vars = task_vars.copy()
151                 temp_vars.update(generate_ansible_template_vars(source))
152 
153                 old_vars = self._templar._available_variables
154                 self._templar.set_available_variables(temp_vars)
155                 resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
156                 self._templar.set_available_variables(old_vars)
157             except AnsibleAction:
158                 raise
159             except Exception as e:
160                 raise AnsibleActionFail("%s: %s" % (type(e).__name__, to_text(e)))
161             finally:
162                 self._loader.cleanup_tmp_file(tmp_source)
163 
164             new_task = self._task.copy()
165             # mode is either the mode from task.args or the mode of the source file if the task.args
166             # mode == 'preserve'
167             new_task.args['mode'] = mode
168             new_task.args.pop('newline_sequence', None)
169             new_task.args.pop('block_start_string', None)
170             new_task.args.pop('block_end_string', None)
171             new_task.args.pop('variable_start_string', None)
172             new_task.args.pop('variable_end_string', None)
173             new_task.args.pop('trim_blocks', None)
174             new_task.args.pop('lstrip_blocks', None)
175 
176             local_tempdir = tempfile.mkdtemp(dir=C.DEFAULT_LOCAL_TMP)
177 
178             try:
179                 result_file = os.path.join(local_tempdir, os.path.basename(source))
180                 with open(result_file, 'wb') as f:
181                     f.write(to_bytes(resultant, errors='surrogate_or_strict'))
182 
183                 new_task.args.update(
184                     dict(
185                         src=result_file,
186                         dest=dest,
187                         follow=follow,
188                     ),
189                 )
190                 copy_action = self._shared_loader_obj.action_loader.get('copy',
191                                                                         task=new_task,
192                                                                         connection=self._connection,
193                                                                         play_context=self._play_context,
194                                                                         loader=self._loader,
195                                                                         templar=self._templar,
196                                                                         shared_loader_obj=self._shared_loader_obj)
197                 result.update(copy_action.run(task_vars=task_vars))
198             finally:
199                 shutil.rmtree(local_tempdir)
200 
201         except AnsibleAction as e:
202             result.update(e.result)
203         finally:
204             self._remove_tmp_path(self._connection._shell.tmpdir)
205 
206         return result
207
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py
--- a/lib/ansible/plugins/action/template.py
+++ b/lib/ansible/plugins/action/template.py
@@ -115,11 +115,12 @@
                 tmp_source = self._loader.get_real_file(source)
             except AnsibleFileNotFound as e:
                 raise AnsibleActionFail("could not find src=%s, %s" % (source, to_text(e)))
+            b_tmp_source = to_bytes(tmp_source, errors='surrogate_or_strict')
 
             # template the source data locally & get ready to transfer
             try:
-                with open(tmp_source, 'r') as f:
-                    template_data = to_text(f.read())
+                with open(b_tmp_source, 'rb') as f:
+                    template_data = to_text(f.read(), errors='surrogate_or_strict')
 
                 # set jinja2 internal search path for includes
                 searchpath = task_vars.get('ansible_search_path', [])
@@ -159,7 +160,7 @@
             except Exception as e:
                 raise AnsibleActionFail("%s: %s" % (type(e).__name__, to_text(e)))
             finally:
-                self._loader.cleanup_tmp_file(tmp_source)
+                self._loader.cleanup_tmp_file(b_tmp_source)
 
             new_task = self._task.copy()
             # mode is either the mode from task.args or the mode of the source file if the task.args
@@ -177,7 +178,7 @@
 
             try:
                 result_file = os.path.join(local_tempdir, os.path.basename(source))
-                with open(result_file, 'wb') as f:
+                with open(to_bytes(result_file, errors='surrogate_or_strict'), 'wb') as f:
                     f.write(to_bytes(resultant, errors='surrogate_or_strict'))
 
                 new_task.args.update(
@@ -196,7 +197,7 @@
                                                                         shared_loader_obj=self._shared_loader_obj)
                 result.update(copy_action.run(task_vars=task_vars))
             finally:
-                shutil.rmtree(local_tempdir)
+                shutil.rmtree(to_bytes(local_tempdir, errors='surrogate_or_strict'))
 
         except AnsibleAction as e:
             result.update(e.result)

{"golden_diff": "diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py\n--- a/lib/ansible/plugins/action/template.py\n+++ b/lib/ansible/plugins/action/template.py\n@@ -115,11 +115,12 @@\n                 tmp_source = self._loader.get_real_file(source)\n             except AnsibleFileNotFound as e:\n                 raise AnsibleActionFail(\"could not find src=%s, %s\" % (source, to_text(e)))\n+            b_tmp_source = to_bytes(tmp_source, errors='surrogate_or_strict')\n \n             # template the source data locally & get ready to transfer\n             try:\n-                with open(tmp_source, 'r') as f:\n-                    template_data = to_text(f.read())\n+                with open(b_tmp_source, 'rb') as f:\n+                    template_data = to_text(f.read(), errors='surrogate_or_strict')\n \n                 # set jinja2 internal search path for includes\n                 searchpath = task_vars.get('ansible_search_path', [])\n@@ -159,7 +160,7 @@\n             except Exception as e:\n                 raise AnsibleActionFail(\"%s: %s\" % (type(e).__name__, to_text(e)))\n             finally:\n-                self._loader.cleanup_tmp_file(tmp_source)\n+                self._loader.cleanup_tmp_file(b_tmp_source)\n \n             new_task = self._task.copy()\n             # mode is either the mode from task.args or the mode of the source file if the task.args\n@@ -177,7 +178,7 @@\n \n             try:\n                 result_file = os.path.join(local_tempdir, os.path.basename(source))\n-                with open(result_file, 'wb') as f:\n+                with open(to_bytes(result_file, errors='surrogate_or_strict'), 'wb') as f:\n                     f.write(to_bytes(resultant, errors='surrogate_or_strict'))\n \n                 new_task.args.update(\n@@ -196,7 +197,7 @@\n                                                                         shared_loader_obj=self._shared_loader_obj)\n                 result.update(copy_action.run(task_vars=task_vars))\n             finally:\n-                shutil.rmtree(local_tempdir)\n+                shutil.rmtree(to_bytes(local_tempdir, errors='surrogate_or_strict'))\n \n         except AnsibleAction as e:\n             result.update(e.result)\n", "issue": "Template module not accepting source that contains UTF8 characters\n##### ISSUE TYPE\r\n - Bug Report\r\n\r\n##### COMPONENT NAME\r\ntemplate\r\n\r\n##### ANSIBLE VERSION\r\n```\r\n$ ansible --version\r\nansible 2.3.1.0\r\n  config file = /work/ansible/configuration/ansible.cfg\r\n  configured module search path = Default w/o overrides\r\n  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]\r\n```\r\n\r\n##### OS / ENVIRONMENT\r\nAnsible runs on CentOS 7.3\r\nTarget system on RHEL 6.9\r\n\r\n##### SUMMARY\r\nThis looks like bug #10110:\r\nUsing the template module, when the source name contains an UTF8 character, the module fails with the following error.\r\nReplacing template: with copy:, there is no error (the \"copy\" module has been used for more than a year and is just now being replaced by template).\r\nIf there is UTF8 only in the destination name, there is no error.\r\n\r\n\r\n##### STEPS TO REPRODUCE\r\n\r\nThis sample triggers the error:\r\n```\r\n- name: Test UTF8\r\n  template:\r\n    src:    \"/tmp/\u00e9\u00e9\u00e9\"\r\n    dest:   \"/tmp/eee\"\r\n```\r\nThis sample works:\r\n```\r\n- name: Test UTF8\r\n  template:\r\n    src:    \"/tmp/eee\"\r\n    dest:   \"/tmp/\u00e9\u00e9\u00e9\"\r\n```\r\n\r\n\r\n##### EXPECTED RESULTS\r\nThe template being copied to the target.\r\n\r\n##### ACTUAL RESULTS\r\n\r\n```\r\nTASK [task : Test UTF8 *********************************************************************************************\r\n[WARNING]: Failure using method (v2_runner_item_on_failed) in callback plugin (<ansible.plugins.callback.default.CallbackModule object at 0x35eb190>):\r\n'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128\r\n```\r\n\n", "before_files": [{"content": "# (c) 2015, Michael DeHaan <michael.dehaan@gmail.com>\n#\n# This file is part of Ansible\n#\n# Ansible is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# Ansible is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.\nfrom __future__ import (absolute_import, division, print_function)\n__metaclass__ = type\n\nimport os\nimport shutil\nimport stat\nimport tempfile\n\nfrom ansible import constants as C\nfrom ansible.config.manager import ensure_type\nfrom ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleAction, AnsibleActionFail\nfrom ansible.module_utils._text import to_bytes, to_text\nfrom ansible.module_utils.parsing.convert_bool import boolean\nfrom ansible.module_utils.six import string_types\nfrom ansible.plugins.action import ActionBase\nfrom ansible.template import generate_ansible_template_vars\n\n\nclass ActionModule(ActionBase):\n\n    TRANSFERS_FILES = True\n    DEFAULT_NEWLINE_SEQUENCE = \"\\n\"\n\n    def run(self, tmp=None, task_vars=None):\n        ''' handler for template operations '''\n\n        if task_vars is None:\n            task_vars = dict()\n\n        result = super(ActionModule, self).run(tmp, task_vars)\n        del tmp  # tmp no longer has any effect\n\n        source = self._task.args.get('src', None)\n        dest = self._task.args.get('dest', None)\n        force = boolean(self._task.args.get('force', True), strict=False)\n        follow = boolean(self._task.args.get('follow', False), strict=False)\n        state = self._task.args.get('state', None)\n        newline_sequence = self._task.args.get('newline_sequence', self.DEFAULT_NEWLINE_SEQUENCE)\n        variable_start_string = self._task.args.get('variable_start_string', None)\n        variable_end_string = self._task.args.get('variable_end_string', None)\n        block_start_string = self._task.args.get('block_start_string', None)\n        block_end_string = self._task.args.get('block_end_string', None)\n        trim_blocks = boolean(self._task.args.get('trim_blocks', True), strict=False)\n        lstrip_blocks = boolean(self._task.args.get('lstrip_blocks', False), strict=False)\n\n        # Option `lstrip_blocks' was added in Jinja2 version 2.7.\n        if lstrip_blocks:\n            try:\n                import jinja2.defaults\n            except ImportError:\n                raise AnsibleError('Unable to import Jinja2 defaults for determing Jinja2 features.')\n\n            try:\n                jinja2.defaults.LSTRIP_BLOCKS\n            except AttributeError:\n                raise AnsibleError(\"Option `lstrip_blocks' is only available in Jinja2 versions >=2.7\")\n\n        wrong_sequences = [\"\\\\n\", \"\\\\r\", \"\\\\r\\\\n\"]\n        allowed_sequences = [\"\\n\", \"\\r\", \"\\r\\n\"]\n\n        # We need to convert unescaped sequences to proper escaped sequences for Jinja2\n        if newline_sequence in wrong_sequences:\n            newline_sequence = allowed_sequences[wrong_sequences.index(newline_sequence)]\n\n        try:\n            for s_type in ('source', 'dest', 'state', 'newline_sequence', 'variable_start_string', 'variable_end_string', 'block_start_string',\n                           'block_end_string'):\n                value = locals()[s_type]\n                value = ensure_type(value, 'string')\n                if value is not None and not isinstance(value, string_types):\n                    raise AnsibleActionFail(\"%s is expected to be a string, but got %s instead\" % (s_type, type(value)))\n                locals()[s_type] = value\n\n            for b_type in ('force', 'follow', 'trim_blocks'):\n                value = locals()[b_type]\n                value = ensure_type(value, 'string')\n                if value is not None and not isinstance(value, bool):\n                    raise AnsibleActionFail(\"%s is expected to be a boolean, but got %s instead\" % (b_type, type(value)))\n                locals()[b_type] = value\n\n            if state is not None:\n                raise AnsibleActionFail(\"'state' cannot be specified on a template\")\n            elif source is None or dest is None:\n                raise AnsibleActionFail(\"src and dest are required\")\n            elif newline_sequence not in allowed_sequences:\n                raise AnsibleActionFail(\"newline_sequence needs to be one of: \\n, \\r or \\r\\n\")\n            else:\n                try:\n                    source = self._find_needle('templates', source)\n                except AnsibleError as e:\n                    raise AnsibleActionFail(to_text(e))\n\n            mode = self._task.args.get('mode', None)\n            if mode == 'preserve':\n                mode = '0%03o' % stat.S_IMODE(os.stat(source).st_mode)\n\n            # Get vault decrypted tmp file\n            try:\n                tmp_source = self._loader.get_real_file(source)\n            except AnsibleFileNotFound as e:\n                raise AnsibleActionFail(\"could not find src=%s, %s\" % (source, to_text(e)))\n\n            # template the source data locally & get ready to transfer\n            try:\n                with open(tmp_source, 'r') as f:\n                    template_data = to_text(f.read())\n\n                # set jinja2 internal search path for includes\n                searchpath = task_vars.get('ansible_search_path', [])\n                searchpath.extend([self._loader._basedir, os.path.dirname(source)])\n\n                # We want to search into the 'templates' subdir of each search path in\n                # addition to our original search paths.\n                newsearchpath = []\n                for p in searchpath:\n                    newsearchpath.append(os.path.join(p, 'templates'))\n                    newsearchpath.append(p)\n                searchpath = newsearchpath\n\n                self._templar.environment.loader.searchpath = searchpath\n                self._templar.environment.newline_sequence = newline_sequence\n                if block_start_string is not None:\n                    self._templar.environment.block_start_string = block_start_string\n                if block_end_string is not None:\n                    self._templar.environment.block_end_string = block_end_string\n                if variable_start_string is not None:\n                    self._templar.environment.variable_start_string = variable_start_string\n                if variable_end_string is not None:\n                    self._templar.environment.variable_end_string = variable_end_string\n                self._templar.environment.trim_blocks = trim_blocks\n                self._templar.environment.lstrip_blocks = lstrip_blocks\n\n                # add ansible 'template' vars\n                temp_vars = task_vars.copy()\n                temp_vars.update(generate_ansible_template_vars(source))\n\n                old_vars = self._templar._available_variables\n                self._templar.set_available_variables(temp_vars)\n                resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)\n                self._templar.set_available_variables(old_vars)\n            except AnsibleAction:\n                raise\n            except Exception as e:\n                raise AnsibleActionFail(\"%s: %s\" % (type(e).__name__, to_text(e)))\n            finally:\n                self._loader.cleanup_tmp_file(tmp_source)\n\n            new_task = self._task.copy()\n            # mode is either the mode from task.args or the mode of the source file if the task.args\n            # mode == 'preserve'\n            new_task.args['mode'] = mode\n            new_task.args.pop('newline_sequence', None)\n            new_task.args.pop('block_start_string', None)\n            new_task.args.pop('block_end_string', None)\n            new_task.args.pop('variable_start_string', None)\n            new_task.args.pop('variable_end_string', None)\n            new_task.args.pop('trim_blocks', None)\n            new_task.args.pop('lstrip_blocks', None)\n\n            local_tempdir = tempfile.mkdtemp(dir=C.DEFAULT_LOCAL_TMP)\n\n            try:\n                result_file = os.path.join(local_tempdir, os.path.basename(source))\n                with open(result_file, 'wb') as f:\n                    f.write(to_bytes(resultant, errors='surrogate_or_strict'))\n\n                new_task.args.update(\n                    dict(\n                        src=result_file,\n                        dest=dest,\n                        follow=follow,\n                    ),\n                )\n                copy_action = self._shared_loader_obj.action_loader.get('copy',\n                                                                        task=new_task,\n                                                                        connection=self._connection,\n                                                                        play_context=self._play_context,\n                                                                        loader=self._loader,\n                                                                        templar=self._templar,\n                                                                        shared_loader_obj=self._shared_loader_obj)\n                result.update(copy_action.run(task_vars=task_vars))\n            finally:\n                shutil.rmtree(local_tempdir)\n\n        except AnsibleAction as e:\n            result.update(e.result)\n        finally:\n            self._remove_tmp_path(self._connection._shell.tmpdir)\n\n        return result\n", "path": "lib/ansible/plugins/action/template.py"}], "after_files": [{"content": "# (c) 2015, Michael DeHaan <michael.dehaan@gmail.com>\n#\n# This file is part of Ansible\n#\n# Ansible is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# Ansible is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.\nfrom __future__ import (absolute_import, division, print_function)\n__metaclass__ = type\n\nimport os\nimport shutil\nimport stat\nimport tempfile\n\nfrom ansible import constants as C\nfrom ansible.config.manager import ensure_type\nfrom ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleAction, AnsibleActionFail\nfrom ansible.module_utils._text import to_bytes, to_text\nfrom ansible.module_utils.parsing.convert_bool import boolean\nfrom ansible.module_utils.six import string_types\nfrom ansible.plugins.action import ActionBase\nfrom ansible.template import generate_ansible_template_vars\n\n\nclass ActionModule(ActionBase):\n\n    TRANSFERS_FILES = True\n    DEFAULT_NEWLINE_SEQUENCE = \"\\n\"\n\n    def run(self, tmp=None, task_vars=None):\n        ''' handler for template operations '''\n\n        if task_vars is None:\n            task_vars = dict()\n\n        result = super(ActionModule, self).run(tmp, task_vars)\n        del tmp  # tmp no longer has any effect\n\n        source = self._task.args.get('src', None)\n        dest = self._task.args.get('dest', None)\n        force = boolean(self._task.args.get('force', True), strict=False)\n        follow = boolean(self._task.args.get('follow', False), strict=False)\n        state = self._task.args.get('state', None)\n        newline_sequence = self._task.args.get('newline_sequence', self.DEFAULT_NEWLINE_SEQUENCE)\n        variable_start_string = self._task.args.get('variable_start_string', None)\n        variable_end_string = self._task.args.get('variable_end_string', None)\n        block_start_string = self._task.args.get('block_start_string', None)\n        block_end_string = self._task.args.get('block_end_string', None)\n        trim_blocks = boolean(self._task.args.get('trim_blocks', True), strict=False)\n        lstrip_blocks = boolean(self._task.args.get('lstrip_blocks', False), strict=False)\n\n        # Option `lstrip_blocks' was added in Jinja2 version 2.7.\n        if lstrip_blocks:\n            try:\n                import jinja2.defaults\n            except ImportError:\n                raise AnsibleError('Unable to import Jinja2 defaults for determing Jinja2 features.')\n\n            try:\n                jinja2.defaults.LSTRIP_BLOCKS\n            except AttributeError:\n                raise AnsibleError(\"Option `lstrip_blocks' is only available in Jinja2 versions >=2.7\")\n\n        wrong_sequences = [\"\\\\n\", \"\\\\r\", \"\\\\r\\\\n\"]\n        allowed_sequences = [\"\\n\", \"\\r\", \"\\r\\n\"]\n\n        # We need to convert unescaped sequences to proper escaped sequences for Jinja2\n        if newline_sequence in wrong_sequences:\n            newline_sequence = allowed_sequences[wrong_sequences.index(newline_sequence)]\n\n        try:\n            for s_type in ('source', 'dest', 'state', 'newline_sequence', 'variable_start_string', 'variable_end_string', 'block_start_string',\n                           'block_end_string'):\n                value = locals()[s_type]\n                value = ensure_type(value, 'string')\n                if value is not None and not isinstance(value, string_types):\n                    raise AnsibleActionFail(\"%s is expected to be a string, but got %s instead\" % (s_type, type(value)))\n                locals()[s_type] = value\n\n            for b_type in ('force', 'follow', 'trim_blocks'):\n                value = locals()[b_type]\n                value = ensure_type(value, 'string')\n                if value is not None and not isinstance(value, bool):\n                    raise AnsibleActionFail(\"%s is expected to be a boolean, but got %s instead\" % (b_type, type(value)))\n                locals()[b_type] = value\n\n            if state is not None:\n                raise AnsibleActionFail(\"'state' cannot be specified on a template\")\n            elif source is None or dest is None:\n                raise AnsibleActionFail(\"src and dest are required\")\n            elif newline_sequence not in allowed_sequences:\n                raise AnsibleActionFail(\"newline_sequence needs to be one of: \\n, \\r or \\r\\n\")\n            else:\n                try:\n                    source = self._find_needle('templates', source)\n                except AnsibleError as e:\n                    raise AnsibleActionFail(to_text(e))\n\n            mode = self._task.args.get('mode', None)\n            if mode == 'preserve':\n                mode = '0%03o' % stat.S_IMODE(os.stat(source).st_mode)\n\n            # Get vault decrypted tmp file\n            try:\n                tmp_source = self._loader.get_real_file(source)\n            except AnsibleFileNotFound as e:\n                raise AnsibleActionFail(\"could not find src=%s, %s\" % (source, to_text(e)))\n            b_tmp_source = to_bytes(tmp_source, errors='surrogate_or_strict')\n\n            # template the source data locally & get ready to transfer\n            try:\n                with open(b_tmp_source, 'rb') as f:\n                    template_data = to_text(f.read(), errors='surrogate_or_strict')\n\n                # set jinja2 internal search path for includes\n                searchpath = task_vars.get('ansible_search_path', [])\n                searchpath.extend([self._loader._basedir, os.path.dirname(source)])\n\n                # We want to search into the 'templates' subdir of each search path in\n                # addition to our original search paths.\n                newsearchpath = []\n                for p in searchpath:\n                    newsearchpath.append(os.path.join(p, 'templates'))\n                    newsearchpath.append(p)\n                searchpath = newsearchpath\n\n                self._templar.environment.loader.searchpath = searchpath\n                self._templar.environment.newline_sequence = newline_sequence\n                if block_start_string is not None:\n                    self._templar.environment.block_start_string = block_start_string\n                if block_end_string is not None:\n                    self._templar.environment.block_end_string = block_end_string\n                if variable_start_string is not None:\n                    self._templar.environment.variable_start_string = variable_start_string\n                if variable_end_string is not None:\n                    self._templar.environment.variable_end_string = variable_end_string\n                self._templar.environment.trim_blocks = trim_blocks\n                self._templar.environment.lstrip_blocks = lstrip_blocks\n\n                # add ansible 'template' vars\n                temp_vars = task_vars.copy()\n                temp_vars.update(generate_ansible_template_vars(source))\n\n                old_vars = self._templar._available_variables\n                self._templar.set_available_variables(temp_vars)\n                resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)\n                self._templar.set_available_variables(old_vars)\n            except AnsibleAction:\n                raise\n            except Exception as e:\n                raise AnsibleActionFail(\"%s: %s\" % (type(e).__name__, to_text(e)))\n            finally:\n                self._loader.cleanup_tmp_file(b_tmp_source)\n\n            new_task = self._task.copy()\n            # mode is either the mode from task.args or the mode of the source file if the task.args\n            # mode == 'preserve'\n            new_task.args['mode'] = mode\n            new_task.args.pop('newline_sequence', None)\n            new_task.args.pop('block_start_string', None)\n            new_task.args.pop('block_end_string', None)\n            new_task.args.pop('variable_start_string', None)\n            new_task.args.pop('variable_end_string', None)\n            new_task.args.pop('trim_blocks', None)\n            new_task.args.pop('lstrip_blocks', None)\n\n            local_tempdir = tempfile.mkdtemp(dir=C.DEFAULT_LOCAL_TMP)\n\n            try:\n                result_file = os.path.join(local_tempdir, os.path.basename(source))\n                with open(to_bytes(result_file, errors='surrogate_or_strict'), 'wb') as f:\n                    f.write(to_bytes(resultant, errors='surrogate_or_strict'))\n\n                new_task.args.update(\n                    dict(\n                        src=result_file,\n                        dest=dest,\n                        follow=follow,\n                    ),\n                )\n                copy_action = self._shared_loader_obj.action_loader.get('copy',\n                                                                        task=new_task,\n                                                                        connection=self._connection,\n                                                                        play_context=self._play_context,\n                                                                        loader=self._loader,\n                                                                        templar=self._templar,\n                                                                        shared_loader_obj=self._shared_loader_obj)\n                result.update(copy_action.run(task_vars=task_vars))\n            finally:\n                shutil.rmtree(to_bytes(local_tempdir, errors='surrogate_or_strict'))\n\n        except AnsibleAction as e:\n            result.update(e.result)\n        finally:\n            self._remove_tmp_path(self._connection._shell.tmpdir)\n\n        return result\n", "path": "lib/ansible/plugins/action/template.py"}]}