<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">1.8.2</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Ubuntu 14.04</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">When using delegate_to with an inventory variable that uses another variable that includes the 'group' variable, the delegate_to host isn't templated correctly.</p>
<p dir="auto">If the variable is defined in the playbook, or if it's using the 'group' var directly, it works as expected.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">inv:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="server1 ansible_connection=local
server2 ansible_connection=local"><pre class="notranslate"><code class="notranslate">server1 ansible_connection=local
server2 ansible_connection=local
</code></pre></div>
<p dir="auto">group_vars/all:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="server_list: &quot;{{ groups.all }}&quot;
first_server: &quot;{{ server_list | first }}&quot;"><pre class="notranslate"><code class="notranslate">server_list: "{{ groups.all }}"
first_server: "{{ server_list | first }}"
</code></pre></div>
<p dir="auto">playbook.yml:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="
---
- hosts: server1
  tasks:
    - debug: var=first_server  # Variable is templated correctly here

    - ping:
      delegate_to: &quot;{{ first_server }}&quot;"><pre class="notranslate"><code class="notranslate">
---
- hosts: server1
  tasks:
    - debug: var=first_server  # Variable is templated correctly here

    - ping:
      delegate_to: "{{ first_server }}"
</code></pre></div>
<h5 dir="auto">Expected Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [debug var=first_server] ************************************************ 
ok: [server2] =&gt; {
    &quot;first_server&quot;: &quot;server1&quot;
}

TASK: [ping ] ***************************************************************** 
ok: [server2 -&gt; server1] "><pre class="notranslate"><code class="notranslate">TASK: [debug var=first_server] ************************************************ 
ok: [server2] =&gt; {
    "first_server": "server1"
}

TASK: [ping ] ***************************************************************** 
ok: [server2 -&gt; server1] 
</code></pre></div>
<h5 dir="auto">Actual Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [debug var=first_server] ************************************************ 
ok: [server2] =&gt; {
    &quot;first_server&quot;: &quot;server1&quot;
}

TASK: [ping ] ***************************************************************** 
fatal: [server2 -&gt; {] =&gt; SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh

FATAL: all hosts have already failed -- aborting"><pre class="notranslate"><code class="notranslate">TASK: [debug var=first_server] ************************************************ 
ok: [server2] =&gt; {
    "first_server": "server1"
}

TASK: [ping ] ***************************************************************** 
fatal: [server2 -&gt; {] =&gt; SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh

FATAL: all hosts have already failed -- aborting
</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"><code class="notranslate">ansible-inventory</code> CLI, found at <a href="https://github.com/ansible/ansible/blob/devel/lib/ansible/cli/inventory.py">https://github.com/ansible/ansible/blob/devel/lib/ansible/cli/inventory.py</a></p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<p dir="auto">2.4 or 2.5</p>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">Any, functionality of output should be consistent with any valid Ansible configurations.</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto"><code class="notranslate">ansible-inventory</code> will return all variables at the host level. This is consistent with behavior inside of playbooks, but can lead to very large outputs, because variables defined at the group level will be replicated with each host inside of it.</p>
<p dir="auto">I previously made an attempt at this in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="243210462" data-permission-text="Title is private" data-url="https://github.com/bcoca/ansible/issues/4" data-hovercard-type="pull_request" data-hovercard-url="/bcoca/ansible/pull/4/hovercard" href="https://github.com/bcoca/ansible/pull/4">bcoca#4</a>, but that solution needed more work to respect the users existing configuration customizations.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">Copy this folder to your local machine, cd into it.</p>
<p dir="auto"><a href="https://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/issues/gvars">https://github.com/AlanCoding/Ansible-inventory-file-examples/tree/master/issues/gvars</a></p>
<p dir="auto"><code class="notranslate">ansible-inventory -i inventory.ini --list</code></p>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Perhaps by adding another flag, like <code class="notranslate">--optimized</code>, we could get output without the <code class="notranslate">airport_code</code> variable duplicated, like so:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;_meta&quot;: {
        &quot;hostvars&quot;: {
            &quot;host1&quot;: {}, 
            &quot;host2&quot;: {}
        }
    }, 
    &quot;all&quot;: {
        &quot;children&quot;: [
            &quot;raleigh&quot;, 
            &quot;ungrouped&quot;
        ]
    }, 
    &quot;raleigh&quot;: {
        &quot;hosts&quot;: [
            &quot;host1&quot;, 
            &quot;host2&quot;
        ],
       &quot;vars&quot;: {
           &quot;airport_code&quot;: &quot;RDU&quot;
       }
    }, 
    &quot;ungrouped&quot;: {}
}"><pre class="notranslate"><code class="notranslate">{
    "_meta": {
        "hostvars": {
            "host1": {}, 
            "host2": {}
        }
    }, 
    "all": {
        "children": [
            "raleigh", 
            "ungrouped"
        ]
    }, 
    "raleigh": {
        "hosts": [
            "host1", 
            "host2"
        ],
       "vars": {
           "airport_code": "RDU"
       }
    }, 
    "ungrouped": {}
}
</code></pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">The only currently available way of using this command will put <code class="notranslate">airport_code</code> (a variable specific to the group <code class="notranslate">raleigh</code>) in hostvars.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
    &quot;_meta&quot;: {
        &quot;hostvars&quot;: {
            &quot;host1&quot;: {
                &quot;airport_code&quot;: &quot;RDU&quot;
            }, 
            &quot;host2&quot;: {
                &quot;airport_code&quot;: &quot;RDU&quot;
            }
        }
    }, 
    &quot;all&quot;: {
        &quot;children&quot;: [
            &quot;raleigh&quot;, 
            &quot;ungrouped&quot;
        ]
    }, 
    &quot;raleigh&quot;: {
        &quot;hosts&quot;: [
            &quot;host1&quot;, 
            &quot;host2&quot;
        ]
    }, 
    &quot;ungrouped&quot;: {}
}"><pre class="notranslate"><code class="notranslate">{
    "_meta": {
        "hostvars": {
            "host1": {
                "airport_code": "RDU"
            }, 
            "host2": {
                "airport_code": "RDU"
            }
        }
    }, 
    "all": {
        "children": [
            "raleigh", 
            "ungrouped"
        ]
    }, 
    "raleigh": {
        "hosts": [
            "host1", 
            "host2"
        ]
    }, 
    "ungrouped": {}
}
</code></pre></div>
<p dir="auto">RESOLVED: <del>Note that <code class="notranslate">ansible_inventory_sources</code> appears to be some other unrelated thing, and I may raise a separate issue about that.</del></p>