<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">block<br>
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.4.3.0
  config file = /Users/kevinp/Documents/src/cm/ansible/ansible.cfg
  configured module search path = [u'/Users/kevinp/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kevinp/Documents/src/cm/ansible/venv2/lib/python2.7/site-packages/ansible
  executable location = /Users/kevinp/Documents/src/cm/ansible/venv2/bin/ansible
  python version = 2.7.14 (default, Nov 10 2017, 16:16:47) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]"><pre class="notranslate"><code class="notranslate">ansible 2.4.3.0
  config file = /Users/kevinp/Documents/src/cm/ansible/ansible.cfg
  configured module search path = [u'/Users/kevinp/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kevinp/Documents/src/cm/ansible/venv2/lib/python2.7/site-packages/ansible
  executable location = /Users/kevinp/Documents/src/cm/ansible/venv2/bin/ansible
  python version = 2.7.14 (default, Nov 10 2017, 16:16:47) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.38)]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ANSIBLE_PIPELINING(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = True
DEFAULT_HOST_LIST(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = [u'/Users/kevinp/Documents/src/cm/ansible/hosts.py']
DEFAULT_TIMEOUT(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = 30"><pre class="notranslate"><code class="notranslate">ANSIBLE_PIPELINING(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = True
DEFAULT_HOST_LIST(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = [u'/Users/kevinp/Documents/src/cm/ansible/hosts.py']
DEFAULT_TIMEOUT(/Users/kevinp/Documents/src/cm/ansible/ansible.cfg) = 30
</code></pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">macOS control machine, CentOS 7.4 guest</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When using the <code class="notranslate">block</code> task, with an embedded <code class="notranslate">include_role</code> task, it appears that any variables defined within the block don't get propagated to handlers that run as part of the included role.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<ol dir="auto">
<li>Create a task with a block declaration that looks like this:</li>
</ol>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- block:
  - include_role:
      name: my_test_role
  vars:
    my_data: &quot;Sample Number 1&quot;"><pre class="notranslate">- <span class="pl-ent">block</span>:
  - <span class="pl-ent">include_role</span>:
      <span class="pl-ent">name</span>: <span class="pl-s">my_test_role</span>
  <span class="pl-ent">vars</span>:
    <span class="pl-ent">my_data</span>: <span class="pl-s"><span class="pl-pds">"</span>Sample Number 1<span class="pl-pds">"</span></span></pre></div>
<ol start="2" dir="auto">
<li>Create a new role named my_test_role with the following task</li>
</ol>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- debug:
    msg: &quot;my_data in role is {{ my_data }}&quot;
  notify: run my handler
  changed_when: true"><pre class="notranslate">- <span class="pl-ent">debug</span>:
    <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">"</span>my_data in role is {{ my_data }}<span class="pl-pds">"</span></span>
  <span class="pl-ent">notify</span>: <span class="pl-s">run my handler</span>
  <span class="pl-ent">changed_when</span>: <span class="pl-c1">true</span></pre></div>
<ol start="3" dir="auto">
<li>Add a handler to the my_test_role with the following declaration</li>
</ol>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- name: run my handler
  debug: 
    msg: &quot;my_data in handler is {{ my_data }}&quot;"><pre class="notranslate">- <span class="pl-ent">name</span>: <span class="pl-s">run my handler</span>
  <span class="pl-ent">debug</span>: 
    <span class="pl-ent">msg</span>: <span class="pl-s"><span class="pl-pds">"</span>my_data in handler is {{ my_data }}<span class="pl-pds">"</span></span></pre></div>
<ol start="4" dir="auto">
<li>run the task</li>
</ol>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">You should see 2 debug messages being printed, something like as follows:</p>
<blockquote>
<p dir="auto">"my_data in role is Sample Number 1"<br>
"my_data in handler is Sample Number 1"</p>
</blockquote>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">The first debug message gets printed as expected, but the second one - the one from the handler - fails due to an undefined variable, something like this:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK [role : debug] *****************************
changed: [kspjenkins] =&gt; {
    &quot;msg&quot;: &quot;my_data in role is Sample Number 1&quot;
}

RUNNING HANDLER [ : run my handler] **********************************************
FAILED! =&gt; {&quot;msg&quot;: &quot;The task includes an option with an undefined variable....&quot;}`"><pre class="notranslate"><code class="notranslate">TASK [role : debug] *****************************
changed: [kspjenkins] =&gt; {
    "msg": "my_data in role is Sample Number 1"
}

RUNNING HANDLER [ : run my handler] **********************************************
FAILED! =&gt; {"msg": "The task includes an option with an undefined variable...."}`
</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">import_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.6.0 (devel 8743b20c8e) last updated 2018/02/13 17:38:47 (GMT +200)
  config file = /home/nikos/Projects/ansible-demo/ansible.cfg
  configured module search path = ['/home/nikos/Projects/ansible-demo/ansible/lib/modules']
  ansible python module location = /home/nikos/Projects/ansible/lib/ansible
  executable location = /home/nikos/Projects/ansible/bin/ansible
  python version = 3.6.4 (default, Jan  5 2018, 02:35:40) [GCC 7.2.1 20171224]"><pre class="notranslate"><code class="notranslate">ansible 2.6.0 (devel 8743b20c8e) last updated 2018/02/13 17:38:47 (GMT +200)
  config file = /home/nikos/Projects/ansible-demo/ansible.cfg
  configured module search path = ['/home/nikos/Projects/ansible-demo/ansible/lib/modules']
  ansible python module location = /home/nikos/Projects/ansible/lib/ansible
  executable location = /home/nikos/Projects/ansible/bin/ansible
  python version = 3.6.4 (default, Jan  5 2018, 02:35:40) [GCC 7.2.1 20171224]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>

<h5 dir="auto">OS / ENVIRONMENT</h5>

<h5 dir="auto">SUMMARY</h5>
<p dir="auto">I am running import_role against a just-above created instance on google cloud. The role runs fine on the distant host, but the role handlers (just a win_reboot task), runs against my localhost.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">This is the tasks that I use:</p>

<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- gce:
    name: custom-windows-image
    disk_auto_delete: no
    disk_size: &quot;{{ win_server.disk_size }}&quot;
    external_ip: ephemeral
    image_family: windows-2016
    ip_forward: yes
    machine_type: n1-highcpu-4
    metadata: {}
    # TODO change this to 'default'
    network: &quot;{{ project_facts.network.name }}&quot;
    subnetwork: &quot;{{ win_server.subnet }}&quot;
    zone: &quot;{{ win_server.zone }}&quot;
    tags: custom-windows-image
  register: win_instance
- command: &gt;-
    gcloud beta compute reset-windows-password
      --quiet
      --user=&quot;{{ win_server.user }}&quot;
      --zone=&quot;{{ win_server.zone }}&quot;
      custom-windows-image
  register: win_instance_password
  until: win_instance_password.rc != 1
  failed_when: false
  retries: 5
- add_host: &amp;winrm
    hostname: win_instance
    ansible_host: &quot;{{ win_instance.instance_data[0].public_ip }}&quot;
    ansible_connection: winrm
    ansible_port: 5986
    ansible_winrm_server_cert_validation: ignore
    ansible_winrm_transport: ntlm
    ansible_user: &quot;{{ win_server.user }}&quot;
    ansible_password: &quot;{{ win_instance_password.stdout_lines[1] | regex_replace('password:[\\s]*([^s]*)', '\\1') }}&quot;
- gce_firewall:
    name: custom-windows-image
    network: &quot;{{ project_facts.network.name }}&quot;
    allowed: udp:3389;tcp:3389;tcp:5986;icmp
    src_ranges: &quot;{{ project_facts.local.address }}/32&quot;
    target_tags: custom-windows-image
- wait_for_connection:
  delegate_to: custom-windows-image
  vars: *winrm
- import_role:
    name: ansible-windows-setup
    private: yes
  vars:
    windows_setup_hostname: &quot;{{ win_server.name }}&quot;
  delegate_to: win_instance"><pre class="notranslate">- <span class="pl-ent">gce</span>:
    <span class="pl-ent">name</span>: <span class="pl-s">custom-windows-image</span>
    <span class="pl-ent">disk_auto_delete</span>: <span class="pl-s">no</span>
    <span class="pl-ent">disk_size</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_server.disk_size }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">external_ip</span>: <span class="pl-s">ephemeral</span>
    <span class="pl-ent">image_family</span>: <span class="pl-s">windows-2016</span>
    <span class="pl-ent">ip_forward</span>: <span class="pl-s">yes</span>
    <span class="pl-ent">machine_type</span>: <span class="pl-s">n1-highcpu-4</span>
    <span class="pl-ent">metadata</span>: <span class="pl-s">{}</span>
    <span class="pl-c"><span class="pl-c">#</span> TODO change this to 'default'</span>
    <span class="pl-ent">network</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ project_facts.network.name }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">subnetwork</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_server.subnet }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">zone</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_server.zone }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">tags</span>: <span class="pl-s">custom-windows-image</span>
  <span class="pl-ent">register</span>: <span class="pl-s">win_instance</span>
- <span class="pl-ent">command</span>: <span class="pl-s">&gt;-</span>
<span class="pl-s">    gcloud beta compute reset-windows-password</span>
<span class="pl-s">      --quiet</span>
<span class="pl-s">      --user="{{ win_server.user }}"</span>
<span class="pl-s">      --zone="{{ win_server.zone }}"</span>
<span class="pl-s">      custom-windows-image</span>
<span class="pl-s"></span>  <span class="pl-ent">register</span>: <span class="pl-s">win_instance_password</span>
  <span class="pl-ent">until</span>: <span class="pl-s">win_instance_password.rc != 1</span>
  <span class="pl-ent">failed_when</span>: <span class="pl-c1">false</span>
  <span class="pl-ent">retries</span>: <span class="pl-c1">5</span>
- <span class="pl-ent">add_host</span>: <span class="pl-s">&amp;winrm</span>
    <span class="pl-ent">hostname</span>: <span class="pl-s">win_instance</span>
    <span class="pl-ent">ansible_host</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_instance.instance_data[0].public_ip }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">ansible_connection</span>: <span class="pl-s">winrm</span>
    <span class="pl-ent">ansible_port</span>: <span class="pl-c1">5986</span>
    <span class="pl-ent">ansible_winrm_server_cert_validation</span>: <span class="pl-s">ignore</span>
    <span class="pl-ent">ansible_winrm_transport</span>: <span class="pl-s">ntlm</span>
    <span class="pl-ent">ansible_user</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_server.user }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">ansible_password</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_instance_password.stdout_lines[1] | regex_replace('password:[<span class="pl-cce">\\</span>s]*([^s]*)', '<span class="pl-cce">\\</span>1') }}<span class="pl-pds">"</span></span>
- <span class="pl-ent">gce_firewall</span>:
    <span class="pl-ent">name</span>: <span class="pl-s">custom-windows-image</span>
    <span class="pl-ent">network</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ project_facts.network.name }}<span class="pl-pds">"</span></span>
    <span class="pl-ent">allowed</span>: <span class="pl-s">udp:3389;tcp:3389;tcp:5986;icmp</span>
    <span class="pl-ent">src_ranges</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ project_facts.local.address }}/32<span class="pl-pds">"</span></span>
    <span class="pl-ent">target_tags</span>: <span class="pl-s">custom-windows-image</span>
- <span class="pl-ent">wait_for_connection</span>:
  <span class="pl-ent">delegate_to</span>: <span class="pl-s">custom-windows-image</span>
  <span class="pl-ent">vars</span>: <span class="pl-s">*winrm</span>
- <span class="pl-ent">import_role</span>:
    <span class="pl-ent">name</span>: <span class="pl-s">ansible-windows-setup</span>
    <span class="pl-ent">private</span>: <span class="pl-s">yes</span>
  <span class="pl-ent">vars</span>:
    <span class="pl-ent">windows_setup_hostname</span>: <span class="pl-s"><span class="pl-pds">"</span>{{ win_server.name }}<span class="pl-pds">"</span></span>
  <span class="pl-ent">delegate_to</span>: <span class="pl-s">win_instance</span></pre></div>

<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">The role handler should be executed against the windows instance and reboot it.</p>
<h5 dir="auto">ACTUAL RESULTS</h5>


<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="RUNNING HANDLER [ansible-windows-setup : Reboot windows] ************************************
&lt;127.0.0.1&gt; ESTABLISH LOCAL CONNECTION FOR USER: nikos
&lt;127.0.0.1&gt; EXEC (Get-WmiObject -ClassName Win32_OperatingSystem).LastBootUpTime
fatal: [localhost]: FAILED! =&gt; {
    &quot;changed&quot;: false,
    &quot;failed&quot;: true,
    &quot;msg&quot;: &quot;win_reboot: failed to get host uptime info, rc: 1, stdout: b'', stderr: b\&quot;/bin/bash: -c: line 0: syntax error near unexpected token `.LastBootUpTime'\\n/bin/bash: -c: line 0: `(Get-WmiObject -ClassName Win32_OperatingSystem).LastBootUpTime'\\n\&quot;&quot;,
    &quot;reboot&quot;: false
}
META: ran handlers"><pre class="notranslate"><code class="notranslate">RUNNING HANDLER [ansible-windows-setup : Reboot windows] ************************************
&lt;127.0.0.1&gt; ESTABLISH LOCAL CONNECTION FOR USER: nikos
&lt;127.0.0.1&gt; EXEC (Get-WmiObject -ClassName Win32_OperatingSystem).LastBootUpTime
fatal: [localhost]: FAILED! =&gt; {
    "changed": false,
    "failed": true,
    "msg": "win_reboot: failed to get host uptime info, rc: 1, stdout: b'', stderr: b\"/bin/bash: -c: line 0: syntax error near unexpected token `.LastBootUpTime'\\n/bin/bash: -c: line 0: `(Get-WmiObject -ClassName Win32_OperatingSystem).LastBootUpTime'\\n\"",
    "reboot": false
}
META: ran handlers
</code></pre></div>