背景:新开一批服务器,先需要用ansible主机对该批服务器质性初始化操作,首先需要分发秘钥
新服务器的密码统一设置为123456
ip:192.168.1.20-30
以下是非交互式分发密钥,for循环实现,ansible主机需要先安装expect
#!/bin/bash
cd /root
PW=123456
for IP in 192.168.1.{20..30}
do
set timeout 10
/usr/bin/expect <<-EOF
spawn ssh-copy-id -i .ssh/id.rsa.pub $IP
expect {
"connecting (yes/no)" {send "yes\r";exp_continue}
"password" {send "$PW\r"}
}
expect eof
EOF
done