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

就可以了

相关推荐
我爱学习好爱好爱7 小时前
Ansible 常用模块详解:cron、archive、unarchive实战
linux·服务器·ansible
我爱学习好爱好爱13 小时前
Ansible 详解:group模块、vars_files变量、user模块实战
linux·运维·ansible
我爱学习好爱好爱14 小时前
Ansible 自动化部署全栈项目(Spring Boot + Vue + MySQL + Redis)实战(Rockylinux9.6)
spring boot·自动化·ansible
我爱学习好爱好爱1 天前
Ansible 常用模块详解:lineinfile、replace、get_url实战
linux·python·ansible
我爱学习好爱好爱1 天前
Ansible 环境搭建
linux·运维·ansible
淼淼爱喝水1 天前
openEuler 系统下 Ansible 一键安装教程(保姆级)
运维·ansible·openeuler
我爱学习好爱好爱2 天前
Ansible 常用模块详解:hostname、selinux 、file实战
前端·chrome·ansible
我爱学习好爱好爱2 天前
Ansible 常用模块详解:yum、service/systemd、copy实战
linux·服务器·ansible
淼淼爱喝水2 天前
Ansible 配置与环境搭建超全教程(自动化运维基础)
运维·自动化·ansible
我爱学习好爱好爱3 天前
Ansible 入门:ad-hoc 临时命令与常用模块
linux·服务器·ansible