# Traceback on group\_by

**URL:** <https://forum.ansible.com/t/traceback-on-group-by/14478>\
**Category:** Ansible Project\
**Created:** [February 3, 2014, 10:01pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478 "2014-02-03T22:01:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [February 3, 2014, 10:01pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/1 "2014-02-03T22:01:14Z")

</div>

Hello,

I’m getting a traceback on a group\_by, which is intended to create a group named after each of a host’s existing groups, prepended by a variable that is being set with ‘-e’ at invocation (a site/host-group ID):

> - name: vagrant | Create provider-agnostic ID\_and\_type group  
> group\_by: key=“{{ id }}\_{{ item }}”  
> with\_items: group\_names  
> when: provider == “vagrant”

The variable **provider** is also being set via ‘-e’. So for example, a host that was part of the dns and mail groups would be added to groups like prodSite1\_dns and prodSite1\_mail. These are not being set statically in an inventory file because sometimes it is run on ec2 hosts and the groups are pulled from tags, and sometimes it is run on a vagrant box and the groups are written into an inventory script.

Here is what I’m getting when running with -vvvv:

> TASK: [iaas\_gather\_multi | ec2 | Create provider-agnostic ID group] \*\*\*\*\*\*\*\*\*\*\*  
> skipping: [localhost]
> 
> TASK: [iaas\_gather\_multi | ec2 | Create provider-agnostic type group] \*\*\*\*\*\*\*\*\*  
> skipping: [localhost]
> 
> TASK: [iaas\_gather\_multi | ec2 | Create provider-agnostic ID\_and\_type group] \*\*\*  
> skipping: [localhost]
> 
> TASK: [iaas\_gather\_multi | ec2 | Create provider-agnostic host-provisioned boolean] \*\*\*  
> skipping: [localhost]
> 
> TASK: [iaas\_gather\_multi | debug var=group\_names] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ok: [localhost] =\> {  
> “group\_names”: [  
> “db”,  
> “webnode”  
> ],  
> “item”: “”  
> }
> 
> TASK: [iaas\_gather\_multi | vagrant | Create provider-agnostic ID\_and\_type group] \*\*\*  
> created ‘group\_by’ ActionModule: key={{id}}_{{item}}  
> changed: [localhost] =\> (item=db) =\> {“changed”: true, “groups”: {“vagrant-devhost\_db”: [“localhost”]}, “item”: “db”}  
> created ‘group\_by’ ActionModule: key={{id}}_{{item}}  
> fatal: [localhost] =\> Traceback (most recent call last):  
> File “/usr/lib/pymodules/python2.7/ansible/runner/ **init**.py”, line 394, in \_executor  
> exec\_rc = self.\_executor\_internal(host, new\_stdin)  
> File “/usr/lib/pymodules/python2.7/ansible/runner/ **init**.py”, line 514, in \_executor\_internal  
> complex\_args=complex\_args  
> File “/usr/lib/pymodules/python2.7/ansible/runner/ **init**.py”, line 685, in \_executor\_internal\_inner  
> result = handler.run(conn, tmp, module\_name, module\_args, inject, complex\_args)  
> File “/usr/lib/pymodules/python2.7/ansible/runner/action\_plugins/group\_by.py”, line 86, in run  
> del self.runner.inventory.\_vars\_per\_host[host]  
> KeyError: u’localhost’
> 
> FATAL: all hosts have already failed – aborting

Thank you,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [February 4, 2014, 11:46pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/2 "2014-02-04T23:46:38Z")

</div>

For what it’s worth, I tried taking out the reference to group\_names and instead passed into the role as a variable a list of group names to test with. I still got the traceback. It doesn’t happen if I just duplicate the task and use multiple copies of it that are even more hard coded such as: “{{ id }}\_aStaticGroupName”. Now the whole thing breaks horribly **_past_** that point because of the change, but the tracebacks stop.

So AFAICT I get the traceback trying to use “{{ id }}\_{{ item }}”.

Also I forgot to mention that I’m seeing this on v1.4.3.

Thank you,  
Mark

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [February 4, 2014, 11:50pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/3 "2014-02-04T23:50:52Z")

</div>

Please file a bug for any tracebacks you find. We would definitely like to know if you can reproduce this on 1.4.4 or on the latest devel.

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [March 24, 2014, 6:58pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/4 "2014-03-24T18:58:12Z")

</div>

Hi Mark/James,  
Do you guys know if there was a bug filed on this? I’m running into a similar thing in 1.5.3. If not i’ll try and put together a minimal playbook to reproduce.

thanks,  
matt

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [March 24, 2014, 7:31pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/5 "2014-03-24T19:31:09Z")

</div>

At the time I wasn’t immediately able to test the newer version James mentioned, and I worked around it soon after. So in short, no, I forgot to file anything in this case (sorry!).

How similar is your issue? (mainly curious if it is a host grouping thing, or if you’re just also iterating a built in structure with with\_items:)

My normal “real” ec2 workflow expected Ansible to use a dynamic inventory anyway, and I’d already parameter-ized that call to allow for different inventory scripts in case I wanted to add rax support later. So what I ended up doing to get by was get help on IRC to write a simple (basically fake) dynamic inventory script for vagrant deploys that just prints out “localhost”, but already a member of the groups I needed. So it’s pretty much just a static inventory file in a format that is more convenient to the situation.

# vagrant.py

> #!/usr/bin/env python
> 
> print ‘{“db”: {“hosts”: [“localhost”], “vars”: {“ansible\_connection”: “local”}}, “vagrant\_dummy\_group”: {“hosts”: [“localhost”], “vars”: {“ansible\_connection”: “local”}}, “web”: {“hosts”: [“localhost”], “vars”: {“ansible\_connection”: “local”}}}’

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [March 24, 2014, 7:41pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/6 "2014-03-24T19:41:43Z")

</div>

Hey Mark,  
Mine is using with\_items as well; i think that’s the common thread here. Specifically the task that seems to cause the exact traceback you see is as follows:

- name: generate role groups  
group\_by: key=“{{ item }}”  
with\_items: all\_roles

and it throws this traceback:

fatal: [waluat1] =\> Traceback (most recent call last):  
File “/rbi/ansible/ansible/lib/ansible/runner/ **init**.py”, line 532, in \_executor  
exec\_rc = self.\_executor\_internal(host, new\_stdin)  
File “/rbi/ansible/ansible/lib/ansible/runner/ **init**.py”, line 654, in \_executor\_internal  
complex\_args=complex\_args  
File “/rbi/ansible/ansible/lib/ansible/runner/ **init**.py”, line 809, in \_executor\_internal\_inner  
result = handler.run(conn, tmp, module\_name, module\_args, inject, complex\_args)  
File “/rbi/ansible/ansible/lib/ansible/runner/action\_plugins/group\_by.py”, line 86, in run  
del self.runner.inventory.\_vars\_per\_host[host]  
KeyError: ‘waluat1’

I will file something on github with a small example to reproduce.

matt

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [March 24, 2014, 8:23pm UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/7 "2014-03-24T20:23:54Z")

</div>

I’ve opened [https://github.com/ansible/ansible/issues/6660](https://github.com/ansible/ansible/issues/6660) with details…

thanks!  
matt

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [March 25, 2014, 12:19am UTC](https://forum.ansible.com/t/traceback-on-group-by/14478/8 "2014-03-25T00:19:48Z")

</div>

Thanks James for the patch! I can verify that [https://github.com/ansible/ansible/commit/dc658eaa1c636c47804bf70eddc55ada5e4078bf](https://github.com/ansible/ansible/commit/dc658eaa1c636c47804bf70eddc55ada5e4078bf), when applied manually to 1.5.3 fixes my issues here!

matt
