<p dir="auto">When incorrectly passing a keyword to a variable, the error message is confusing:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fatal: [flexo.kormoc.com] =&gt; Traceback (most recent call last):
  File &quot;/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py&quot;, line 421, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File &quot;/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py&quot;, line 512, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File &quot;/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py&quot;, line 706, in _executor_internal_inner
    module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
  File &quot;/Users/kormoc/Projects/Ansible/ansible/lib/ansible/utils/template.py&quot;, line 319, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File &quot;/Users/kormoc/Projects/Ansible/ansible/lib/ansible/utils/template.py&quot;, line 551, in template_from_string
    return res
UnboundLocalError: local variable 'res' referenced before assignment"><pre class="notranslate"><code class="notranslate">fatal: [flexo.kormoc.com] =&gt; Traceback (most recent call last):
  File "/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py", line 421, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py", line 512, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/Users/kormoc/Projects/Ansible/ansible/lib/ansible/runner/__init__.py", line 706, in _executor_internal_inner
    module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
  File "/Users/kormoc/Projects/Ansible/ansible/lib/ansible/utils/template.py", line 319, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File "/Users/kormoc/Projects/Ansible/ansible/lib/ansible/utils/template.py", line 551, in template_from_string
    return res
UnboundLocalError: local variable 'res' referenced before assignment
</code></pre></div>
<p dir="auto">This is due to the error checking being:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="        try:
            res = jinja2.utils.concat(rf)
        except TypeError, te:
            if 'StrictUndefined' in str(te):
                raise errors.AnsibleUndefinedVariable(&quot;unable to look up a name or access an attribute in template string&quot;)
        return res"><pre class="notranslate"><code class="notranslate">        try:
            res = jinja2.utils.concat(rf)
        except TypeError, te:
            if 'StrictUndefined' in str(te):
                raise errors.AnsibleUndefinedVariable("unable to look up a name or access an attribute in template string")
        return res
</code></pre></div>
<p dir="auto">There should be a else condition to that if statement that at least outputs str(te) rather then falling though to the return res.</p>
<p dir="auto">The following playbook generates a traceback when run on the devel branch:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: test
  hosts: all
  gather_facts: false
  vars:
    - a_list:
      - a_value
  tasks:
    - name: I meant to concat a string with an array value
      debug: msg=&quot;{{ 'foo =&gt; ' + a_list[0] }}&quot;  

    - name: But I accidentally concat a string with array
      debug: msg=&quot;{{ 'foo =&gt; ' + a_list }}&quot;"><pre class="notranslate"><code class="notranslate">- name: test
  hosts: all
  gather_facts: false
  vars:
    - a_list:
      - a_value
  tasks:
    - name: I meant to concat a string with an array value
      debug: msg="{{ 'foo =&gt; ' + a_list[0] }}"  

    - name: But I accidentally concat a string with array
      debug: msg="{{ 'foo =&gt; ' + a_list }}"
</code></pre></div>
<p dir="auto">The stacktrace generated:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fatal: [rizo] =&gt; Traceback (most recent call last):
  File &quot;/srv/ansible-source/lib/ansible/runner/__init__.py&quot;, line 418, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File &quot;/srv/ansible-source/lib/ansible/runner/__init__.py&quot;, line 509, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File &quot;/srv/ansible-source/lib/ansible/runner/__init__.py&quot;, line 703, in _executor_internal_inner
    module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
  File &quot;/srv/ansible-source/lib/ansible/utils/template.py&quot;, line 319, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File &quot;/srv/ansible-source/lib/ansible/utils/template.py&quot;, line 550, in template_from_string
    return res
UnboundLocalError: local variable 'res' referenced before assignment"><pre class="notranslate"><code class="notranslate">fatal: [rizo] =&gt; Traceback (most recent call last):
  File "/srv/ansible-source/lib/ansible/runner/__init__.py", line 418, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/srv/ansible-source/lib/ansible/runner/__init__.py", line 509, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/srv/ansible-source/lib/ansible/runner/__init__.py", line 703, in _executor_internal_inner
    module_args = template.template(self.basedir, module_args, inject, fail_on_undefined=self.error_on_undefined_vars)
  File "/srv/ansible-source/lib/ansible/utils/template.py", line 319, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File "/srv/ansible-source/lib/ansible/utils/template.py", line 550, in template_from_string
    return res
UnboundLocalError: local variable 'res' referenced before assignment
</code></pre></div>
<p dir="auto">I gather that relates to a bug in the error trapping code rather than the jinja2 error.</p>
<p dir="auto">The above minimal example was derived from a much more complex playbook.<br>
It took me a while to figure out that the problem was with the data for a particular host, given that the playbook worked on other hosts :-(</p>