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.builtin.user: Change User Password with Ansible (Secure Guide) — Video Tutorial

How to change user passwords with Ansible user module. Hash passwords securely, use Vault for credentials, manage password rotation across servers.

Watch Video

Watch "ansible.builtin.user: Change User Password with Ansible (Secure Guide)" on YouTube

What You'll Learn

Full Tutorial Content

Introduction In today's episode of Ansible Pilot, I'm Luca Berton, and we'll be delving into the process of changing a user password on a Linux system using Ansible. Specifically, we'll be utilizing the `ansible.builtin.user` module, an integral part of Ansible's collection of built-in modules. The Ansible User Module The `ansible.builtin.user` module is a stable and well-established component of Ansible, designed to manage user accounts. It boasts compatibility with a wide range of Linux distributions, including RHEL, CentOS, Fedora, Ubuntu, Debian, SUSE, as well as SunOS, macOS, and FreeBSD. For Windows systems, the equivalent module is `ansible.windows.win_user`. Parameters The `user` module comes with various parameters, but the three key ones for our password-changing task are: - **name (string):** Specifies the username. - **state (string):** Indicates the desired state of the user account (present or absent). - **password (string):** For Linux systems, the password must be provided in encrypted form, while macOS accepts cleartext passwords. Writing the Ansible Playbook Let's take a practical approach by crafting an Ansible Playbook that changes the password for a user account on a Linux system. Ansible Playbook Code: `change_password.yml` ```yaml --- - name: user module Playbook hosts: all become: true vars: myuser: "example" mypassword: "password" tasks: - name: change password ansible.builtin.user: name: "{{ myuser }}" state: present password: "{{ mypassword | password_hash('sha512') }}" ``` Executing the Playbook To execute the playbook, use the following command: ```bash $ ansible-playbook -i Playbook/inventory change\ user\ password/user.yaml ``` output ```bash $ ansible-playbook -i Playbook/inventory change\ user\ password/user.yaml PLAY [user module Playbook] *************************************************************************** TASK [Gathering Facts] **************************************************************************** ok: [demo.example.com] TASK [change password] **************************************************************************** changed: [demo.example.com] PLAY RECAP **************************************************************************************** demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 ``` Verification You can verify the password change by attempting to SSH into the system with the updated credentials: ```bash $ sshpass -p 'password' example@demo.example.com ``` **Note:** Ensure that `sshpass` is installed on the system for this verification step. Conclusion In conclusion, you now possess the knowledge to change a user password on a Linux system using Ansible. The `ansible.builtin.user` module simplifies this task, allowing for seamless automation of user account management. Set User Password Ansible requires **hashed** passwords (not plaintext): `

About This Tutorial

Read the full written article: ansible.builtin.user: Change User Password with Ansible (Secure Guide)

Topics Covered

Related Video Tutorials