# Aggregating hosts by group\_by and groups

**URL:** <https://forum.ansible.com/t/aggregating-hosts-by-group-by-and-groups/18868>\
**Category:** Ansible Project\
**Created:** [July 27, 2015, 9:34pm UTC](https://forum.ansible.com/t/aggregating-hosts-by-group-by-and-groups/18868 "2015-07-27T21:34:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Offer\_Sharabi](https://avatars.discourse-cdn.com/v4/letter/o/f07891/32.png) [@Offer\_Sharabi](https://forum.ansible.com/u/Offer_Sharabi)\
**Post date:** [July 27, 2015, 9:34pm UTC](https://forum.ansible.com/t/aggregating-hosts-by-group-by-and-groups/18868/1 "2015-07-27T21:34:39Z")

</div>

Hi there,  
So my task is quite simple, run a play against a group of hosts. Then , in the next play report which hosts completed the task successfully and which don’t. I am using the group\_by keyword for that, but without much success.

In more detail ,  
I am running a deploy playbook against a large group of hosts ( called bid ) in one play where I also group each host by a boolean variable.  
In the next play (same playbook) I am trying to write to screen all hosts from each group.

This is how it is laid out:

play1:

- name: group by already\_failed  
group\_by: key={{ (already\_failed) | ternary(‘failed\_hosts’, ‘success\_hosts’) }}  
tags:
- release

play2:

- name: set success and failed groups  
set\_fact:  
failed\_hosts=“{{ groups[‘failed\_hosts’] | default(‘None’) }}”  
success\_hosts=“{{ groups[‘success\_hosts’] | default(‘None’) }}”  
run\_once: True  
tags:

- notification

- release

- name: log grouped hosts to screen  
debug: msg="Failed releases on - {{ failed\_hosts }}. Success releases on - {{ groups[‘success\_hosts’] }} "  
run\_once: True  
tags:

- release

When I run this task against 3 hosts and serial is 3 or more than this succeeds :

TASK: [set success and failed groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.yyy.32.xxx] =\> {“ansible\_facts”: {“failed\_hosts”: “None”, “success\_hosts”: “[‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’]”}}

But when I run it with serial which is lower than the number of hosts, then the output only contains the first hosts in that group, e.g.

TASK: [set success and failed groups] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.yyy.32.xxx] =\> {“ansible\_facts”: {“failed\_hosts”: “None”, “success\_hosts”: “[‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’]”}}

So it looks like the newly created group when accessed from the group.succes\_host is only aware of the first hosts that were set in it.

To make things more confusing, when I ran the second play against the newly created group, then ALL hosts were actually running it - meaning that the group actually contains all hosts, but not when accessed from the groups vars.

TASK: [log grouped hosts to screen] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.yyy.32.xxx] =\> {  
“msg”: "Failed releases on - None. Success releases on - [‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’] "  
}  
ok: [54.yyy.32.xxx] =\> {  
“msg”: "Failed releases on - None. Success releases on - [‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’] "  
}  
ok: [54.yyy.32.xxx] =\> {  
“msg”: "Failed releases on - None. Success releases on - [‘54.yyy.32.xxx’, ‘54.yyy.32.xxx’] "  
}

I got a sense that this is a bug, but would appreciate a second pair of eyes .

Any help will be much appreciated,  
Thanks,  
Offer

---

<div class="post-metadata">

**Author:** ![Martin2](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@Martin2](https://forum.ansible.com/u/Martin2)\
**Post date:** [July 28, 2015, 2:39pm UTC](https://forum.ansible.com/t/aggregating-hosts-by-group-by-and-groups/18868/2 "2015-07-28T14:39:05Z")

</div>

How about this:  
debug: msg="Failed releases on - {{ groups[‘bids:!success\_hosts’ }}. Success releases on - {{ groups[‘success\_hosts’] }} "

Not sure about the syntax right now but basically:

- You have a group of all hosts “bid”
- You record all the hosts that are good (success hosts)
- You output the inventory of “bid:!success\_hosts” – that is all hosts in the bid group except for the ones that were successfull.

Why is that necessary?

AFAIK it’s necessary because once a host is failed ansible will remove it from the play (or even playbook?). But you can still access the group where it originated from and just exclude all the hosts that were good. This way you also need only one group and not 2 groups 🙂

Someone with a greater understanding of ansible will probably cringe by now and tell you all the stuff that I was stating falsely 🙂

HTH,  
Martin

---

<div class="post-metadata">

**Author:** ![Offer\_Sharabi](https://avatars.discourse-cdn.com/v4/letter/o/f07891/32.png) [@Offer\_Sharabi](https://forum.ansible.com/u/Offer_Sharabi)\
**Post date:** [July 28, 2015, 7:47pm UTC](https://forum.ansible.com/t/aggregating-hosts-by-group-by-and-groups/18868/3 "2015-07-28T19:47:12Z")

</div>

Thanks Martin !

But the issue still remains, the success\_hosts group does not contain all the hosts that should be included ( I suspect this is a bug and will open an issue in the right place), so doing bid - success will not give you the right answer.  
Also, I control for failure with a variable, so those hosts who failed are still in the game. This is so if all hosts failed, the playbook will not abort and I was supposed to have all hosts in the failed group reported.
