解决ansible批量加入新IP涉及known_hosts报错的问题

我们把一批新的IP加入到ansible的hosts文件,比如/etc/ansible/hosts,往往会有这样的提示,

因为本机的~/.ssh/known_hosts文件中并有fingerprint key串,使用ssh连接目标主机时,一般会提示是否将key字符串加入到~/.ssh/known_hosts文件中,若输入yes,则本机以后再次使用ssh连接目标主机时,不会弹出确认。于是,我们得一个个ssh目标主机,输入一个个yes,非常繁琐,这里提供一个批量操作的脚本。

bash 复制代码
yum -y install expect

vim group.exp

bash 复制代码
#!/usr/bin/expect
set timeout 2
spawn ssh -p 22 user@[lindex $argv 0]
expect {
  "sure" { send "yes\r"; exp_continue }
  "*#" { send "\r" }
}

然后 chmod a+x group.exp

bash 复制代码
for ip in `cat iplist`;do ./group.exp $ip;done

就可以了

相关推荐
江华森4 小时前
Ansible 自动化运维:从入门到实战
运维·自动化·ansible
JackSparrow4141 天前
使用Ansible批量管理+更新产品环境服务器配置
运维·服务器·ci/cd·kubernetes·自动化·ansible·sre
Cat_Rocky4 天前
Linux-ansible之Playbook简单应用
linux·网络·ansible
淼淼爱喝水5 天前
Ansible 入门实战:四种变量优先级比较实验
ansible·变量
淼淼爱喝水5 天前
ansible实战:「磁盘空间 ≥ 2G 时自动创建文件」
前端·ansible·自动创建
红茶要加冰7 天前
四、ansible的templates
linux·运维·服务器·ansible
红茶要加冰7 天前
五、ansible的流程控制
linux·运维·服务器·ansible
Cat_Rocky7 天前
Linux学习-ansible自动化
linux·学习·ansible
大白菜和MySQL8 天前
ansible使用简记
ansible
灰色人生qwer8 天前
入门 Ansible
ansible·ansible tower