How to Update User Password with Ansible

Photo by Alek Kalinowski on Unsplash

It’s very easy to change a user’s password with Ansible. Here I included 2 ways for your perusal: one with Ansible Playbook, and another one directly via command line.

Before we proceed, since Ansible does not allow us to pass a cleartext password through the User module, we’ll need to leverage on a password hashing library in Python.

pip install passlib

Update User Password with Ansible Playbook

Setup your playbook as followed. Let’s call it change-password.yml.

This playbook aims to update the password for the user admin across all hosts.

---
- hosts: all
become: yes
tasks:
- name: Change user password
user:
name: admin
update_password: always
password: "{{ newpassword|password_hash('sha512') }}"

To run this playbook, run the command as below. This will input the newpassword variable that will be used by our playbook.

ansible-playbook -i hostfile change-password.yml --extra-vars newpassword=12345678

Update User Password with Ansible from Command Line

Alternatively, if you prefer to run it only one time from command line directly, here’s the command that will do the same thing as the playbook above:

ansible -i hostfile all -m user -a "name=admin update_password=always password={{ newpassword|password_hash('sha512') }}" -b --extra-vars "newpassword=12345678"

Personally, I prefer the playbook approach as I could then commit to Github for future reuse.

I write about Laravel, PHP, and web development related articles.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

This is just testing!

Why String is immutable (Python)

Infrastructure as Code — Quickest Overview Possible

13 Side Hustle Ideas Programmers Can Do to Make More Money

Ansible roles for load balancer and apache webserver

PlatON Lectures: Online Open Class on PlatON Smart Contract (I)

COMBO (COMB) will be available on Pool-X BurningDrop

Configuring SSL for a Hostgator Domain with Heroku Hosting

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jian Jye

Jian Jye

I write about Laravel, PHP, and web development related articles.

More from Medium

Publishing Jenkins Job build status to GitLab

Automating A Virtual Fabric with Ansible, Terraform, GNS3 and FRR

Jenkins Vs Teamcity CI Tool

CI/CD with Gitlab-CI