From 609c7920729c641c69c0c553e485b46ce304c387 Mon Sep 17 00:00:00 2001 From: Alicia Cozine Date: Thu, 7 Jun 2018 14:24:13 -0500 Subject: [PATCH] Better error message if the template is not utf-8 encoded (#41030) (#41265) * Better error message if the template is not utf-8 encoded Also document this in the porting guide (cherry picked from commit cef4d862bc944fbcab082f2710575f2c14c7cd54) --- docs/docsite/rst/porting_guides/porting_guide_2.5.rst | 6 ++++++ lib/ansible/plugins/action/template.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/docsite/rst/porting_guides/porting_guide_2.5.rst b/docs/docsite/rst/porting_guides/porting_guide_2.5.rst index 1b9acf30e8e..e4c6bf17739 100644 --- a/docs/docsite/rst/porting_guides/porting_guide_2.5.rst +++ b/docs/docsite/rst/porting_guides/porting_guide_2.5.rst @@ -197,6 +197,12 @@ desired. * The :ref:`blockinfile module ` had its ``follow`` parameter removed because it inherently modifies the content of an existing file so it makes no sense to operate on the link itself. + * In Ansible-2.5.3, the :ref:`template module ` became more strict about its + ``src`` file being proper utf-8. Previously, non-utf8 contents in a template module src file + would result in a mangled output file (the non-utf8 characters would be replaced with a unicode + replacement character). Now, on Python2, the module will error out with the message, "Template + source files must be utf-8 encoded". On Python3, the module will first attempt to pass the + non-utf8 characters through verbatim and fail if that does not succeed. Plugins ======= diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index f9374072671..f92e255b806 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -85,7 +85,10 @@ class ActionModule(ActionBase): # template the source data locally & get ready to transfer try: with open(b_tmp_source, 'rb') as f: - template_data = to_text(f.read(), errors='surrogate_or_strict') + try: + template_data = to_text(f.read(), errors='surrogate_or_strict') + except UnicodeError: + raise AnsibleActionFail("Template source files must be utf-8 encoded") # set jinja2 internal search path for includes searchpath = task_vars.get('ansible_search_path', [])