- 积分
- 16843
在线时间 小时
最后登录1970-1-1
|

楼主 |
发表于 2020-11-3 22:13:12
|
显示全部楼层
cat hosts_pwd
; h! V0 K. r2 M3 u# 未分组机器,放在所有组前面& K; K) K7 Z& R0 c
# 默认端口22,可省略7 k; j6 t( t* k, O* V
# 方式1:主机 + 端口 + 密码$ n- T% E2 Q0 k! ?, F( a( k
172.16.1.180 ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'" y2 J: d4 m+ ~
$ a {8 t: U @& k/ u
# 方式2:主机 + 端口 + 密码
4 A u+ z1 r; b! i% G+ _/ s" y[proxyservers]% I+ l& G% n0 y* ~. b( q! ]) M
172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'& G2 @; I& t8 B2 F! t y
0 _) F/ A8 W# _# O
# 方式3:主机 + 端口 + 密码+ a1 r: R: ~! k
[webservers]% ~1 ^% [; s3 O/ z" f2 _# |
172.16.1.18[3:5] ansible_ssh_port=22 ansible_ssh_user=yun
" @1 Z, E$ E2 E1 | R; H8 m[webservers:vars]8 Q" f& N. [) W8 G0 A V* F1 u
ansible_ssh_pass='123456'+ H7 P5 G+ o, O* p) d7 w
% ^) P% t6 p: J$ f/ s
0 ^0 U5 Y( u; y* c% D9 ?
& U( O, R A. m# ?
4 W& f: W( d1 P5 Nsudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行
3 w" P! b2 q" h5 ]172.16.1.180 | FAILED! => {7 s# |9 q S/ X& X! M
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
0 k& n7 f- C2 N& C4 K) v}
6 N3 Y1 [ K. s# G$ J& d
4 W; d. s! I* N/ |8 c3 F; |
. Q! `/ N% G1 O' y大概提示信息:因为启用了主机密钥检查,而 sshpass 不支持这一点。请将此主机「172.16.1.180」的指纹添加到你本机的known_hosts文件中以管理此主机。
! v$ `# k2 B' ?/ T* b
- C# K* r: _! ^2 _8 U, k: m9 c( W, o1 q
跳过主机密钥检查,有两种方式:方式1:修改 Linux 系统配置 vim /etc/ssh/ssh_config
: h& n! r- ]) \2 |: s, D8 ]* U# AddressFamily any( ] d' y! [6 v9 O( c3 A
# ConnectTimeout 0: \3 L9 o& Q/ M/ I8 Q/ n! ~
# StrictHostKeyChecking ask # 将该配置的注释打开,并改为 StrictHostKeyChecking no 这样针对所有用户都不会在进行 「主机密钥检查」了! X( v% g: {: C' S1 G
# IdentityFile ~/.ssh/identity m$ h( s |( H0 G* k( G
( P p( d) e; C) l8 A
# s, h) r. a$ ]% l6 ^" `5 N7 q: y7 w
6 {8 T3 U+ o7 D U8 O9 ]( H; y: h* v. k6 d( J
但是这个是 Linux 自带的配置,我们不能随意去更改。因此不建议如此操作。
8 [: p4 N( r/ x/ r6 r+ ~
8 T2 Y$ z6 k6 v0 k' S5 a6 Q: X) T3 h7 Y+ s
方式2:修改 ansible 配置
6 q" g' i: ^7 v# ^
7 _7 k2 u T! f1 v3 L: F! n8 cvim /etc/ansible/ansible.cfg
0 O& b; Q; j( \0 d6 @ b, Y& }# uncomment this to disable SSH key host checking
5 u5 B+ `2 P# q/ J+ Khost_key_checking = False # 将该配置的注释去掉
* Q5 a+ b: U5 Q, b( v
6 P9 O6 |1 A$ x$ b$ s+ i2 o; |/ ^4 z' |& K7 M8 d( C
改配置仅对 root 用户生效,其他普通用户是不生效的。这里使用该方法。" L. F ~( p* Q8 T
再次连接测试$ ansible 172.16.1.180 -m ping -i ./hosts_pwd # 普通用户还是不行2 G* g+ V1 v/ p5 _. J5 ^
172.16.1.180 | FAILED! => {
" ?1 k+ |/ P( f% K$ _1 g0 ~& s' z+ ^ "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
+ ~5 {& ~. d* Z}& y9 e0 J2 Y5 U8 P
$ sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行
, N1 ~7 f% i1 v* n3 z- h. H8 Y1 m* K172.16.1.180 | SUCCESS => {
4 P% o5 w4 v n ~ "ansible_facts": {
8 Z5 b7 P% L' s+ z( u& a; N "discovered_interpreter_python": "/usr/bin/python"
3 n7 n+ H+ a" Q! ~7 [6 P },
3 W3 w& d% V% G! T) [+ x "changed": false,
* ?# s6 |; W0 t5 [" [2 N7 G "ping": "pong"
; I* W7 @5 |+ n. C7 M5 ]- f} |
|