直接利用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