<p dir="auto">Per discussion at <a href="https://groups.google.com/forum/#!topic/ansible-project/A79N7LRm-9I" rel="nofollow">https://groups.google.com/forum/#!topic/ansible-project/A79N7LRm-9I</a>, when I add systems to a group using group_by, they don't get variables for that group which are defined in group_vars.</p>
<p dir="auto">I've given an example that shows the issue in current devel (<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/c41a23a35ad166aa1e22d205fe8ad11318bef88a/hovercard" href="https://github.com/ansible/ansible/commit/c41a23a35ad166aa1e22d205fe8ad11318bef88a"><tt>c41a23a</tt></a>) in a gist at <a href="https://gist.github.com/mcodd/8250193">https://gist.github.com/mcodd/8250193</a>.  When running the test.yml playbook, I'd expect the variable groupvar to resolve to "this_group_var", but it instead remains undefined.</p>
<h5 dir="auto">ISSUE TYPE</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">inventory files</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.1.0
  config file = /Users/craig.bryan/.ansible.cfg
  configured module search path = [u'/Users/craig.bryan/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/craig.bryan/.virtualenvs/ansible24/lib/python2.7/site-packages/ansible
  executable location = /Users/craig.bryan/.virtualenvs/ansible24/bin/ansible
  python version = 2.7.13 (default, Dec 17 2016, 23:03:43) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]"><pre class="notranslate"><code class="notranslate">ansible 2.4.1.0
  config file = /Users/craig.bryan/.ansible.cfg
  configured module search path = [u'/Users/craig.bryan/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/craig.bryan/.virtualenvs/ansible24/lib/python2.7/site-packages/ansible
  executable location = /Users/craig.bryan/.virtualenvs/ansible24/bin/ansible
  python version = 2.7.13 (default, Dec 17 2016, 23:03:43) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<p dir="auto">No changes in configuration</p>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Running on OSX 10.11.6</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">Adding a inventory group var block before the inventory group member block causes the group membership to be modified.</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">Run <code class="notranslate">ansible-inventory</code> against the following inventories files to see the changes to group membership</p>
<p dir="auto">Test inventory 1</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[supergroup:children]
subgroup

[subgroup]
host1

[subgroup:vars]
foo=bar"><pre class="notranslate"><code class="notranslate">[supergroup:children]
subgroup

[subgroup]
host1

[subgroup:vars]
foo=bar
</code></pre></div>
<p dir="auto">Test inventory 2</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[supergroup:children]
subgroup

[subgroup:vars]
foo=bar

[subgroup]
host1"><pre class="notranslate"><code class="notranslate">[supergroup:children]
subgroup

[subgroup:vars]
foo=bar

[subgroup]
host1
</code></pre></div>
<p dir="auto">Both are inspected with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible-inventory -i &lt;INVENTORY_FILE&gt; --list"><pre class="notranslate"><code class="notranslate">ansible-inventory -i &lt;INVENTORY_FILE&gt; --list
</code></pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Both inventories act the same way when listed with <code class="notranslate">ansible-inventory</code></p>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">Test inventory 1 acts as expected - <code class="notranslate">subgroup</code> is within the <code class="notranslate">supergroup</code> group</p>
<p dir="auto">Test inventory 2 does not act the same way - <code class="notranslate">subgroup</code> is no longer within the <code class="notranslate">supergroup</code> group.</p>

<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  % ansible-inventory -i test-inventory-1 --list
{
    &quot;_meta&quot;: {
        &quot;hostvars&quot;: {
            &quot;host1&quot;: {
                &quot;foo&quot;: &quot;bar&quot;
            }
        }
    },
    &quot;all&quot;: {
        &quot;children&quot;: [
            &quot;supergroup&quot;,
            &quot;ungrouped&quot;
        ]
    },
    &quot;subgroup&quot;: {
        &quot;hosts&quot;: [
            &quot;host1&quot;
        ],
        &quot;vars&quot;: {
            &quot;foo&quot;: &quot;bar&quot;
        }
    },
    &quot;supergroup&quot;: {
        &quot;children&quot;: [
            &quot;subgroup&quot;
        ]
    },
    &quot;ungrouped&quot;: {}
}

  % ansible-inventory -i test-inventory-2 --list
{
    &quot;_meta&quot;: {
        &quot;hostvars&quot;: {
            &quot;host1&quot;: {
                &quot;foo&quot;: &quot;bar&quot;
            }
        }
    },
    &quot;all&quot;: {
        &quot;children&quot;: [
            &quot;subgroup&quot;,
            &quot;supergroup&quot;,
            &quot;ungrouped&quot;
        ]
    },
    &quot;subgroup&quot;: {
        &quot;hosts&quot;: [
            &quot;host1&quot;
        ],
        &quot;vars&quot;: {
            &quot;foo&quot;: &quot;bar&quot;
        }
    },
    &quot;supergroup&quot;: {},
    &quot;ungrouped&quot;: {}
}"><pre class="notranslate"><code class="notranslate">  % ansible-inventory -i test-inventory-1 --list
{
    "_meta": {
        "hostvars": {
            "host1": {
                "foo": "bar"
            }
        }
    },
    "all": {
        "children": [
            "supergroup",
            "ungrouped"
        ]
    },
    "subgroup": {
        "hosts": [
            "host1"
        ],
        "vars": {
            "foo": "bar"
        }
    },
    "supergroup": {
        "children": [
            "subgroup"
        ]
    },
    "ungrouped": {}
}

  % ansible-inventory -i test-inventory-2 --list
{
    "_meta": {
        "hostvars": {
            "host1": {
                "foo": "bar"
            }
        }
    },
    "all": {
        "children": [
            "subgroup",
            "supergroup",
            "ungrouped"
        ]
    },
    "subgroup": {
        "hosts": [
            "host1"
        ],
        "vars": {
            "foo": "bar"
        }
    },
    "supergroup": {},
    "ungrouped": {}
}
</code></pre></div>