<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">template plugin</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible-playbook 2.2.1.0 (stable-2.2 941552d107) last updated 2017/01/10 08:55:57 (GMT -400)
  lib/ansible/modules/core: (detached HEAD a9d844c089) last updated 2017/01/10 09:13:05 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 19d20ca1a0) last updated 2017/01/10 09:13:05 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">ansible-playbook 2.2.1.0 (stable-2.2 941552d107) last updated 2017/01/10 08:55:57 (GMT -400)
  lib/ansible/modules/core: (detached HEAD a9d844c089) last updated 2017/01/10 09:13:05 (GMT -400)
  lib/ansible/modules/extras: (detached HEAD 19d20ca1a0) last updated 2017/01/10 09:13:05 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Gentoo linux</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">First of all, sorry, this is an incomplete issue report. I haven't yet properly isolated the problem, but I see that you're approaching a v2.2.1 release and I think it's a release blocker. I'll work on completing the issue soon.</p>
<p dir="auto">jinjia2 recently released its v2.9, but upgrading to jinja2 2.9 causes problems in my provisioning that is fixed by reverting to jinja2 2.8.x</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">None yet, coming soon.</p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">No error</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'undefined variable: 0'
fatal: [sfl-web-django-ci]: FAILED! =&gt; {&quot;failed&quot;: true, &quot;msg&quot;: &quot;Unexpected failure during module execution.&quot;, &quot;stdout&quot;: &quot;&quot;}
"><pre class="notranslate"><code class="notranslate">An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'undefined variable: 0'
fatal: [sfl-web-django-ci]: FAILED! =&gt; {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

</code></pre></div>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">Template</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.0 (devel 0aad46c85b) last updated 2017/01/09 16:11:21 (GMT -400)
  config file = /home/jadams/.ansible.cfg
  configured module search path = Default w/o overrides
"><pre class="notranslate"><code class="notranslate">ansible 2.3.0 (devel 0aad46c85b) last updated 2017/01/09 16:11:21 (GMT -400)
  config file = /home/jadams/.ansible.cfg
  configured module search path = Default w/o overrides

</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Tested on vanilla / no extra configuration</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">N/A but tested from Fedora 25</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When Jinja2 dependency is &gt;= 2.9.0, nested templates inside of loops defined in a parent template do not receive the local scope. For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{% for item in foo %}
    {% include &quot;other_template.j2&quot; %}
{% endfor %"><pre class="notranslate"><code class="notranslate">{% for item in foo %}
    {% include "other_template.j2" %}
{% endfor %
</code></pre></div>
<p dir="auto">In the above example, other_template.j2 would not have the item variable defined.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# Example Playbook
- hosts: localhost
  connection: local
  vars:
    foo:
      - bar
      - baz
  tasks:
    - name: Test Template
      template: src=parent.j2 dest=/tmp/template.out"><pre class="notranslate"><span class="pl-c"><span class="pl-c">#</span> Example Playbook</span>
- <span class="pl-ent">hosts</span>: <span class="pl-s">localhost</span>
  <span class="pl-ent">connection</span>: <span class="pl-s">local</span>
  <span class="pl-ent">vars</span>:
    <span class="pl-ent">foo</span>:
      - <span class="pl-s">bar</span>
      - <span class="pl-s">baz</span>
  <span class="pl-ent">tasks</span>:
    - <span class="pl-ent">name</span>: <span class="pl-s">Test Template</span>
      <span class="pl-ent">template</span>: <span class="pl-s">src=parent.j2 dest=/tmp/template.out</span></pre></div>
<div class="highlight highlight-text-html-django notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# parent.j2
{% for item in foo %}
    {% include &quot;subtemplate.j2&quot; %}
{% endfor %}
"><pre class="notranslate"># parent.j2
<span class="pl-e">{%</span> <span class="pl-k">for</span> <span class="pl-s">item</span> <span class="pl-k">in</span> <span class="pl-s">foo</span> <span class="pl-e">%}</span>
    <span class="pl-e">{%</span> <span class="pl-k">include</span> <span class="pl-s">"subtemplate.j2"</span> <span class="pl-e">%}</span>
<span class="pl-e">{%</span> <span class="pl-k">endfor</span> <span class="pl-e">%}</span>
</pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#subtemplate.j2
{{ item }}"><pre class="notranslate"><code class="notranslate">#subtemplate.j2
{{ item }}
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Expected the output of the following file:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="bar
baz"><pre class="notranslate"><code class="notranslate">bar
baz
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [Test Template] ************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! =&gt; {&quot;changed&quot;: false, &quot;failed&quot;: true, &quot;msg&quot;: &quot;AnsibleUndefinedVariable: 'item' is undefined&quot;}"><pre class="notranslate"><code class="notranslate">TASK [Test Template] ************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! =&gt; {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'item' is undefined"}
</code></pre></div>
<p dir="auto">I believe this is because of the Jinja2 Context changes mentioned <a href="http://jinja.pocoo.org/docs/2.9/changelog/#version-2-9" rel="nofollow">here</a> in combination with Ansible overriding default Jinja2 functions. I wasn't able to track down the root cause in my limited testing.</p>