<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">include_role module</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.4.0.0
  config file = /home/esio/work/ansible/ansible.cfg
  configured module search path = [u'/home/esio/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Sep  5 2017, 08:53:59) [GCC 7.1.1 20170622 (Red Hat 7.1.1-3)]
"><pre class="notranslate"><code class="notranslate">ansible 2.4.0.0
  config file = /home/esio/work/ansible/ansible.cfg
  configured module search path = [u'/home/esio/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Sep  5 2017, 08:53:59) [GCC 7.1.1 20170622 (Red Hat 7.1.1-3)]

</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ANSIBLE_NOCOWS(/home/esio/work/ansible/ansible.cfg) = True
DEFAULT_BECOME_EXE(/home/esio/work/ansible/ansible.cfg) = sudo su -
DEFAULT_BECOME_METHOD(/home/esio/work/ansible/ansible.cfg) = su
DEFAULT_HASH_BEHAVIOUR(/home/esio/work/ansible/ansible.cfg) = merge
DEFAULT_HOST_LIST(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/inventories/test/hosts']
DEFAULT_REMOTE_TMP(/home/esio/work/ansible/ansible.cfg) = /tmp/.ansible-${USER}/tmp # workaround become permission issues
DEFAULT_ROLES_PATH(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/roles', u'/home/esio/work/ansible/vendor']
HOST_KEY_CHECKING(/home/esio/work/ansible/ansible.cfg) = False
RETRY_FILES_SAVE_PATH(/home/esio/work/ansible/ansible.cfg) = /home/esio/work/ansible/.retryfiles"><pre class="notranslate"><code class="notranslate">ANSIBLE_NOCOWS(/home/esio/work/ansible/ansible.cfg) = True
DEFAULT_BECOME_EXE(/home/esio/work/ansible/ansible.cfg) = sudo su -
DEFAULT_BECOME_METHOD(/home/esio/work/ansible/ansible.cfg) = su
DEFAULT_HASH_BEHAVIOUR(/home/esio/work/ansible/ansible.cfg) = merge
DEFAULT_HOST_LIST(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/inventories/test/hosts']
DEFAULT_REMOTE_TMP(/home/esio/work/ansible/ansible.cfg) = /tmp/.ansible-${USER}/tmp # workaround become permission issues
DEFAULT_ROLES_PATH(/home/esio/work/ansible/ansible.cfg) = [u'/home/esio/work/ansible/roles', u'/home/esio/work/ansible/vendor']
HOST_KEY_CHECKING(/home/esio/work/ansible/ansible.cfg) = False
RETRY_FILES_SAVE_PATH(/home/esio/work/ansible/ansible.cfg) = /home/esio/work/ansible/.retryfiles
</code></pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Fedora 26 x86_64</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When I use include_role ansible doesn't apply default variables.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">I have directories and files in my ansible config:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="└── roles
    ├── r1
    │   └── tasks
    │       └── main.yml
    └── r2
        ├── defaults
        │   └── main.yml
        └── tasks
            └── main.yml"><pre class="notranslate"><code class="notranslate">└── roles
    ├── r1
    │   └── tasks
    │       └── main.yml
    └── r2
        ├── defaults
        │   └── main.yml
        └── tasks
            └── main.yml
</code></pre></div>
<p dir="auto">In roles/r1/tasks/main.yml</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- name: Run role r2
  include_role:
    name: gpdw.deploy-hdfs-component
  vars:
    artifact_version: &quot;{{ x[2] }}&quot;
    artifact_id: &quot;{{ x[1] }}&quot;
  with_list: &quot;{{ list }}&quot;
  loop_control:
    loop_var: x"><pre class="notranslate">- <span class="pl-ent">name</span>: <span class="pl-s">Run role r2</span>
  <span class="pl-ent">include_role</span>:
    <span class="pl-ent">name</span>: <span class="pl-s">gpdw.deploy-hdfs-component</span>
  <span class="pl-ent">vars</span>:
    <span class="pl-ent">artifact_version</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ x[2] }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">artifact_id</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ x[1] }}<span class="pl-pds">"</span></span>
  <span class="pl-ent">with_list</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ list }}<span class="pl-pds">"</span></span>
  <span class="pl-ent">loop_control</span>:
    <span class="pl-ent">loop_var</span>: <span class="pl-s">x</span></pre></div>
<p dir="auto">In roles/r2/tasks/main.yml</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- name: debug
  debug:
    msg: &quot;{{ artifact_extension }}"><pre class="notranslate">- <span class="pl-ent">name</span>: <span class="pl-s">debug</span>
  <span class="pl-ent">debug</span>:
    <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ artifact_extension }}</span></pre></div>
<p dir="auto">In roles/r2/defaults/main.yml</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="artifact_extension: &quot;tar.gz&quot;"><pre class="notranslate"><span class="pl-ent">artifact_extension</span>: <span class="pl-s"><span class="pl-pds">"</span>tar.gz<span class="pl-pds">"</span></span></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">It should work and print tar.gz as debug.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">Ansible fails with error, that variable artifact_extension is undefined. In my opinion ansible should use variables from defaults/main.yml file in included role.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fatal: [ap-hdpen1t]: FAILED! =&gt; {
    &quot;msg&quot;: &quot;The task includes an option with an undefined variable. The error was: 'artifact_extension' is undefined\n\nThe error appears to have been in '/home/esio/work/ansible/roles/gpdw.deploy-dq-all/tasks/main.yml': line 29, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug\n  ^ here\n\nexception type: &lt;class 'ansible.errors.AnsibleUndefinedVariable'&gt;\nexception: 'artifact_extension' is undefined&quot;
}"><pre class="notranslate"><code class="notranslate">fatal: [ap-hdpen1t]: FAILED! =&gt; {
    "msg": "The task includes an option with an undefined variable. The error was: 'artifact_extension' is undefined\n\nThe error appears to have been in '/home/esio/work/ansible/roles/gpdw.deploy-dq-all/tasks/main.yml': line 29, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: debug\n  ^ here\n\nexception type: &lt;class 'ansible.errors.AnsibleUndefinedVariable'&gt;\nexception: 'artifact_extension' is undefined"
}
</code></pre></div>
<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Feature Idea</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">include_role</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">ansible 2.2.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">None.</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">OS: CentOS7, but shouldn't matter.</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When including a role using include_role its vars do not become available to the rest of the playbook. This does work when using the regular roles definition.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">A minimal test-case lives here: <a href="https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/bad-playbook.yml">https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/bad-playbook.yml</a> <a href="https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/roles/testrole/vars/main.yml">https://github.com/wouterhund/demo-ansible-include_role-bug/blob/master/roles/testrole/vars/main.yml</a></p>
<p dir="auto">The actual code that triggers the issue:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---
- hosts: localhost

  tasks:
    - include_role:
        name: testrole
    - debug: msg=&quot;{{some_var}}&quot;"><pre class="notranslate">---
- <span class="pl-ent">hosts</span>: <span class="pl-s">localhost</span>

  <span class="pl-ent">tasks</span>:
    - <span class="pl-ent">include_role</span>:
        <span class="pl-ent">name</span>: <span class="pl-s">testrole</span>
    - <span class="pl-ent">debug</span>: <span class="pl-s">msg="{{some_var}}"</span></pre></div>
<p dir="auto"><code class="notranslate">roles/testrole/vars/main.yml</code></p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---
some_var: &quot;Hello world!&quot;"><pre class="notranslate">---
<span class="pl-ent">some_var</span>: <span class="pl-s"><span class="pl-pds">"</span>Hello world!<span class="pl-pds">"</span></span></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">I would expect <code class="notranslate">good-playbook.yml</code> and <code class="notranslate">bad-playbook.yml</code> from the linked github to work identically, however <code class="notranslate">bad-playbook.yml</code> fails. I expect variables defined within an included role to be made available to the rest of the playbook.</p>
<p dir="auto">For the example above I'd expect it to pass and display "Hello world!"</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="» ansible-playbook -vvvv bad-playbook.yml                                                                                                                1 ↵
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available

Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: bad-playbook.yml *****************************************************
1 plays in bad-playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py
&lt;127.0.0.1&gt; ESTABLISH LOCAL CONNECTION FOR USER: vagrant
&lt;127.0.0.1&gt; EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p &quot;` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `&quot; &amp;&amp; echo ansible-tmp-1487936470.27-110880382737009=&quot;` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `&quot; ) &amp;&amp; sleep 0'
&lt;127.0.0.1&gt; PUT /tmp/tmp017eF_ TO /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py
&lt;127.0.0.1&gt; EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/ /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py &amp;&amp; sleep 0'
&lt;127.0.0.1&gt; EXEC /bin/sh -c '/usr/bin/python2 /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py; rm -rf &quot;/home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/&quot; &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'
ok: [localhost]

TASK [testrole : debug] ********************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/roles/testrole/tasks/main.yml:2
ok: [localhost] =&gt; {
    &quot;msg&quot;: &quot;Hello world!&quot;
}

TASK [debug] *******************************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml:7
fatal: [localhost]: FAILED! =&gt; {
    &quot;failed&quot;: true, 
    &quot;msg&quot;: &quot;the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'some_var' is undefined\n\nThe error appears to have been in '/home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        name: testrole\n    - debug: msg=\&quot;{{some_var}}\&quot;\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \&quot;{{ foo }}\&quot;\n&quot;
}
	to retry, use: --limit @/home/vagrant/demo-ansible-include_role-bug/bad-playbook.retry

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1   "><pre class="notranslate"><code class="notranslate">» ansible-playbook -vvvv bad-playbook.yml                                                                                                                1 ↵
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available

Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: bad-playbook.yml *****************************************************
1 plays in bad-playbook.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py
&lt;127.0.0.1&gt; ESTABLISH LOCAL CONNECTION FOR USER: vagrant
&lt;127.0.0.1&gt; EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `" &amp;&amp; echo ansible-tmp-1487936470.27-110880382737009="` echo ~/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009 `" ) &amp;&amp; sleep 0'
&lt;127.0.0.1&gt; PUT /tmp/tmp017eF_ TO /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py
&lt;127.0.0.1&gt; EXEC /bin/sh -c 'chmod u+x /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/ /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py &amp;&amp; sleep 0'
&lt;127.0.0.1&gt; EXEC /bin/sh -c '/usr/bin/python2 /home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/setup.py; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1487936470.27-110880382737009/" &gt; /dev/null 2&gt;&amp;1 &amp;&amp; sleep 0'
ok: [localhost]

TASK [testrole : debug] ********************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/roles/testrole/tasks/main.yml:2
ok: [localhost] =&gt; {
    "msg": "Hello world!"
}

TASK [debug] *******************************************************************
task path: /home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml:7
fatal: [localhost]: FAILED! =&gt; {
    "failed": true, 
    "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'some_var' is undefined\n\nThe error appears to have been in '/home/vagrant/demo-ansible-include_role-bug/bad-playbook.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        name: testrole\n    - debug: msg=\"{{some_var}}\"\n      ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes.  Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"
}
	to retry, use: --limit @/home/vagrant/demo-ansible-include_role-bug/bad-playbook.retry

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