<p dir="auto">The following combination of <code class="notranslate">include_role</code> and <code class="notranslate">when</code> condition in one of my roles results in the error message posted below.</p>
<p dir="auto"><code class="notranslate">myrole/tasks/main.yml</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" ---
 - include_role: name=mydebug
   when: project == &quot;some-project&quot;"><pre class="notranslate"><code class="notranslate"> ---
 - include_role: name=mydebug
   when: project == "some-project"
</code></pre></div>
<p dir="auto">Error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fatal: [localhost]: FAILED! =&gt; {&quot;failed&quot;: true, &quot;msg&quot;: &quot;The conditional check 'p' failed. The error was: error while evaluating conditional (p): 'p' is undefined\n\nThe error appears to have been in '/opt/ansible/roles/proj-conf/tasks/main.yml': line 2, 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- include_role: name=mydebug\n  ^ here\n&quot;}"><pre class="notranslate"><code class="notranslate">fatal: [localhost]: FAILED! =&gt; {"failed": true, "msg": "The conditional check 'p' failed. The error was: error while evaluating conditional (p): 'p' is undefined\n\nThe error appears to have been in '/opt/ansible/roles/proj-conf/tasks/main.yml': line 2, 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- include_role: name=mydebug\n  ^ here\n"}
</code></pre></div>
<p dir="auto">Without the condition, the role runs fine. Any clue what's causing this behavior?</p>
<p dir="auto">For information:<br>
ansible 2.2.0.0</p>
<p dir="auto">The <code class="notranslate">mydebug</code> role is a simple wrapper around the predefined <code class="notranslate">debug</code> role.</p>
<p dir="auto"><code class="notranslate">mydebug/tasks/main.yml</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---
- debug:
  msg: &quot;test&quot;"><pre class="notranslate"><code class="notranslate">---
- debug:
  msg: "test"
</code></pre></div>
<p dir="auto">My playbook:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="---
- hosts: '{{ target }}'
  roles:
    - { role: myrole, project: '{{ project }}'}"><pre class="notranslate"><code class="notranslate">---
- hosts: '{{ target }}'
  roles:
    - { role: myrole, project: '{{ project }}'}
</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">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.0.0
  config file =
  configured module search path = Default w/o overrides"><pre class="notranslate"><code class="notranslate">ansible 2.2.0.0
  config file =
  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">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">include_role: inside a block: does not work with when: crashes when starting to execute the included role.<br>
If I remove the external when: clause, everything works fine.</p>
<p dir="auto">(this is a re-submition of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="194653202" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/19085" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/19085/hovercard" href="https://github.com/ansible/ansible/issues/19085">#19085</a> which was incorrectly closed)</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">calling script:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- block:
    - include_role:
          name: &quot;ansible-lib--infra/v1/ez123--moodle-moosh&quot;
          static:  no
          private: yes
      when: (zzz_moodle__skip__moosh is not defined) or (not (zzz_moodle__skip__moosh |bool))"><pre class="notranslate"><code class="notranslate">- block:
    - include_role:
          name: "ansible-lib--infra/v1/ez123--moodle-moosh"
          static:  no
          private: yes
      when: (zzz_moodle__skip__moosh is not defined) or (not (zzz_moodle__skip__moosh |bool))
</code></pre></div>
<p dir="auto">on the included role, under tasks/main.yml</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- name: &quot;check if moosh is already installed (1)&quot;
  command: &quot;./moosh --help&quot;
  args:
    chdir: &quot;{{ ez123_moosh__bin_dir }}&quot;
  register: _X_moosh_ver_0
  changed_when: false
  failed_when: false"><pre class="notranslate"><code class="notranslate">- name: "check if moosh is already installed (1)"
  command: "./moosh --help"
  args:
    chdir: "{{ ez123_moosh__bin_dir }}"
  register: _X_moosh_ver_0
  changed_when: false
  failed_when: false
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">There should be no error/crash.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [ansible-lib--infra/v1/ez123--moodle-moosh : check if moosh is already installed (1)] ***
task path: /etc/ansible/roles.glx/ansible-lib--infra/v1/ez123--moodle-moosh/tasks/main.yml:4
fatal: [host000000]: FAILED! =&gt; {
    &quot;failed&quot;: true,
    &quot;msg&quot;: &quot;The conditional check '{' failed. The error was: template error while templating string: unexpected '%'. String: {% if ( %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/opt/src/ansible/dom/roles/dom-roles/func/tasks/main.yml': line 4, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- block:\n    - include_role:\n      ^ here\n&quot;
}```"><pre class="notranslate"><code class="notranslate">TASK [ansible-lib--infra/v1/ez123--moodle-moosh : check if moosh is already installed (1)] ***
task path: /etc/ansible/roles.glx/ansible-lib--infra/v1/ez123--moodle-moosh/tasks/main.yml:4
fatal: [host000000]: FAILED! =&gt; {
    "failed": true,
    "msg": "The conditional check '{' failed. The error was: template error while templating string: unexpected '%'. String: {% if ( %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/opt/src/ansible/dom/roles/dom-roles/func/tasks/main.yml': line 4, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n- block:\n    - include_role:\n      ^ here\n"
}```
</code></pre></div>