AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Manage Groups: Create, Delete & Modify with group Module

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to manage Linux groups with Ansible group module. Create groups, delete groups, set GIDs, manage system groups, and assign users to groups.

How to delete a group in Linux with Ansible? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot

Ansible deletes a group account • ansible.builtin.group • Add or remove groups

Today we're talking about the Ansible module group. The full name is ansible.builtin.group, which means that is part of the collection of modules "builtin" with ansible and shipped with it. It's a module pretty stable and out for years. It adds or removes groups. It supports a huge variety of Linux distributions and macOS. It relies on three Linux commands: groupadd, groupdel and groupmod. For Windows, use the ansible.windows.win_group module instead.

Parameters • name string - group name • state string - present/absent • local string - "local" command alternatives

This module has some parameters to perform some tasks. The only required is "name", which is the group name. The "state" parameter allows us to create or delete a group, in our use case set to "absent" to delete a group. The "local" parameter allows using the "local" command alternatives on platforms that implement it if you have a central authentication system.

## Playbook

Let's jump in a real-life Ansible Playbook to delete a group.

code • group_delete.yml

execution output

verification

code with ❤️ in GitHub

Conclusion

Now you know how to delete a group in Linux with Ansible.

Remove a Group

Remove Multiple Groups

Safe Removal (Check Members First)

Remove Users from Group First

Create vs Delete Comparison

Windows Group Removal

FAQ

What happens to files owned by a deleted group?

Files keep their GID but show a numeric ID instead of a name. Find them with:

Can I delete a group that is a user's primary group?

No - you'll get an error. Remove or reassign the user first.

Is group removal idempotent?

Yes - running state: absent on a non-existent group succeeds without error.

Delete a Group

Create a Group

Manage Multiple Groups

User + Group Management

Remove Users Before Group

Check Group Exists

group Module Parameters

| Parameter | Description | |-----------|-------------| | name | Group name | | state | present or absent | | gid | Group ID number | | system | Create as system group | | force | Force deletion (even if primary group) | | local | Use local commands (not LDAP/NIS) | | non_unique | Allow duplicate GIDs |

FAQ

"group is primary group" error when deleting?

The group is a user's primary group. Remove or reassign users first:

How do I change a group's GID?

Can I manage Windows groups?

Use ansible.windows.win_group:

Create a Group

Delete a Group

Create with Specific GID

System Group

Multiple Groups

Create Group Then Add Users

Non-Unique GID

Full User + Group Setup

Verify Group Exists

FAQ

Can I rename a group?

No — the group module doesn't support renaming. Create the new group, move users, delete the old one.

What happens if I delete a group with members?

The group is removed but users keep their other group memberships. Their files retain the old GID numerically.

group vs user module for group membership?

group manages group existence (create/delete). user manages group membership (which users belong to which groups).

Related ArticlesAnsible Become GuideAnsible Inventory GuideAnsible Roles GuideAnsible for Windows Guide

Category: troubleshooting

Watch the video: Ansible Manage Groups: Create, Delete & Modify with group Module — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home