# root user password needs to set for many servers however the password should be different password, not to be same

**URL:** <https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299>\
**Category:** Ansible Project\
**Created:** [September 17, 2019, 4:36pm UTC](https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299 "2019-09-17T16:36:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [September 17, 2019, 4:36pm UTC](https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299/1 "2019-09-17T16:36:24Z")

</div>

Hi,

manually login to each servers and setting root password, login to server1, set password “password123” ; then login to server2 set password “redhat123” like this i’m looking for ansible playbook, where i can automate for 100+servers.

Idea looking forward:-  
1, Random password needs to be generated.  
2, on each server, root user password should be reset by picking up from random password.

Note:- For security reason, we are resetting root password on monthly basis and those password should be generated randomly and reset.

Thanks,  
Deepan M

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [September 17, 2019, 7:27pm UTC](https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299/2 "2019-09-17T19:27:48Z")

</div>

If you’re ok with Ansible generating the password for you then storing it on the machine you ran the playbook from, then the `password` plugin might help a bit.

Assuming you have an inventory of servers and you’re OK with saving the latest password to “/tmp/root.password.hostname.txt”, I believe something like this will do what you’re looking for:

```auto
- name: Force new root password
  user:
    name: root
    password: "{{ lookup('password', '/tmp/root.password.{{ inventory_hostname }}.txt length=60 chars=ascii_letters,digits,punctuation') | password_hash('sha512', 1000000 | random(seed=inventory_hostname) | string ) }}"
    update_password: always

```

This will generate a random password of ASCII letters, digits and punctuation, the password will be 60 characters long, and the plain-text of it will be stored in /tmp/root.password.{hostname}.txt for each system.

The “password\_hash()” modifier on the “password:” line hashes the password so the “user:” module can use it. It also assumes that the system getting the new password can handle SHA512 passwords. It also uses the “inventory\_hostname” to ensure that the hashed password is idempotent between runs. The “1000000|…|string” uses the name of the system being worked on as a random seed and picks a pseudo-random value to use for the password hash.

NOTE: The first time this is run, the /tmp/root.password.{hostname}.txt file is created and used. The next time you run it, since that file exists it will re-use that raw password and not change it. To change the root password of that server, either delete the file and a new random password will be assigned, or create your own password and put it in this file.

---

<div class="post-metadata">

**Author:** ![Deepan\_M](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Deepan\_M](https://forum.ansible.com/u/Deepan_M)\
**Post date:** [September 18, 2019, 4:18pm UTC](https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299/3 "2019-09-18T16:18:03Z")

</div>

Thank you Dan Linder, playbook is working perfect.

Regards,  
Deepan M

---

<div class="post-metadata">

**Author:** ![dan\_linder](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dan_linder/32/3833_2.png) [@dan\_linder](https://forum.ansible.com/u/dan_linder)\
**Post date:** [September 18, 2019, 4:41pm UTC](https://forum.ansible.com/t/root-user-password-needs-to-set-for-many-servers-however-the-password-should-be-different-password-not-to-be-same/31299/4 "2019-09-18T16:41:59Z")

</div>

Great to hear! Thanks for the feedback.
