--- - hosts: "{{ hosts }}" vars: ansible_ssh_user: root ansible_ssh_pass: "{{ lookup('password', 'passwords/' + inventory_hostname + '.pwd') }}" tasks: - debug: msg="the variable value of inventory_hostname is {{ inventory_hostname }}" - name: Add SSH public key to target server authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
在執行的目錄結構以下python
[root@mesos-master-33128 passwd]# ls change_passwd.yml hosts passwd.py passwords push_authorized_key.yml [root@mesos-master-33128 passwd]# ls passwords/ 10.1.33.131.pwd [root@mesos-master-33128 passwd]# cat passwords/10.1.33.131.pwd ******** 文件裏的內容就是須要鏈接客戶端的密碼