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.

Join 50+ hours of courses in our exclusive community

Playbook

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

code

  • group_delete.yml
---
- name: group module Playbook
  hosts: all
  become: true
  vars:
    mygroup: "example"
  tasks:
    - name: delete group
      ansible.builtin.group:
        name: "{{ mygroup }}"
        state: absent

execution

output

$ ansible-playbook -i Playbook/inventory group/delete.yml
PLAY [group module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [delete group] *******************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

verification

$ ssh [email protected]
[devops@demo ~]$ sudo su
[root@demo devops]# getent group | grep example
[root@demo devops]# getent group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:33:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
users:x:100:devops
nobody:x:65534:
dbus:x:81:
utmp:x:22:
utempter:x:35:
input:x:999:
kvm:x:36:
render:x:998:
systemd-journal:x:190:
systemd-coredump:x:997:
systemd-resolve:x:193:
tss:x:59:
polkitd:x:996:
ssh_keys:x:995:
unbound:x:994:
sssd:x:993:
chrony:x:992:
sshd:x:74:
vagrant:x:1000:
vboxsf:x:991:
slocate:x:21:

code with ❤️ in GitHub

Conclusion

Now you know how to delete a group in Linux with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza