|
|
楼主 |
发表于 2020-11-3 22:13:12
|
显示全部楼层
cat hosts_pwd * L3 v z7 \/ h$ A
# 未分组机器,放在所有组前面
& ~9 h3 j. F+ `8 C& J4 n# 默认端口22,可省略4 Z0 X' X1 R0 i1 c3 ^
# 方式1:主机 + 端口 + 密码
( l4 [% O5 @4 c8 z: ?7 F5 t5 t, n5 X172.16.1.180 ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
4 P' N$ h5 P! `4 e4 H! |: h: J9 G8 s% i8 v# Q
# 方式2:主机 + 端口 + 密码
# ?6 z/ C, `; g( A: T! v5 _5 f[proxyservers]
$ \; f: u# A; E5 T( E' X172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
, @8 W j# B2 y' j/ T9 W! L7 x) I. s
# 方式3:主机 + 端口 + 密码
6 M5 S& v5 a, Y$ W[webservers]
! o& O; U. @8 a9 ^172.16.1.18[3:5] ansible_ssh_port=22 ansible_ssh_user=yun
! z! Y S2 }/ T" \[webservers:vars]/ u1 Z* U/ q: }1 ]6 o' C1 a$ P
ansible_ssh_pass='123456'0 S7 ~% r/ c( a) Q) U1 @
k7 x# F* x3 p7 j. y M H
" E7 E4 o. s3 }( T/ J0 a
2 S. z) r& J6 {3 g% R( c& c/ N/ H0 Z$ f
sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行
- L9 i! N: k7 v Z$ ]4 `* g172.16.1.180 | FAILED! => {9 P- v/ s# s( _# R/ F7 f4 k' X
"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."
! l+ x( C: i" F% ]7 i}5 Y( Y7 r/ A9 j# S& b) k
8 w0 H/ o; w& ~7 K8 K7 w* [: P; L4 |7 g
大概提示信息:因为启用了主机密钥检查,而 sshpass 不支持这一点。请将此主机「172.16.1.180」的指纹添加到你本机的known_hosts文件中以管理此主机。3 ^* b2 }, T' V# q; [7 H0 Y0 X
' Z) `6 \/ ?; M- h8 v# q, K
+ Y* X: B3 ]1 }+ p( O跳过主机密钥检查,有两种方式:方式1:修改 Linux 系统配置 vim /etc/ssh/ssh_config % j b& ~/ i* j0 e7 C# t
# AddressFamily any1 y1 E: o1 l/ X8 q
# ConnectTimeout 04 H4 c) A% e) G' Z
# StrictHostKeyChecking ask # 将该配置的注释打开,并改为 StrictHostKeyChecking no 这样针对所有用户都不会在进行 「主机密钥检查」了
+ R/ A( w( T# M, M; E# IdentityFile ~/.ssh/identity! ^; m+ X" z+ O' P$ f$ W, L; g9 O
! V, n4 e6 x! r" x, W5 w4 r, I/ a8 ]; V0 u) v; J/ V W
4 Q }% h. H# [8 g
9 m5 M, `6 {% h' w5 p8 [2 Y8 o9 R8 T- }* h' K& D+ Y3 U
但是这个是 Linux 自带的配置,我们不能随意去更改。因此不建议如此操作。
3 h* a% ]* i# v9 U5 L
5 G4 ^: S% g( G, ^5 F6 D7 J% l$ e2 E& M5 k' E: R; t0 t
方式2:修改 ansible 配置) R! B, `! _8 X+ T. P
( _6 \7 I. B! _ w6 D0 z2 A$ c: e1 l
vim /etc/ansible/ansible.cfg
* `+ ]& h% K& J; v3 Y" d4 G! d# O# uncomment this to disable SSH key host checking
& S" n$ i& t1 Z/ ~0 x! o/ Chost_key_checking = False # 将该配置的注释去掉
5 }4 ^$ W5 j# ^" @2 W' x6 V, o4 H! x7 [! U
& v# v- U* p, H& I改配置仅对 root 用户生效,其他普通用户是不生效的。这里使用该方法。
7 f/ L6 m9 D8 h3 y4 T5 V- r E1 n0 c再次连接测试$ ansible 172.16.1.180 -m ping -i ./hosts_pwd # 普通用户还是不行
1 R# \" Y( E9 i' g. M/ _172.16.1.180 | FAILED! => {
* v$ ^! W% b" H0 t* Y' p. 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." t4 G3 J0 W4 r9 ~
}
" `' q2 _ `+ s/ \; {5 x$ sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行 w. `5 R) k# W3 H# W. k5 x R
172.16.1.180 | SUCCESS => {+ _& k$ C: R2 T C
"ansible_facts": {
7 W0 a4 }* \( ~* X "discovered_interpreter_python": "/usr/bin/python"
5 h. s: O6 B# T, t }, X( j7 P5 |; p) Z+ }. N2 W
"changed": false,
- H" Q4 f( n) c7 X/ c1 N "ping": "pong"0 v; X3 N6 N9 L. j
} |
|