<h5 dir="auto">Issue Type:</h5>
<ul dir="auto">
<li>Bug Report</li>
<li>Bugfix Pull Request</li>
</ul>
<h5 dir="auto">Ansible Version:</h5>
<ul dir="auto">
<li>1.9.4</li>
</ul>
<h5 dir="auto">Ansible Configuration:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[defaults]
inventory=inventory"><pre class="notranslate"><code class="notranslate">[defaults]
inventory=inventory
</code></pre></div>
<h5 dir="auto">Environment:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="./inventory/g
./host_vars/h1
./host_vars/h2
./playbook.yml"><pre class="notranslate"><code class="notranslate">./inventory/g
./host_vars/h1
./host_vars/h2
./playbook.yml
</code></pre></div>
<h5 dir="auto">Summary:</h5>
<p dir="auto">host_vars are not loaded when using inventory directory and --limit flag</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#./inventory/g
h1
h2"><pre class="notranslate"><code class="notranslate">#./inventory/g
h1
h2
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#./host_vars/h1

---
key: h1"><pre class="notranslate"><code class="notranslate">#./host_vars/h1

---
key: h1
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#./host_vars/h2

---
key: h2"><pre class="notranslate"><code class="notranslate">#./host_vars/h2

---
key: h2
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#./playbook.yml

---

- name: test host_vars includes
  gather_facts: no
  hosts: all
  tasks:
    - debug: var=hostvars['h1'].key
    - debug: var=hostvars['h2'].key"><pre class="notranslate"><code class="notranslate">#./playbook.yml

---

- name: test host_vars includes
  gather_facts: no
  hosts: all
  tasks:
    - debug: var=hostvars['h1'].key
    - debug: var=hostvars['h2'].key
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible-playbook playbook.yml -l h1"><pre class="notranslate"><code class="notranslate">ansible-playbook playbook.yml -l h1
</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 [test host_vars includes] ************************************************

TASK: [debug var=hostvars['h1'].key] ******************************************
ok: [h1] =&gt; {
    &quot;var&quot;: {
        &quot;hostvars['h1'].key&quot;: &quot;h1&quot;
    }
}

TASK: [debug var=hostvars['h2'].key] ******************************************
ok: [h1] =&gt; {
    &quot;var&quot;: {
        &quot;hostvars['h2'].key&quot;: &quot;h2&quot;
    }
}

PLAY RECAP ********************************************************************
h1                         : ok=2    changed=0    unreachable=0    failed=0"><pre class="notranslate"><code class="notranslate">PLAY [test host_vars includes] ************************************************

TASK: [debug var=hostvars['h1'].key] ******************************************
ok: [h1] =&gt; {
    "var": {
        "hostvars['h1'].key": "h1"
    }
}

TASK: [debug var=hostvars['h2'].key] ******************************************
ok: [h1] =&gt; {
    "var": {
        "hostvars['h2'].key": "h2"
    }
}

PLAY RECAP ********************************************************************
h1                         : ok=2    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 [test host_vars includes] ************************************************

TASK: [debug var=hostvars['h1'].key] ******************************************
ok: [h1] =&gt; {
    &quot;var&quot;: {
        &quot;hostvars['h1'].key&quot;: &quot;h1&quot;
    }
}

TASK: [debug var=hostvars['h2'].key] ******************************************
ok: [h1] =&gt; {
    &quot;var&quot;: {
        &quot;hostvars['h2'].key&quot;: &quot;{{hostvars['h2'].key}}&quot;
    }
}

PLAY RECAP ********************************************************************
h1                         : ok=2    changed=0    unreachable=0    failed=0"><pre class="notranslate"><code class="notranslate">PLAY [test host_vars includes] ************************************************

TASK: [debug var=hostvars['h1'].key] ******************************************
ok: [h1] =&gt; {
    "var": {
        "hostvars['h1'].key": "h1"
    }
}

TASK: [debug var=hostvars['h2'].key] ******************************************
ok: [h1] =&gt; {
    "var": {
        "hostvars['h2'].key": "{{hostvars['h2'].key}}"
    }
}

PLAY RECAP ********************************************************************
h1                         : ok=2    changed=0    unreachable=0    failed=0
</code></pre></div>
<p dir="auto">This bug report is a result investigating <a href="https://groups.google.com/forum/#!searchin/ansible-project/limit$20hostvars$20/ansible-project/ZZ12KPiOE2s/rfdotpnmKgAJ" rel="nofollow">https://groups.google.com/forum/#!searchin/ansible-project/limit$20hostvars$20/ansible-project/ZZ12KPiOE2s/rfdotpnmKgAJ</a> .</p>
<p dir="auto">It turns out that the host_vars/ directory must be relative to the inventory file <em>when the host is not included in --limit</em>. Ansible can define any such requirement, but note that it is inconsistent to be more liberal with the placement of the host_vars directory when a file is not excluded with --limit (i.e., if the host is included in the run, either the directory relative to the playbook can be used, or the directory relative to the inventory file).</p>
<p dir="auto">To reproduce, see the project in <a href="http://feliksik.nl/ansible-limit.tgz" rel="nofollow">http://feliksik.nl/ansible-limit.tgz</a> structure, which structure looks as follows (modify inventory to your environment, of course).</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=".
├── host_vars
│   └── outlimit
├── inventory
├── playbook.yml
└── sub
    └── inventory -&gt; ../inventory"><pre class="notranslate"><code class="notranslate">.
├── host_vars
│   └── outlimit
├── inventory
├── playbook.yml
└── sub
    └── inventory -&gt; ../inventory
</code></pre></div>
<p dir="auto">There is the following calls possible:<br>
root1) ansible-playbook -i inventory playbook.yml --limit inlimit<br>
root2) ansible-playbook -i inventory playbook.yml --limit inlimit,outlimit<br>
sub1) ansible-playbook -i sub/inventory playbook.yml --limit inlimit<br>
sub2) ansible-playbook -i sub/inventory playbook.yml --limit inlimit,outlimit</p>
<p dir="auto">Expected behaviour:</p>
<ul dir="auto">
<li>if root2 works, then root1 should also work</li>
<li>if sub2 works (i.e. it is legal for the host_vars directory to not be in the same directory as the inventory file), then sub1 should also work.</li>
</ul>
<p dir="auto">Actual behavior:</p>
<ul dir="auto">
<li>root1 works</li>
<li>root2 works</li>
<li>sub1 <strong>fails</strong>!</li>
<li>sub2 works</li>
</ul>