Create a local user on Windows-like systems - Ansible module win_user
How to automate the creation of the “example” local user member of Users group on Windows-like systems with Ansible Playbook with win_user module.


How to Create a local user on Windows-like systems 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 creates the 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
- password string - cleartext password
- description string - description of the group
- groups list - list of groups to adds or removes
- update_password string - always / on_create
- password_never_expires boolean - no / yes
- password_expired string - yes - change at next login / no
- account_locked / account_disabled - no /yes
The only required is “name”, which is the user name. The “state” parameter allows us to create or delete a user, in our use case the default it’s already set to “present” to create a user. The “password” set the password in cleartext. So easily specify what password assign to the user, no hash function are needed. The “description” parameter allows you to specify a description of the user, it’s not mandatory but sometimes is useful. The “groups” parameter allows you to add or remove the user from this list of groups. The “update_password” parameter specifies when the module will update the user password. “always” option will update passwords if they differ, “on_create” will only set the password for newly created users. Let me highlight also some parameters about password expiration “password_never_expires” to sets the password to never expire, or “password_expired” force the user to change the password at the next login. You could also lock or disable the account using “account_locked” and “account_disabled” parameters.
Links
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
How to create a local user on Windows-like systems with Ansible Playbook. I’m going to show you how to automate the creation of the “example” user member of the Users group on my demo Windows machine.
code
---
- name: windows user add
hosts: all
vars:
usr_name: 'example'
usr_password: 'EfY#Qs9*BWy9?'
usr_groups: "Users"
tasks:
- name: create local user
ansible.windows.win_user:
name: "{{ usr_name }}"
password: "{{ usr_password }}"
groups: "{{ usr_groups }}"
update_password: on_create
password_expired: true
execution
ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/user_add.yml
PLAY [windows user add] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [create 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_add.yml
PLAY [windows user add] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [create 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
Recap
Now you know how to create a local user on Windows-like systems 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