自己寫的playbook批量修改密碼並推送公鑰,實現免密登錄!

自己寫的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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章