0%

Ansible-快速添加免密码认证

Ansible使用小技巧

生成管理机的私钥和公钥

1
ssh-keygen -t rsa -b 2048 -P '' -f /root/.ssh/id_rsa

添加主机信息到主机清单中

1
vim /etc/ansible/hosts
1
2
3
4
5
6
7
8
9
10
11
12
#添加分组
[test-k8s]
172.20.21.249
172.20.21.250
172.20.21.251
172.20.21.252
172.20.21.253
172.20.21.254
172.20.21.255
172.20.22.0
172.20.22.1
172.20.22.2

配置playbook

1
vim /etc/ansible/ssh-addkey.yml
1
2
3
4
5
6
7
8
9
10
---
- hosts: pro-k8s
gather_facts: no

tasks:

- name: install ssh key
authorized_key: user=root
key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
state=present

运行playbook

1
ansible-playbook -i /etc/ansible/hosts -k /etc/ansible/ssh-addkey.yml

输入服务器密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
SSH password:

PLAY [pro-k8s] ***************************************************************************************************************************

TASK [install ssh key] *******************************************************************************************************************
changed: [172.21.5.197]
changed: [172.21.5.196]
changed: [172.21.5.199]
changed: [172.21.5.192]
changed: [172.21.5.195]
changed: [172.21.5.193]
changed: [172.21.2.240]
changed: [172.21.5.198]
changed: [172.21.2.238]
changed: [172.21.5.194]
changed: [172.21.2.236]
changed: [172.21.2.235]
changed: [172.21.2.237]
changed: [172.21.2.239]
changed: [172.21.2.234]
changed: [172.21.0.159]
changed: [172.21.0.155]
changed: [172.21.2.241]
changed: [172.21.0.154]
changed: [172.21.0.153]
changed: [172.21.0.158]
changed: [172.21.0.152]
changed: [172.21.0.160]
changed: [172.21.0.156]
changed: [172.21.0.157]
changed: [172.21.2.232]
changed: [172.21.2.233]
changed: [172.21.0.151]
changed: [172.21.5.191]

PLAY RECAP *******************************************************************************************************************************
172.21.0.151 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.152 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.153 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.154 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.155 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.156 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.157 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.158 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.159 : ok=1 changed=1 unreachable=0 failed=0
172.21.0.160 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.232 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.233 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.234 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.235 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.236 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.237 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.238 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.239 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.240 : ok=1 changed=1 unreachable=0 failed=0
172.21.2.241 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.191 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.192 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.193 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.194 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.195 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.196 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.197 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.198 : ok=1 changed=1 unreachable=0 failed=0
172.21.5.199 : ok=1 changed=1 unreachable=0 failed=0