Batch setup of random passwords via ansible

Source: Internet
Author: User
Tags base64 openssl rand

Requirements:
Batch Modify the server's account (assuming root account) password, with a randomly generated string to do the password

Environment:
Assume that the target group for the server in the/etc/ansible/hosts file is as follows

[Target-servers]
192.168.0.[1:100]
Perform Actions

1 generate random strings for candidate password characters on each remote server using the OpenSSL rand command

Ansible target-servers-m shell-a "OpenSSL rand-base64 > ~/.openssl"

You can view the results of a build with the following command

Ansible target-servers-m shell-a "Cat ~/.openssl"

2 setting a password with a candidate string
The above OpenSSL rand-base64 12 command generates a 16-bit random string, for example, taking the first 12 bits as the password, can be implemented by the following set-pass.sh script:

#!/bin/bash
pass= ' cat ~/.openssl '
subpass= ' echo ${pass:0:12} '
echo ' root: $SUBPASS ' | chpasswd

Put the above script on the Ansible console, through the script module can be executed on the remote server, the simple playbook as follows SET-PASS.YML:

---
-hosts: ' {{myhosts}} '
  remote_user:admin
  tasks:
  -Name:set passwd
    script: ~/set-pass.sh 
    Become:true

The bulk execution action is:

Ansible-playbook set-pass.yml-e "Myhosts=target-servers"

After execution, the password is set to complete.

3. Take the string files from each server to the central control machine
You can get the files on the remote server through the Ansible fetch module, the simple playbook is as follows FETCH-FILE.YML:

---
-hosts: ' {{myhosts}} '
  remote_user:admin
  tasks:
  -name:fetch file
    fetch:
      src: ~/. OpenSSL
      dest: ~/fetch/openssl-{{inventory_hostname}}
      Flat:yes

The bulk execution action is:

Ansible-playbook fetch-file.yml-e "Myhosts=target-servers"

After execution, you can see the obtained file in the ~/fetch/directory, the contents of which is the password string generated above

Finally, you can delete the string file on the remote server:

Ansible target-servers-m shell-a "Rm-f ~/.openssl"

All of the above actions can be summarized into a playbook inside.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.