运用ansible进行自动化运维之前,我们需要对所有机器进行SSH认证,运用下面脚本一键实现机器批量SSH免密登录,务必保证批量机器为同一用户同一密码。
#!/bin/bash#批量实现SSH免密登录#没有则安装expectif ! rpm -q expect > /dev/nullthen echo "###expect 未安装,现在安装###" yum install -y expect &>/dev/null if [ $? -ne 0 ] then echo "###expect 安装失败###" exit 1 fifi#本机没有SSH密钥则生成if [ ! -f ~/.ssh/id_rsa ]then echo "###请按3次enter键###" ssh-keygen -t rsafissh_expect () { expect -c "set timeout -1; spawn ssh-copy-id -f $1 expect { "yes/no" { send -- yes\r;exp_continue;} "password:" { send -- $2\r;exp_continue;} eof }";}[ -f hosts.txt ] && rm -rf hosts.txt#定义 hosts.txtcat > hosts.txt << EOF 192.168.30.128 192.168.30.129 192.168.30.130 EOFpasswd=123456789for ip in `cat hosts.txt |awk '{print $1}'`do ssh_expect $ip $passwddone