自己寫的playbook批量修改密碼並推送公鑰,實現免密登錄!
由於工作需要需要寫這個東西,就把這個記錄下來了。
第一步:使用 ssh-keygen -t rsa生成密鑰對。
第二步:添加ansible hosts,我這個是test代替。
第三步:確定服務器上sshd_config文件裏的root登錄權限是否開啓。
實例代碼:
- hosts: test
sudo: yes
tasks:- name: 修改sshd_config文件
shell: sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
notify: restart sshd - name: 修改root密碼
shell: echo '密碼' | passwd --stdin root - name: 分發公鑰
authorized_key:
user: root
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
state: present
exclusive: no
handlers: - name: restart sshd
shell: service sshd restart
- name: 修改sshd_config文件