找回密码
 注册
查看: 3249|回复: 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 # I" @4 F0 K& F- T# F9 k. d
# 未分组机器,放在所有组前面
$ y0 A. ~2 c* {# 默认端口22,可省略( m: W8 n/ V- ^0 m
# 方式1:主机 + 端口 + 密码; v$ x, Q$ k+ Z( Y
172.16.1.180   ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
% G" z) J! g( t
# z6 Q1 d, e( y& v5 m+ a: O5 Z# 方式2:主机 + 端口 + 密码
) g7 t# B7 }0 D8 p  ]* U: c% R[proxyservers]5 T. Z  I' Q/ P( ^! ~8 o9 Y
172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'
" g. \" I* }3 G8 O( g
- i) l/ Z8 [5 H2 k' R# 方式3:主机 + 端口 + 密码
4 R# c5 [& Q6 m1 m% V2 w& a9 R[webservers]: X0 Y( u6 z# H# F
172.16.1.18[3:5] ansible_ssh_port=22 ansible_ssh_user=yun
) M+ s, y1 S" z8 K" B[webservers:vars]8 {* z# ?. ^: }3 ?. P# F0 c2 R! `
ansible_ssh_pass='123456'
* n1 b; H. |1 _, ]* a3 w3 [
2 f7 v# w: _$ c4 W+ ]
$ w  I  e# [, d& s

3 b5 j% c  C, Q* @3 w& B
$ V) t$ ~" a3 z9 U0 F+ H
sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd  # 提权使用 root 用户执行
& C/ x' T# ^0 h! u. d1 }172.16.1.180 | FAILED! => {3 I" m! p: u* C. }4 U" V
    "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."
! n: L8 V  @/ a3 n* H% _}

8 b; D7 L, \3 a1 f$ a; R% \
1 @' Y; u8 e) c0 S% ~1 h- N$ d
7 w3 M# K* z8 }( |' E% X" a
大概提示信息:因为启用了主机密钥检查,而 sshpass 不支持这一点。请将此主机「172.16.1.180」的指纹添加到你本机的known_hosts文件中以管理此主机。( r! {+ K+ h( A3 g

+ B: Y# [$ K" w9 P% c/ q$ ~
( I) \+ h6 [# z7 d
跳过主机密钥检查,有两种方式:

方式1:修改 Linux 系统配置

vim /etc/ssh/ssh_config ! _0 I0 P/ a+ k
#   AddressFamily any
4 _  P; R& g( p#   ConnectTimeout 0) K5 ?3 W4 ~: F! Y6 d; z7 Q+ R
#   StrictHostKeyChecking ask   # 将该配置的注释打开,并改为  StrictHostKeyChecking no  这样针对所有用户都不会在进行 「主机密钥检查」了
9 w( A$ i' Y4 H  V2 O9 o1 O3 }( H, j#   IdentityFile ~/.ssh/identity
7 x7 i' g" H  I
, V/ w. }- m6 I2 ^. J9 w5 ^0 }
' b/ S' K, U9 n/ y& T1 y
' `) a+ R$ b6 z* {( T, E5 l$ _$ Q
6 u: e. U6 V$ J5 [: D6 i& d7 r6 z' E2 m! |
但是这个是 Linux 自带的配置,我们不能随意去更改。因此不建议如此操作。: X$ l5 D" ~; V: J9 A* v" ~
' B* U4 W" o/ A$ d+ a

; Y* }. ?. n' F3 W方式2:修改 ansible 配置
/ D& S; P. z+ |  `- n
: A. l! A( O2 |' q3 G* t! l4 Xvim /etc/ansible/ansible.cfg$ h9 P* o! }* i. H5 c
# uncomment this to disable SSH key host checking
: \' p! i7 A' m  k6 W/ |# p! Y& ihost_key_checking = False    # 将该配置的注释去掉
; \" e. L, m* C8 T

' i# C" V, s% z  }7 k

5 H4 ~9 s9 V/ t改配置仅对 root 用户生效,其他普通用户是不生效的。这里使用该方法。
& M8 A5 {; S9 p$ q; P. @( f再次连接测试

$ ansible 172.16.1.180 -m ping -i ./hosts_pwd  # 普通用户还是不行" \; _  {$ f8 H, s  y& m1 q: d1 Q& s
172.16.1.180 | FAILED! => {( L  }' i7 u. L" j8 [2 A9 d
    "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.": h+ c/ e& D* n, H+ ~
}
6 U+ u( X6 Q2 N0 c% N- m$ sudo ansible 172.16.1.180 -m ping -i ./hosts_pwd  # 提权使用 root 用户执行
  Y8 E$ S5 J: {% S172.16.1.180 | SUCCESS => {2 a1 l7 `+ G# P6 o
    "ansible_facts": {6 c. _5 D5 g- B- v6 ^4 d0 i, p  E
        "discovered_interpreter_python": "/usr/bin/python"- u( ?$ e: i& v
    }, 7 x/ g3 q8 g8 o3 G# y0 e9 t
    "changed": false, ; z, S3 I; [# U2 \6 m' G% T8 @1 c! N
    "ping": "pong"
& S3 k$ o; ^2 e" M* ?}

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-11-3 22:14:54 | 显示全部楼层
cat hosts_group   p$ @/ ^2 B' a  P: [1 o
# 未分组机器,放在所有组前面6 R( ?0 c$ Z3 ~, e
# 默认端口22,可省略' S: h6 q$ U  \) {2 c
# 方式1、主机 + 端口 + 密钥
; Q3 {( m# Q4 P2 V172.16.1.180+ H- f/ B; ?9 h6 p3 S
3 N, W; a; ?! b
# 方式一、主机组变量 + 主机 + 密码
5 A/ \' {% O, K; ^* ~2 _% [[proxyservers]! E7 M( ^! L. g& _2 ?+ h' ^
172.16.1.18[1:2] ansible_ssh_port=22 ansible_ssh_user=yun ansible_ssh_pass='123456'/ |! p2 n  B0 n4 J6 L* l

- s4 y5 u, F% T* ]; S* n# 方式二、主机组变量 + 主机 + 密钥
0 Z( [% u" y( ^5 U& ?[webservers]0 _1 f+ ^" l6 @' e% o! F
172.16.1.18[3:5]:222 r  `5 E- U1 Q- x; D+ c2 w

' |: [% j( y- _' Y# 定义多组,多组汇总整合
2 p9 u! r$ |6 g" L. [# website 组包括两个子组[proxyservers, webservers]
: h# ?7 R. h& ~" m2 O9 f[website:children]
$ C% ?: Q- Z( R9 @6 tproxyservers6 B; O. ^! d6 Z; D* ~4 n
webservers

1

主题

0

回帖

12

积分

管理员

积分
12
QQ
 楼主| 发表于 2020-11-3 22:15:24 | 显示全部楼层
$ sudo ansible proxyservers -m ping -i ./hosts_group   4 z8 d8 b, b0 h
172.16.1.182 | SUCCESS => {
$ @3 }( X, @! M0 m1 A  u9 M    "ansible_facts": {
" e! S9 C" p5 ?. v        "discovered_interpreter_python": "/usr/bin/python"
0 b) n6 s, _. @5 e    },
3 u  E- A% ]: Y6 K% ^! g3 L    "changed": false,
) Z( X+ C3 {' W, l" u/ `2 e/ k7 f! v    "ping": "pong"
( k- K$ i6 U3 y- ^}
2 o9 J1 n5 \9 X, B7 q6 V; J$ R172.16.1.181 | SUCCESS => {# @. f- C1 A# T* q8 v) V$ F6 H
    "ansible_facts": {& G. a( z' H8 K5 B. V) V% B. p
        "discovered_interpreter_python": "/usr/bin/python"4 d9 P" ^9 v8 m5 Q3 z
    }, # z# G, ^4 B. b2 P: a5 U5 s
    "changed": false,
. L: x/ U" W3 L6 c    "ping": "pong"4 l8 Y2 L) y2 u8 A( P  B
}
) d9 v/ F  V( F( n/ ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-6-12 03:26 , Processed in 0.021542 second(s), 26 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

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