<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Fancy Corner Case Bug</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">Introduced in current devel as well as 1.8.2 and up.<br>
In the 1.8 tree, the bug was introduced by <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/4cd87ec93ec1f5266e5afaaa8e6734d642284afe/hovercard" href="https://github.com/ansible/ansible/commit/4cd87ec93ec1f5266e5afaaa8e6734d642284afe"><tt>4cd87ec</tt></a></p>
<p dir="auto">Note that while the bug is unrelated, this "faulty" commit is the same as for <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="51405872" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/9753" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/9753/hovercard" href="https://github.com/ansible/ansible/issues/9753">#9753</a></p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">N/A (tested on Ubuntu 14.04)</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">Using a key pointing to a variable, that itself is defined as another variable construct, in combination with group_by, the resulting key (probably that subvar) is onlky calculated once for the first host, so all hosts are put in the same group.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">Create an inventory <code class="notranslate">hosts</code> file with some hosts where the first three chars point to a datacenter:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dc1-ch1-bl1                                                                     
dc2-ch1-bl2                                                                     
dc1-ch1-bl3                                                                     
dc2-ch1-bl4                                                                     
dc2-ch1-bl5   "><pre class="notranslate"><code class="notranslate">dc1-ch1-bl1                                                                     
dc2-ch1-bl2                                                                     
dc1-ch1-bl3                                                                     
dc2-ch1-bl4                                                                     
dc2-ch1-bl5   
</code></pre></div>
<p dir="auto">and provide this variable in <code class="notranslate">group_vars/all.yml</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="datacenter: '{{ inventory_hostname[:3] }}'"><pre class="notranslate"><code class="notranslate">datacenter: '{{ inventory_hostname[:3] }}'
</code></pre></div>
<p dir="auto">Test with the commmand<br>
<code class="notranslate">ansible all -i hosts -c local -m group_by -a key={{datacenter}}</code></p>
<p dir="auto">Also change the <em>first</em> host in the inventory file from <code class="notranslate">dc1-ch1-bl1</code> to <code class="notranslate">dc2-ch1-bl1</code> and observe the difference.</p>
<h5 dir="auto">Expected Results:</h5>
<p dir="auto">Works with ansible release1.8.1 <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/ad675235e2f65a0ed464f57a5c5b65553f09e8bb/hovercard" href="https://github.com/ansible/ansible/commit/ad675235e2f65a0ed464f57a5c5b65553f09e8bb"><tt>ad67523</tt></a> last updated 2014/12/02 10:53:48 (GMT +200):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible all -i hosts -c local -m group_by -a key={{datacenter}}
dc1-ch1-bl1 | success &gt;&gt; {
    &quot;changed&quot;: true, 
    &quot;groups&quot;: {
        &quot;dc1&quot;: [
            &quot;dc1-ch1-bl1&quot;, 
            &quot;dc1-ch1-bl3&quot;
        ], 
        &quot;dc2&quot;: [
            &quot;dc2-ch1-bl2&quot;, 
            &quot;dc2-ch1-bl4&quot;, 
            &quot;dc2-ch1-bl5&quot;
        ]
    }
}"><pre class="notranslate"><code class="notranslate">$ ansible all -i hosts -c local -m group_by -a key={{datacenter}}
dc1-ch1-bl1 | success &gt;&gt; {
    "changed": true, 
    "groups": {
        "dc1": [
            "dc1-ch1-bl1", 
            "dc1-ch1-bl3"
        ], 
        "dc2": [
            "dc2-ch1-bl2", 
            "dc2-ch1-bl4", 
            "dc2-ch1-bl5"
        ]
    }
}
</code></pre></div>
<h5 dir="auto">Actual Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dc1-ch1-bl1 | success &gt;&gt; {
    &quot;changed&quot;: true,
    &quot;groups&quot;: {
        &quot;dc1&quot;: [
            &quot;dc1-ch1-bl1&quot;,
            &quot;dc2-ch1-bl2&quot;,
            &quot;dc1-ch1-bl3&quot;,
            &quot;dc2-ch1-bl4&quot;,
            &quot;dc2-ch1-bl5&quot;
        ]
    }
}"><pre class="notranslate"><code class="notranslate">dc1-ch1-bl1 | success &gt;&gt; {
    "changed": true,
    "groups": {
        "dc1": [
            "dc1-ch1-bl1",
            "dc2-ch1-bl2",
            "dc1-ch1-bl3",
            "dc2-ch1-bl4",
            "dc2-ch1-bl5"
        ]
    }
}
</code></pre></div>
<p dir="auto">or after changing the <em>first</em> host in the inventory file from <code class="notranslate">dc1-ch1-bl1</code> to <code class="notranslate">dc2-ch1-bl1</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="dc2-ch1-bl1 | success &gt;&gt; {
    &quot;changed&quot;: true,
    &quot;groups&quot;: {
        &quot;dc2&quot;: [
            &quot;dc2-ch1-bl1&quot;,
            &quot;dc2-ch1-bl2&quot;,
            &quot;dc1-ch1-bl3&quot;,
            &quot;dc2-ch1-bl4&quot;,
            &quot;dc2-ch1-bl5&quot;
        ]
    }
}"><pre class="notranslate"><code class="notranslate">dc2-ch1-bl1 | success &gt;&gt; {
    "changed": true,
    "groups": {
        "dc2": [
            "dc2-ch1-bl1",
            "dc2-ch1-bl2",
            "dc1-ch1-bl3",
            "dc2-ch1-bl4",
            "dc2-ch1-bl5"
        ]
    }
}
</code></pre></div>
<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">ansible 1.5.4</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Ubuntu 12.04 x64 running Ansible, managing 13.10 x64 hosts</p>
<h5 dir="auto">Summary:</h5>
<ul dir="auto">
<li>Attempting to use 'group_by:' with 'when:'.</li>
<li>Two hosts; one in group <em><strong>dbnodes</strong></em>, one in group <em><strong>webnodes</strong></em>, both also a member of ec2_hosts.</li>
<li>One host satisfies the when clause, one does not.</li>
<li>Flipping group membership doesn't flip which host is grouped...just skips the task altogether.</li>
</ul>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">See gist: <a href="https://gist.github.com/mark-casey/11386608">https://gist.github.com/mark-casey/11386608</a><br>
One provided inventory works for me, one doesn't.</p>
<h5 dir="auto">Expected Results:</h5>
<p dir="auto">The host which is a member of 'dbnodes' should be added to the 'Debian' group, for either of the two inventory files provided.</p>
<h5 dir="auto">Actual Results:</h5>
<p dir="auto">One of the inventory files sees no hosts added to the 'Debian' group.</p>
<p dir="auto">Also interesting, either of the two inventories will work if you entirely remove the ec2_hosts group and change site.yml's play to use this host selector instead:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  hosts: dbnodes:webnodes"><pre class="notranslate"><code class="notranslate">  hosts: dbnodes:webnodes
</code></pre></div>