Loading...

Watch: Delete a group - Ansible module group

How to delete the \"example\" group or verify that is not present on your target Linux system or macOS.

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

``yaml

---

  • name: group module Playbook

hosts: all

become: true

vars:

mygroup: "example"

tasks:

- name: delete group

ansible.builtin.group:

name: "{{ mygroup }}"

state: absent

`

execution

output

`bash

$ 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

``bash

$ 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:

ma

Read the full tutorial: Delete a group - Ansible module group