解决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

就可以了

相关推荐
亚空间仓鼠2 天前
Ansible之Playbook(一):简单应用
ansible
亚空间仓鼠2 天前
Ansible之Playbook(六):实例部署实战
linux·网络·ansible
小梦爱安全2 天前
ansible基础配置和ansible模块
运维·自动化·ansible
亚空间仓鼠4 天前
Ansible之Playbook(三):变量应用
java·前端·ansible
亚空间仓鼠4 天前
Ansible之Playbook(二):模块大全
ansible
亚空间仓鼠4 天前
Ansible之Playbook(四):循环与判断
java·服务器·ansible
Harvy_没救了4 天前
Ansible 学习指南
linux·运维·服务器·ansible
TechWJ5 天前
Ansible 跨千台机器批量部署,Node Exporter 监控实现 Prometheus 统一采集
ansible·prometheus
淼淼爱喝水5 天前
Ansible 批量运维实战:openEuler 环境一键安装 httpd 服务
运维·ansible
wwj888wwj5 天前
Ansible基础(复习3)
linux·运维·服务器·git·ansible