步骤
通过SSH远程登录其它Linux服务器时,一般需要输入用户口令才能进入,也可以通过配置密钥的方式实现SSH的免密码登录,具体步骤如下:
- 在本地生成一对公私钥匙对;
- 私钥放在本地(SSH客户端)~/.ssh/id_rsa,权限为0600;
- 公钥放在远端(SSH服务端)~/.ssh/authorized_keys文件内。
配置免密码登录的示例
编辑文件inventory
#file:inventory
node1 ansible_ssh_host=192.168.56.101 ansible_ssh_port=22 ansible_ssh_pass=*****
node2 ansible_ssh_host=192.168.56.102 ansible_ssh_port=22 ansible_ssh_pass=*****
[cluster]
node[1:2]
#部署节点,执行playbook的节点
[deploy]
node1
编辑文件set_nopasslogin.yml
---
#file:set_nopasslogin.yml
- hosts: deploy
tasks:
- name: Create SSH Directory
file:
path: ~/.ssh
state: directory
- name: Create Openssh Keypair
openssh_keypair:
path: ~/.ssh/id_rsa
type: rsa
- hosts: cluster
tasks:
- name: Create SSH Directory
file:
path: ~/.ssh
state: directory
- name: Config Private Keys
copy:
dest: ~/.ssh/id_rsa
src: ~/.ssh/id_rsa
mode: 0600
- name: Config Public Keys
copy:
dest: ~/.ssh/authorized_keys
src: ~/.ssh/id_rsa.pub
网友评论