# group module fails when trying to delete non local group

**URL:** <https://forum.ansible.com/t/group-module-fails-when-trying-to-delete-non-local-group/19289>\
**Category:** Ansible Project\
**Created:** [September 17, 2015, 1:52pm UTC](https://forum.ansible.com/t/group-module-fails-when-trying-to-delete-non-local-group/19289 "2015-09-17T13:52:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaps](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shaps/32/1356_2.png) [@Shaps](https://forum.ansible.com/u/Shaps)\
**Post date:** [September 17, 2015, 1:52pm UTC](https://forum.ansible.com/t/group-module-fails-when-trying-to-delete-non-local-group/19289/1 "2015-09-17T13:52:09Z")

</div>

Hi list,

Sorry for the probably confusing subject, but I don’t really know how to say that better, I’ll try to explain better:

In my environment I have a requirement to remove certain local groups ( that have now been moved to remote groups ) and found out the behaviour of the module is quite strange.

So what happens is:

I try to remove the “support” group, which as said was a local group, and is now a remote group managed by LDAP:

- group: name=support state=absent  
What I get back from this is an error from groupdel:  
groupdel: cannot remove entry ‘support’ from /etc/group

Which I believe to be wrong as if the group does not exist in /etc/group the module should just return OK.

Digging into the module code I found why this is happening ( I’m using ansible v1.9 ):

group calls group\_exists: [https://github.com/ansible/ansible-modules-core/blob/stable-1.9/system/group.py#L413](https://github.com/ansible/ansible-modules-core/blob/stable-1.9/system/group.py#L413)

defined here: [https://github.com/ansible/ansible-modules-core/blob/stable-1.9/system/group.py#L128](https://github.com/ansible/ansible-modules-core/blob/stable-1.9/system/group.py#L128)

grp.getgrnam(’support’) will return the remote group data, so group\_exists returns True, hence the module runs groupdel which in turn fails as it doesn’t find the group in /etc/groups.

I have found out that grp.getgrall() only returns with local groups, so I worked around that problem this way:  
[http://fpaste.org/268435/97803144/](http://fpaste.org/268435/97803144/)

I don’t know if that can be considered a good way of handling that error, and I really don’t know if it makes sense to have that added to the upstream module. It worked for me so I thought it would be good to share 😉

Thanks,

Andrea

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [September 18, 2015, 6:24pm UTC](https://forum.ansible.com/t/group-module-fails-when-trying-to-delete-non-local-group/19289/2 "2015-09-18T18:24:27Z")

</div>

Won't that cause the opposite problem when creating groups? Also I  
believe the return is correct, it failed to delete the group as it is  
not a local group and will still be present on the machine. We might  
want to add a 'local\_only' flag to both this and user to be more  
specific on what you want as a user.

---

<div class="post-metadata">

**Author:** ![Shaps](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shaps/32/1356_2.png) [@Shaps](https://forum.ansible.com/u/Shaps)\
**Post date:** [September 21, 2015, 12:00pm UTC](https://forum.ansible.com/t/group-module-fails-when-trying-to-delete-non-local-group/19289/3 "2015-09-21T12:00:45Z")

</div>

Yes,  
That would indeed cause the opposite problem when creating groups! Didn’t really think about that, was focusing on the deletion  
Yes, probably adding a ‘local\_only’ to both the modules makes more sense, and should keep the return correct in both cases.

Thanks,  
Andrea
