Repository: ansible/ansible

Issue:
Wrong exception raised on templating error
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report


##### COMPONENT NAME
<!---
Name of the module, plugin, task or feature
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path
-->

core

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0 (topic/templating_reraise a735b912de) last updated 2018/01/23 18:38:20 (GMT +200)
  config file = None
  configured module search path = [u'/home/quinot/projects/ansible/lib/ansible/modules']
  ansible python module location = /home/quinot/projects/ansible/lib/ansible
  executable location = /home/quinot/projects/ansible/bin/ansible
  python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]

```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = [u'/home/quinot/projects/ansible/lib/ansible/modules']

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
When an exception is raised while processing a template, the code to annotate it with indications of the context where the exception occurred makes incorrect assumptions about exception constructors, resulting in another exception being raised. This makes it very difficult to identify the root cause of the issue.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Consider a lookup plugin that returns arbitrary binary data. This is not expected to work, and it is expected that the raised exception will give an indication pointing at this.

<!--- Paste example playbooks or commands between quotes below -->
```python
# lookup_plugins/toto.py
# python 3 headers, required if submitting to Ansible
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.lookup import LookupBase
import pickle

class LookupModule(LookupBase):

    def run(self, terms, variables=None, **kwargs):
        return [{"key": '\xff{{pouet'}]
```

Then run the following playbook:

```yaml
# toto.yml
- hosts: all
  tasks:
   - name: pouet
     debug: msg="test {{ item }}"
     with_toto: toto
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

```
$ ansible-playbook -i localhost, -c local toto.yml

PLAY [all] *************************************************************************************************

TASK [Gathering Facts] *************************************************************************************
ok: [localhost]

TASK [pouet] ***********************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "An unhandled exception occurred while templating '{'key': '\\xff{{pouet'}'. Error was a <type 'exceptions.UnicodeDecodeError'>, original message: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)"}
	to retry, use: --limit @/tmp/toto/toto.retry

PLAY RECAP *************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1 
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
$ ansible-playbook -i localhost, -c local -vvvv toto.yml
ansible-playbook 2.5.0 (detached HEAD ef6b478d2a) last updated 2018/01/24 10:35:35 (GMT +200)
  config file = None
  configured module search path = [u'/home/quinot/projects/ansible/lib/ansible/modules']
  ansible python module location = /home/quinot/projects/ansible/lib/ansible
  executable location = /home/quinot/projects/ansible/bin/ansible-playbook
  python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]
No config file found; using defaults
setting up inventory plugins
Set default localhost to localhost
Parsed localhost, inventory source with host_list plugin
Loading callback plugin default of type stdout, v2.0 from /home/quinot/projects/ansible/lib/ansible/plugins/callback/default.pyc

PLAYBOOK: toto.yml *****************************************************************************************
1 plays in toto.yml

PLAY [all] *************************************************************************************************

TASK [Gathering Facts] *************************************************************************************
task path: /tmp/toto/toto.yml:1
Using module file /home/quinot/projects/ansible/lib/ansible/modules/system/setup.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: quinot
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302 `" && echo ansible-tmp-1516786554.85-70695942021302="` echo /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302 `" ) && sleep 0'
<localhost> PUT /home/quinot/.ansible/tmp/ansible-local-270530bkmwA/tmpsdkk2k TO /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302/setup.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302/ /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302/setup.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/quinot/.ansible/tmp/ansible-tmp-1516786554.85-70695942021302/setup.py && sleep 0'
ok: [localhost]
META: ran handlers

TASK [pouet] ***********************************************************************************************
task path: /tmp/toto/toto.yml:3
fatal: [localhost]: FAILED! => {
    "failed": true, 
    "msg": "Unexpected templating type error occurred on (test {{ item }}): function takes exactly 5 arguments (1 given)"
}
	to retry, use: --limit @/tmp/toto/toto.retry

PLAY RECAP *************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1   

```

The exception here is from the code attempting to instantiate UnicodeError, passing only a string to the constructor. The UnicodeError constructor actually expects five arguments.



Discussion:

Resolving patch:
diff --git a/lib/ansible/template/vars.py b/lib/ansible/template/vars.py
--- a/lib/ansible/template/vars.py
+++ b/lib/ansible/template/vars.py
@@ -23,6 +23,7 @@
 
 from jinja2.utils import missing
 
+from ansible.errors import AnsibleError
 from ansible.module_utils.six import iteritems
 from ansible.module_utils._text import to_native
 
@@ -105,10 +106,10 @@ def __getitem__(self, varname):
             try:
                 value = self._templar.template(variable)
             except Exception as e:
-                try:
-                    raise type(e)(to_native(variable) + ': ' + e.message)
-                except AttributeError:
-                    raise type(e)(to_native(variable) + ': ' + to_native(e))
+                msg = getattr(e, 'message') or to_native(e)
+                raise AnsibleError("An unhandled exception occurred while templating '%s'. "
+                                   "Error was a %s, original message: %s" % (to_native(variable), type(e), msg))
+
             return value
 
     def add_locals(self, locals):


Test patch: