<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">ansible 1.7 (devel <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/ansible/ansible/commit/ee82681d142c9f06c9d4c1db811f6f83b399b007/hovercard" href="https://github.com/ansible/ansible/commit/ee82681d142c9f06c9d4c1db811f6f83b399b007"><tt>ee82681</tt></a>) last updated 2014/05/23 10:56:18 (GMT +200)</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">Ubuntu 14.04</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">Using <code class="notranslate">group_by</code> with <code class="notranslate">with_items</code>, host-specific variables seem to be resolved only for the first host.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">hosts:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[group]
host1
host2"><pre class="notranslate"><code class="notranslate">[group]
host1
host2
</code></pre></div>
<p dir="auto">playbook:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ item }}
    with_items:
    - &quot;{{ inventory_hostname }}&quot;
  - debug: var=group_names"><pre class="notranslate"><code class="notranslate">- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ item }}
    with_items:
    - "{{ inventory_hostname }}"
  - debug: var=group_names
</code></pre></div>
<p dir="auto">Run <code class="notranslate">ansible-playbook</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible-playbook -i hosts test.yml"><pre class="notranslate"><code class="notranslate">$ ansible-playbook -i hosts test.yml
</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 [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host1&quot;
}
ok: [host2] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host2&quot;
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=host1)
changed: [host2] =&gt; (item=host2)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host1&quot;
    ]
}
ok: [host2] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host2&quot;
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   "><pre class="notranslate"><code class="notranslate">PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    "inventory_hostname": "host1"
}
ok: [host2] =&gt; {
    "inventory_hostname": "host2"
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=host1)
changed: [host2] =&gt; (item=host2)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    "group_names": [
        "group", 
        "host1"
    ]
}
ok: [host2] =&gt; {
    "group_names": [
        "group", 
        "host2"
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    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 [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host1&quot;
}
ok: [host2] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host2&quot;
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=host1)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host1&quot;
    ]
}
ok: [host2] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host1&quot;
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   "><pre class="notranslate"><code class="notranslate">PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    "inventory_hostname": "host1"
}
ok: [host2] =&gt; {
    "inventory_hostname": "host2"
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=host1)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    "group_names": [
        "group", 
        "host1"
    ]
}
ok: [host2] =&gt; {
    "group_names": [
        "group", 
        "host1"
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   
</code></pre></div>
<h5 dir="auto">Differences:</h5>
<p dir="auto">Note two things:</p>
<ol dir="auto">
<li><code class="notranslate">host2</code> is in group <code class="notranslate">host1</code> instead of <code class="notranslate">host2</code>.</li>
<li>(cosmetic) There is only one status line for the <code class="notranslate">group_by</code> task.</li>
</ol>
<h5 dir="auto">Further info:</h5>
<p dir="auto">Using <code class="notranslate">with_items</code> without host-specific variables does work, but still only shows status lines for the first host:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ item }}
    with_items:
    - test1
    - test2
    - test3
  - debug: var=group_names"><pre class="notranslate"><code class="notranslate">- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ item }}
    with_items:
    - test1
    - test2
    - test3
  - debug: var=group_names
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host2] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host2&quot;
}
ok: [host1] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host1&quot;
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=test1)
changed: [host1] =&gt; (item=test2)
changed: [host1] =&gt; (item=test3)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;test1&quot;, 
        &quot;test2&quot;, 
        &quot;test3&quot;
    ]
}
ok: [host2] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;test1&quot;, 
        &quot;test2&quot;, 
        &quot;test3&quot;
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   "><pre class="notranslate"><code class="notranslate">PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host2] =&gt; {
    "inventory_hostname": "host2"
}
ok: [host1] =&gt; {
    "inventory_hostname": "host1"
}

TASK: [group_by key={{item}}] ************************************************* 
changed: [host1] =&gt; (item=test1)
changed: [host1] =&gt; (item=test2)
changed: [host1] =&gt; (item=test3)

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    "group_names": [
        "group", 
        "test1", 
        "test2", 
        "test3"
    ]
}
ok: [host2] =&gt; {
    "group_names": [
        "group", 
        "test1", 
        "test2", 
        "test3"
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   
</code></pre></div>
<p dir="auto">Using a host-specific variable is in itself also not a problem:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ inventory_hostname }}
  - debug: var=group_names"><pre class="notranslate"><code class="notranslate">- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=inventory_hostname
  - group_by: key={{ inventory_hostname }}
  - debug: var=group_names
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host1&quot;
}
ok: [host2] =&gt; {
    &quot;inventory_hostname&quot;: &quot;host2&quot;
}

TASK: [group_by key={{inventory_hostname}}] *********************************** 
changed: [host1]

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host1&quot;
    ]
}
ok: [host2] =&gt; {
    &quot;group_names&quot;: [
        &quot;group&quot;, 
        &quot;host2&quot;
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   "><pre class="notranslate"><code class="notranslate">PLAY [all] ******************************************************************** 

TASK: [debug var=inventory_hostname] ****************************************** 
ok: [host1] =&gt; {
    "inventory_hostname": "host1"
}
ok: [host2] =&gt; {
    "inventory_hostname": "host2"
}

TASK: [group_by key={{inventory_hostname}}] *********************************** 
changed: [host1]

TASK: [debug var=group_names] ************************************************* 
ok: [host1] =&gt; {
    "group_names": [
        "group", 
        "host1"
    ]
}
ok: [host2] =&gt; {
    "group_names": [
        "group", 
        "host2"
    ]
}

PLAY RECAP ******************************************************************** 
host1                      : ok=3    changed=1    unreachable=0    failed=0   
host2                      : ok=3    changed=1    unreachable=0    failed=0   
</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>