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.