找回密码
 注册
查看: 3250|回复: 3

ansible加上-i参数,使用文件执行命令

[复制链接]

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
发表于 2020-11-3 22:07:50 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 2 金钱 才能浏览

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 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 K
7 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 D
7 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
}

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-11-3 22:14:54 | 显示全部楼层
cat hosts_group
+ C2 O  f( x0 Z+ N: _# 未分组机器,放在所有组前面
" E; ^! W& u, M4 Z/ t# N4 N+ e% Y1 w# 默认端口22,可省略
9 S' W$ X! q$ |" s# 方式1、主机 + 端口 + 密钥
5 t0 ]. D' X+ N! ^$ A8 F" y3 F172.16.1.180
% n  t1 ]* a. ^) B7 T! `$ ~
' i1 S1 W, U1 T, G. V7 y# 方式一、主机组变量 + 主机 + 密码
$ k  D( @6 B1 l0 O! i: r[proxyservers]& I; p# u4 t4 _2 W& `
172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
2 D, q+ D3 t2 N" q% a9 F
5 V% \$ i8 Q: c" V6 N5 @! }& ~# 方式二、主机组变量 + 主机 + 密钥
$ m/ B& j) n* E* F- D( V; f4 g[webservers]
9 E/ a( \/ U0 s5 |* ]$ N3 ^5 W172.16.1.18[3:5]:22
0 Q- `# @5 k1 i% B. {4 X& a) }! {2 i: I$ V1 u- w0 `
# 定义多组,多组汇总整合) @3 h" {% Y% C! A4 G; @. L
# website 组包括两个子组[proxyservers, webservers]; M- y* q0 G# [% {* G
[website:children]
0 z( m9 S8 m' c7 {5 \8 Z4 pproxyservers" ~" g) d) G3 x+ n
webservers

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-11-3 22:15:24 | 显示全部楼层
$ sudo ansible proxyservers -m ping -i ./hosts_group   " V4 @1 i- O# i
172.16.1.182 | SUCCESS => {( ?% c1 O1 B( n
    "ansible_facts": {- {+ ^  }6 O% K4 b! o) d. X) H
        "discovered_interpreter_python": "/usr/bin/python"4 T; A0 ~6 `8 u4 j1 I5 C
    }, 6 H( D/ ~% v: N0 f
    "changed": false,
3 B4 l0 j: f" |6 r2 P/ u4 p, |; w    "ping": "pong"7 o" L1 o( v8 M$ j/ t- s) |
}% I& `% E+ U7 X) }
172.16.1.181 | SUCCESS => {
+ v1 S+ L1 C% D" n- T    "ansible_facts": {
/ Q, S0 K- g9 S* U. O        "discovered_interpreter_python": "/usr/bin/python"7 f9 c* Y+ _+ l6 t0 B( h3 \
    },
$ m) X* x# ]0 y7 I: [7 V: a9 Z. M    "changed": false,
! Y* E: N0 n3 M$ P    "ping": "pong"3 Q* N- ]2 M+ J( _$ ?3 k1 v, v* v4 b
}

5 i: {- c6 c/ ?! K( y
您需要登录后才可以回帖 登录 | 注册

本版积分规则

返回首页|Archiver|手机版|小黑屋|易陆发现技术论坛 ( 蜀ICP备2026014127号-1 )

GMT+8, 2026-6-12 04:29 , Processed in 0.021604 second(s), 25 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表