<h5 dir="auto">ISSUE TYPE</h5>

<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto"><code class="notranslate">include</code> task</p>
<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[vagrant@localhost ~]$ ansible --version
ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">[vagrant@localhost ~]$ ansible --version
ansible 2.1.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>

<p dir="auto">No changes</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>

<p dir="auto">Centos 7.2</p>
<h5 dir="auto">SUMMARY</h5>

<p dir="auto">the nested include task fails with <code class="notranslate">"'item' is undefined"</code> error when outer include task uses a loop</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>

<ol dir="auto">
<li>create a directory structure and place yml files as described in the gist below</li>
<li>run the ansible-playbook to hit the error</li>
</ol>


<p dir="auto"><a href="https://gist.github.com/mapuri/b4b74e81c531eedb834c5ba81cde449f">https://gist.github.com/mapuri/b4b74e81c531eedb834c5ba81cde449f</a></p>
<h5 dir="auto">EXPECTED RESULTS</h5>

<p dir="auto">There shall be no error and the nested includes should work. This was working with ansible 2.1.0.0</p>
<h5 dir="auto">ACTUAL RESULTS</h5>


<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[vagrant@localhost ~]$ ansible-playbook -i ./hosts ./tmp/site.yml -kK
SSH password:
SUDO password[defaults to SSH password]:

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [include] *****************************************************************
fatal: [localhost]: FAILED! =&gt; {&quot;failed&quot;: true, &quot;reason&quot;: &quot;'item' is undefined&quot;}

NO MORE HOSTS LEFT *************************************************************
        to retry, use: --limit @./tmp/site.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1"><pre class="notranslate"><code class="notranslate">[vagrant@localhost ~]$ ansible-playbook -i ./hosts ./tmp/site.yml -kK
SSH password:
SUDO password[defaults to SSH password]:

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [include] *****************************************************************
fatal: [localhost]: FAILED! =&gt; {"failed": true, "reason": "'item' is undefined"}

NO MORE HOSTS LEFT *************************************************************
        to retry, use: --limit @./tmp/site.retry

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1
</code></pre></div>
<h5 dir="auto">ISSUE TYPE</h5>

<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">ANSIBLE VERSION</h5>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.1.1.0"><pre class="notranslate"><code class="notranslate">ansible 2.1.1.0
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">None</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Variables do not resolve and error out as undefined when using <em>include</em> statements in tasks. When you replace the below code for /duck/main.yml with anything but 'include' statements, it works fine. I have verified that this is not an issue in 2.1.0.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>

<p dir="auto">tasks/main.yml</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
---

  - set_fact:
      folder_name: 'duck'

  - debug:
      var: folder_name

  - include: &quot;{{ folder_name }}/main.yml&quot;
"><pre class="notranslate"><code class="notranslate">
---

  - set_fact:
      folder_name: 'duck'

  - debug:
      var: folder_name

  - include: "{{ folder_name }}/main.yml"

</code></pre></div>
<p dir="auto">tasks/duck/main.yml</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
---
  - include: test.yml"><pre class="notranslate"><code class="notranslate">
---
  - include: test.yml
</code></pre></div>
<p dir="auto">tasks/duck/test.yml</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
---
  - debug:
      msg: &quot;I made it here!&quot;"><pre class="notranslate"><code class="notranslate">
---
  - debug:
      msg: "I made it here!"
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAY [Set Facts Issue] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [foo : set_fact] **********************************************************
task path: ansible/roles/foo/tasks/main.yml:3
ok: [localhost] =&gt; {&quot;ansible_facts&quot;: {&quot;folder_name&quot;: &quot;duck&quot;}, &quot;changed&quot;: false, &quot;invocation&quot;: {&quot;module_args&quot;: {&quot;folder_name&quot;: &quot;duck&quot;}, &quot;module_name&quot;: &quot;set_fact&quot;}}

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/main.yml:6
ok: [localhost] =&gt; {
    &quot;folder_name&quot;: &quot;duck&quot;
}

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/main.yml:8
included: ansible/roles/foo/tasks/duck/main.yml for localhost

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/duck/main.yml:2
included: ansible/roles/foo/tasks/duck/test.yml for localhost

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/duck/test.yml:2
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;I made it here!&quot;
}

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0"><pre class="notranslate"><code class="notranslate">PLAY [Set Facts Issue] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [foo : set_fact] **********************************************************
task path: ansible/roles/foo/tasks/main.yml:3
ok: [localhost] =&gt; {"ansible_facts": {"folder_name": "duck"}, "changed": false, "invocation": {"module_args": {"folder_name": "duck"}, "module_name": "set_fact"}}

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/main.yml:6
ok: [localhost] =&gt; {
    "folder_name": "duck"
}

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/main.yml:8
included: ansible/roles/foo/tasks/duck/main.yml for localhost

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/duck/main.yml:2
included: ansible/roles/foo/tasks/duck/test.yml for localhost

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/duck/test.yml:2
ok: [localhost] =&gt; {
    "msg": "I made it here!"
}

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAY [Set Facts Issue] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [foo : set_fact] **********************************************************
task path: ansible/roles/foo/tasks/main.yml:3
ok: [localhost] =&gt; {&quot;ansible_facts&quot;: {&quot;folder_name&quot;: &quot;duck&quot;}, &quot;changed&quot;: false, &quot;invocation&quot;: {&quot;module_args&quot;: {&quot;folder_name&quot;: &quot;duck&quot;}, &quot;module_name&quot;: &quot;set_fact&quot;}}

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/main.yml:6
ok: [localhost] =&gt; {
    &quot;folder_name&quot;: &quot;duck&quot;
}

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/main.yml:8
fatal: [localhost]: FAILED! =&gt; {&quot;failed&quot;: true, &quot;reason&quot;: &quot;'folder_name' is undefined&quot;}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1"><pre class="notranslate"><code class="notranslate">PLAY [Set Facts Issue] *********************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [foo : set_fact] **********************************************************
task path: ansible/roles/foo/tasks/main.yml:3
ok: [localhost] =&gt; {"ansible_facts": {"folder_name": "duck"}, "changed": false, "invocation": {"module_args": {"folder_name": "duck"}, "module_name": "set_fact"}}

TASK [foo : debug] *************************************************************
task path: ansible/roles/foo/tasks/main.yml:6
ok: [localhost] =&gt; {
    "folder_name": "duck"
}

TASK [foo : include] ***********************************************************
task path: ansible/roles/foo/tasks/main.yml:8
fatal: [localhost]: FAILED! =&gt; {"failed": true, "reason": "'folder_name' is undefined"}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1
</code></pre></div>