AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Create Windows Local User: win_user Module (Complete Guide)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to create local user accounts on Windows with Ansible win_user module. Set passwords, groups, permissions, and manage user lifecycle with playbook examples.

Ansible Create Windows Local User: win_user Module (Complete Guide)

How to Create 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

See also: Ansible Change Windows User Password: win_user Module (Examples)

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.

See also: Ansible Remove Windows User: win_user Module state=absent (Examples)

Links

ansible.windows.win_user

## Playbook

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 Playbook 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

win_user before execution

after execution

win_user after execution win_user after execution

code with ❤️ in GitHub

Conclusion

Now you know how to create a local user on Windows-like systems with Ansible.

See also: Create Local Groups on Windows with Ansible Playbooks

Create Basic User

- name: Create local user
  ansible.windows.win_user:
    name: deploy
    password: "{{ vault_user_password }}"
    state: present
    groups:
      - Users
      - Remote Desktop Users
  no_log: true

Create Admin User

- name: Create local admin
  ansible.windows.win_user:
    name: admin_user
    password: "{{ vault_admin_password }}"
    state: present
    groups:
      - Administrators
    password_never_expires: true
  no_log: true

Create Service Account

- name: Create service account
  ansible.windows.win_user:
    name: svc_myapp
    password: "{{ vault_svc_password }}"
    state: present
    groups: []
    description: "Service account for MyApp"
    user_cannot_change_password: true
    password_never_expires: true
  no_log: true

Full User Setup

---
- name: Create user with home and profile
  hosts: windows
  tasks:
    - name: Create user
      ansible.windows.win_user:
        name: "{{ item.name }}"
        fullname: "{{ item.fullname }}"
        password: "{{ item.password }}"
        groups: "{{ item.groups }}"
        state: present
        description: "{{ item.description | default('') }}"
      loop: "{{ vault_users }}"
      loop_control:
        label: "{{ item.name }}"
      no_log: true

- name: Create user directories ansible.windows.win_file: path: "C:\\Users\\{{ item.name }}\\Documents\\Work" state: directory loop: "{{ vault_users }}" loop_control: label: "{{ item.name }}"

win_user Parameters

| Parameter | Description | |-----------|-------------| | name | Username | | password | Plain text password | | state | present, absent, query | | groups | List of local groups | | groups_action | add, replace, remove | | fullname | Full display name | | description | Account description | | password_expired | Force change at next login | | password_never_expires | Disable expiry | | user_cannot_change_password | Lock password | | account_disabled | Disable account | | account_locked | Lock/unlock account | | update_password | always or on_create |

Disable/Enable User

# Disable
- ansible.windows.win_user:
    name: john
    account_disabled: true

# Enable - ansible.windows.win_user: name: john account_disabled: false

Remove User

- ansible.windows.win_user:
    name: old_user
    state: absent

Query User Info

- ansible.windows.win_user:
    name: deploy
    state: query
  register: user_info

- debug: msg: | Exists: {{ user_info.state == 'present' }} Groups: {{ user_info.groups | default([]) }} Disabled: {{ user_info.account_disabled | default(false) }}

FAQ

win_user vs microsoft.ad.user?

win_user manages local accounts. microsoft.ad.user manages Active Directory domain accounts.

Can I create a user without a password?

No — Windows requires a password for local accounts (unless Group Policy allows blank passwords, which is insecure).

How do I add a user to a group without removing from others?

- ansible.windows.win_user:
    name: deploy
    groups: [Docker Users]
    groups_action: add  # Don't remove from existing groups

Related Articles

Ansible inventory best practicesWindows management with Ansible

Category: troubleshooting

Watch the video: Ansible Create Windows Local User: win_user Module (Complete Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home