Remove user account - Ansible module user
How to remove an example user removing home directory and spool files.


How to remove a user account with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible remove user account
Today we’re talking about the Ansible module user
.
The full name is ansible.builtin.user
, 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 manages user accounts.
It supports a huge variety of Linux distributions, SunOS and macOS, and FreeBSD.
This module uses Linux distributions userdel
to delete, on FreeBSD, this module uses pw userdel
, on macOS, this module uses dscl
.
For Windows, use the ansible.windows.win_user
module instead.
Main Parameters
- name string - username
- state string - present/absent
- remove boolean - no/yes
This module has many parameters to perform any task.
The only required is “name”, which is the username.
“state” allows us to create or delete a user, in the use case we need to specify “absent” to delete a user.
If we would like to try to remove the directories associated with the user, we need to set the parameter “remove”.
The behavior is the same as userdel --remove
.
Files in the user’s home directory will be removed along with the home directory itself and the user’s mail spool.
Files in other parts of the file system will have to be searched for and deleted manually.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Let’s jump into a real-life Ansible Playbook to delete a user.
- delete_user.yml
---
- name: user module demo
hosts: all
become: true
tasks:
- name: user example not present
ansible.builtin.user:
name: example
state: "absent"
remove: true
Recap
Now you know how to remove a user account with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate