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

楼主 |
发表于 2020-11-3 22:13:12
|
显示全部楼层
cat hosts_pwd
" d9 V/ @' K7 Z5 N: P# 未分组机器,放在所有组前面6 ]& L; n9 Y `; m
# 默认端口22,可省略3 V( g+ x5 o3 E4 V3 B$ O
# 方式1:主机 + 端口 + 密码7 d, \9 m4 e" j. j2 L/ Z
172.16.1.180 ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
0 \ P2 V6 X: K& C u, L
. g" X" T$ G7 X# 方式2:主机 + 端口 + 密码
( o7 K& v& s" G, s[proxyservers]
2 p1 E) i8 |+ R( O v172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'2 V! \& |# |! Z. h7 f. m
4 A( g# ?* B4 y3 V5 s2 ?! ~/ n
# 方式3:主机 + 端口 + 密码8 g/ v" Z; o3 [5 T. y u. H6 _
[webservers]1 }& B" V$ d0 ]7 |& W
172.16.1.18[3:5] ansible_ssh_port=22 ansible_ssh_user=yun
' @; F$ U: e4 N3 j[webservers:vars]
, g* h/ T+ m0 F" {, W: Qansible_ssh_pass='123456'
2 D% f1 Z9 A: N" E0 `- r/ O8 N4 l1 e" E8 u/ |
8 e' s j$ k( @( Y
: ?; \8 E3 V/ ~2 y' f4 S# Y9 N4 Y* V" x- V. \
sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行
' N2 a4 ]3 t7 L% q172.16.1.180 | FAILED! => {3 x s/ Q0 C; r$ R. ]$ C2 I2 [! j
"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."1 I( y! S3 S! c0 `- ?' d/ W! [
}
" Z( M8 d2 R$ X6 @* D3 R0 u5 f# `2 d' H" d- @
- e/ e! {; v9 g# c大概提示信息:因为启用了主机密钥检查,而 sshpass 不支持这一点。请将此主机「172.16.1.180」的指纹添加到你本机的known_hosts文件中以管理此主机。
0 h; [5 W8 V5 R* f
$ A+ `2 u& n( T9 Q1 W
6 B T% ^+ {% ~% m, Q跳过主机密钥检查,有两种方式:方式1:修改 Linux 系统配置 vim /etc/ssh/ssh_config
' I9 z5 R; V6 T) B% V1 i* v% U# AddressFamily any
# A2 x4 X. Z0 Q' j& \) {2 \# ConnectTimeout 0 b: s" k( b* D2 Z
# StrictHostKeyChecking ask # 将该配置的注释打开,并改为 StrictHostKeyChecking no 这样针对所有用户都不会在进行 「主机密钥检查」了# T# K$ d& l* y. Z
# IdentityFile ~/.ssh/identity
8 u ]# b: F6 i, r Q
! K. s( W; F2 |
6 h& ?" p0 J6 a- t$ ^ }+ d+ v; F/ d- ` t
- k1 l' P! J2 d; C
2 _( g1 T& w* h5 G- q
但是这个是 Linux 自带的配置,我们不能随意去更改。因此不建议如此操作。
: e% @4 R* I) ?6 C$ E' ~- {- K. `. d0 y3 t; E
: w, i" N2 I- N- O0 P( t' M方式2:修改 ansible 配置7 `, s7 r) L! {( ]3 ?+ V1 K
) p: Q3 _( v) Q' { p. Q2 ?! Bvim /etc/ansible/ansible.cfg
5 m$ [' s% p( m3 K) b& e, E% W# uncomment this to disable SSH key host checking
/ A- F# A. @" bhost_key_checking = False # 将该配置的注释去掉
2 D& J) G7 N4 C1 ?* A4 l; W" x2 T- @" a4 h& q
; w" @- r$ [ Y% ~: M1 n% ?1 o
改配置仅对 root 用户生效,其他普通用户是不生效的。这里使用该方法。
3 X" O x6 G; k( d$ K2 k再次连接测试$ ansible 172.16.1.180 -m ping -i ./hosts_pwd # 普通用户还是不行$ e, d- u8 C. j! E! E0 q
172.16.1.180 | FAILED! => {
* Y* B; z: g7 d9 ]/ W "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."+ t7 f8 g' I- A, q3 N$ H- L/ L; j
} C# w' @: I9 ` \0 m& Q( H
$ sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd # 提权使用 root 用户执行
2 _' }4 v2 o! ~* K172.16.1.180 | SUCCESS => {! ` l2 f, w4 m* a8 T; W+ c
"ansible_facts": {
|) W* I7 d* V8 }5 ] "discovered_interpreter_python": "/usr/bin/python"( T; G: r" i8 j, H% u; f0 J, m+ M/ N
}, , i# ~ t8 Q E7 p( N
"changed": false,
3 R8 f3 P6 M0 D1 M "ping": "pong"
* F% L2 g7 d- I2 p5 W} |
|