ansible批量修改root爲隨機密碼

直接利用ansible-playbook實現,首先能免密登陸每個節點,可以執行ansible all -m ping測試,然後執行下面playbook。

[root@localhost ansible]# vim passwd.yaml

- hosts: test
remote_user: root
tasks:
- name: Generate the password
shell: openssl rand -base64 12 | cut -b 1-8  > ~/.openssl
- name: modify passwd
shell: echo `cat ~/.openssl` | passwd --stdin root
- name: fetch file
fetch:
src: ~/.openssl
dest: ~/fetch/openssl-{{ inventory_hostname }}
flat: yes
- name: delete pass file
shell: rm -rf ~/.openssl
- hosts: '{{hosts}}'
remote_user: root
tasks:
- name:
shell: for i in `ls ~/fetch`;do echo -e ${i##openssl-}\|`cat ~/fetch/$i` >> ~/fetch/.`date +%F-%R`_passwd;done

2、執行

[root@localhost ansible]# ansible-playbook passwd.yaml -e "hosts=192.168.0.50"
#hosts爲你ansible-server端地址

3、查看修改的密碼

[root@localhost ansible]# cat ~/fetch/.2018-07-31-13\:47_passwd

192.168.0.37|S2oeLoaa
192.168.0.50|zPaV75Tt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章