How to Remove a local user on Windows-like systems 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 remove local user account
ansible.windows.win_user
- Manages local Windows user accounts
Today we’re talking about Ansible module win_user
.
The full name is ansible.windows.win_user
, which means that is part of the collection of modules specialized to interact with Windows target host.
It’s a module pretty stable and out for years.
It works in Windows and Windows Server operating systems.
It manages local Windows user accounts.
For Linux target use the user
module instead.
Parameters
- name string - user name
- state string - present/absent
The only required is “name”, which is the user name. The “state” parameter allows us to create or delete a user. For our use case, we need to use the “absent” option.
Playbook
How to Remove a local user on Windows-like systems with Ansible Playbook. I’m going to show you how to automate the deletion of the “example” user on my Playbook Windows machine.
code
---
- name: windows user remove
hosts: all
vars:
usr_name: 'example'
tasks:
- name: delete local user
ansible.windows.win_user:
name: "{{ usr_name }}"
state: absent
execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/user_remove.yml
PLAY [windows user remove] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [delete local user] **************************************************************************
changed: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/user_remove.yml
PLAY [windows user remove] ************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [delete local user] **************************************************************************
ok: [WindowsServer]
PLAY RECAP ****************************************************************************************
WindowsServer : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
after execution
Conclusion
Now you know how to Remove a local user on Windows-like systems 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.
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