Ansible Pilot

Change local user password on Windows-like systems - Ansible module win_user

How to automate the password changing of the local user “example” on Windows-like systems using Ansible Playbook and module win_user.

March 24, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to change user passwords on Windows-like systems with Ansible?

Password change is a mundane task that every System Administrator needs to perform regularly for your user base. Using Ansible you could simplify your workflow and maintain consistent your IT infrastructure fleet. 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 changes local user password

Today we’re talking about the 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

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 is needed. 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.

The Best Resources For Ansible

Video Course

Books

demo

Change user password on Windows-like systems with Ansible Playbook. I’m going to show you how to automate the password of local user “example” with an autogenerated one and verify on the Windows side with a successful login.

code

---
- name: windows change password
  hosts: all
  vars:
    usr_name: 'example'
    usr_password: 'SMJAo$%8AzU6'
  tasks:
    - name: change password
      ansible.windows.win_user:
        name: "{{ usr_name }}"
        password: "{{ usr_password }}"

execution

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory windows/user_changepassword.yml
PLAY [windows change password] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [change password] ****************************************************************************
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_changepassword.yml
PLAY [windows change password] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [WindowsServer]
TASK [change password] ****************************************************************************
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

Recap

Now you know how to change local user passwords 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

BUY the Complete PDF BOOK to easily Copy and Paste the 200+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress