How to add or remove Windows user by Ansible playbook

To Add or Remove Windows user by Ansible playbook

Introduction:

Ansible is an IT automation tool used to manage various configurations of both Unix-like and Windows-based systems. The win_user module handles local Windows accounts.

Master Server Requirements:

ansible

python3-pip

pywinrm (python package)

Windows Requirements:

powershell 3+

Dot net 4

Installation Procedure:

Step 1: Check the OS version by using the following command

root@linuxhelp:~# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Check the availability of Ansible package

root@linuxhelp:~# apt list -a ansible
Listing... Done
ansible/hirsute,hirsute,now 4.8.0-1ppa~hirsute all [installed]
ansible/hirsute,hirsute 2.10.7-1 all

Step 3: Check the availability of python3-pip package

root@linuxhelp:~# apt list -a python3
Listing... Done
python3/hirsute,now 3.9.4-1 amd64 [installed,automatic]

Step 4: Check the availability of python package pywinrm

root@linuxhelp:~# pip list | grep pywinrm
pywinrm                0.4.2

Step 5: Create inventory for Windows node system

root@linuxhelp:~# vi /etc/ansible/hosts 

[windows]
192.168.6.104

[windows:vars]
ansible_user=Admin
ansible_password=Admin@123
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Step 6: Create playbook for Creating Windows user

root@linuxhelp:~# vi useradd.yml
- hosts: windows
  gather_facts: true
  tasks:
  - name: Creating user "ansibleuser"
    win_user:
     name: ansibleuser
     password: password
     state: present
     groups:
       - Users

Step 7: Check the syntax of the useradd.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook useradd.yml --syntax-check

playbook: useradd.yml

Step 8: Run the useradd.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook useradd.yml 

PLAY [windows] **************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [192.168.6.104]

TASK [Creating user "ansibleuser"] ******************************************************************************
changed: [192.168.6.104]

PLAY RECAP *************************************************************************************************

192.168.6.104              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Step 9: Windows user Created by Ansible

Step 10: Create playbook for removing Windows user

root@linuxhelp:~# vi removeuser.yml
- hosts: windows
  gather_facts: true
  tasks:
  - name: Removing user "ansibleuser"
    win_user:
     name: ansibleuser
     state: absent

Step 11: Check the syntax of the removeuser.yml ansible playbook by using the following command

root@linuxhelp:~# ansible-playbook removeuser.yml --syntax-check

playbook: removeuser.yml

Step 12: Run the removeuser.yml playbook by using the following command

root@linuxhelp:~# ansible-playbook removeuser.yml 

PLAY [windows] **************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
ok: [192.168.6.104]

TASK [Removing user "ansibleuser"] ******************************************************************************
changed: [192.168.6.104]

PLAY RECAP ******************************************************************************************************
192.168.6.104              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Step 13: Created Windows user has been removed

With this adding/removing of users for windows comes to end

FAQ
Q
How to remove a User?
A
Use "state: absent" to remove a user.
Q
How to Change the Password of a user?
A
Use "update_password: password" to Change the Password of a user.
Q
How to define the custom Home Directory of a user?
A
Use home_directory: path" to define custom Home Directory.
Q
How to lock an account?
A
Use "account_locked: yes" to lock any account.
Q
What is win_user?
A
win_user is a module that is maintained by the Ansible Core for managing local Windows user accounts.