<h5 dir="auto">ISSUE TYPE</h5>
<p dir="auto">[x] - Bug Report</p>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">Playbook &amp; Inventory</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.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 18 2016, 07:03:39) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]"><pre class="notranslate"><code class="notranslate">ansible 2.3.0.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 18 2016, 07:03:39) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
</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">Inventory:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[slave]
10.0.0.1:1222

[master]
10.0.0.1:9322"><pre class="notranslate"><code class="notranslate">[slave]
10.0.0.1:1222

[master]
10.0.0.1:9322
</code></pre></div>
<p dir="auto">Playbook:</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- hosts: master
  tasks:
    - name: Display all variables/facts known for a host
      debug:
        var: hostvars[inventory_hostname]
        verbosity: 4"><pre class="notranslate">- <span class="pl-ent">hosts</span>: <span class="pl-s">master</span>
  <span class="pl-ent">tasks</span>:
    - <span class="pl-ent">name</span>: <span class="pl-s">Display all variables/facts known for a host</span>
      <span class="pl-ent">debug</span>:
        <span class="pl-ent">var</span>: <span class="pl-s">hostvars[inventory_hostname]</span>
        <span class="pl-ent">verbosity</span>: <span class="pl-c1">4</span></pre></div>
<p dir="auto">Ansible connects to port <code class="notranslate">1222</code> instead of <code class="notranslate">9322</code> as expected.</p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Ansible to connect to port <code class="notranslate">9322</code> as per the host group</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 -i inventory playbook.yml
Loading callback plugin default of type stdout, v2.0 from /usr/local/Cellar/ansible/2.3.0.0_2/libexec/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

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

PLAY [master] ******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
Using module file /usr/local/Cellar/ansible/2.3.0.0_2/libexec/lib/python2.7/site-packages/ansible/modules/system/setup.py
&lt;10.0.0.1&gt; ESTABLISH CONNECTION FOR USER: root on PORT 1222 TO 10.0.0.1
&lt;10.0.0.1&gt; CONNECTION: pid 83121 waiting for lock on 8
&lt;10.0.0.1&gt; CONNECTION: pid 83121 acquired lock on 8"><pre class="notranslate"><code class="notranslate">$ ansible-playbook -vvvv -i inventory playbook.yml
Loading callback plugin default of type stdout, v2.0 from /usr/local/Cellar/ansible/2.3.0.0_2/libexec/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

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

PLAY [master] ******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
Using module file /usr/local/Cellar/ansible/2.3.0.0_2/libexec/lib/python2.7/site-packages/ansible/modules/system/setup.py
&lt;10.0.0.1&gt; ESTABLISH CONNECTION FOR USER: root on PORT 1222 TO 10.0.0.1
&lt;10.0.0.1&gt; CONNECTION: pid 83121 waiting for lock on 8
&lt;10.0.0.1&gt; CONNECTION: pid 83121 acquired lock on 8
</code></pre></div>
<ul dir="auto">
<li>hosts: S6_scanner_dc1<br>
tasks:
<ul dir="auto">
<li>set_fact: datacenter={{ dc }}</li>
</ul>
</li>
<li>hosts: S6_scanner_{{ datacenter }}<br>
tasks:
<ul dir="auto">
<li>shell: echo {{ datacenter }}</li>
</ul>
</li>
</ul>
<p dir="auto">In this playbook, the variable "datacenter" is well substituted in the shell command but it is not in the hosts declaration of the 2nd play.</p>
<p dir="auto">Ansible version used : 1.2</p>
<p dir="auto">If I pass the variable as an extra_vars in ansible-playbook command, it works fine.<br>
ansible-playbook myplaybook.yml -e "datacenter=dc1"</p>
<ul dir="auto">
<li>hosts: S6_scanner_{{ datacenter }}<br>
tasks:
<ul dir="auto">
<li>shell: echo {{ datacenter }}</li>
</ul>
</li>
</ul>
<p dir="auto">The best use case would be that it works between plays but also even if we include playbooks. For ex:</p>
<ul dir="auto">
<li>hosts: S6_scanner_dc1<br>
tasks:
<ul dir="auto">
<li>set_fact: datacenter={{ dc }}</li>
</ul>
</li>
<li>include anotherplaybook.yml =&gt; could reuse var in this playbook</li>
</ul>