Skip to content

Instantly share code, notes, and snippets.

@ianling
Created December 10, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianling/13782cd2293a1ea08035 to your computer and use it in GitHub Desktop.
Save ianling/13782cd2293a1ea08035 to your computer and use it in GitHub Desktop.
Sets the root password of a remote host to a randomly generated one. The new password gets stored locally in plaintext at /backup/passwords/<hostname>
---
- name: Change the root password and store the updated password locally in /backup/passwords/<hostname>
hosts: all
remote_user: backup
vars:
plaintextpw: "{{ lookup('password', '/backup/passwords/'+inventory_hostname+' chars=ascii_letters,digits,hexdigits,punctuation,, length=20') }}"
tasks:
- name: Remove the old password file, if it exists
local_action: file path=/backup/passwords/{{ inventory_hostname }} state=absent
- name: Generate the hashed password from plaintextpw
local_action: command /usr/bin/mkpasswd --method=SHA-512 {{ plaintextpw }}
register: mkpasswd_output
- name: Change the root user's password
user: name=root
password={{ mkpasswd_output.stdout }}
update_password=always
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment