#! /bin/bash
host_controller=192.168.174.150 # 控制节点IP地址
host_address=(192.168.174.151 192.168.174.152) # 客户端们的IP地址
host_hostname=(client1 client2) # 客户端们的域名
host_username=root # ssh连接的用户,控制端的用户为root
host_passwd=110119 # ssh连接的用户密码
# 安装expect命令
expect -v &> /dev/null
if [ `echo $?` -ne 0 ];then
echo "没有expect,安装expect命令"
yum install -y expect
fi
# 配置免密登录
echo "";echo ""
echo "########################## 本地开始配置ssh ##########################"
if [ `test -a ~/.ssh/id_rsa.pub;echo $?` == 0 ];then
echo "ssh公钥已创建"
else
echo "ssh公钥未创建,开始创建"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 10
spawn ssh-keygen -t rsa -b 1024
## 开始进连续捕获
expect {
"connecting (yes/no)?" { send "yes\n"; exp_continue }
"s password:" { send "${host_passwd}\n"; exp_continue }
".ssh/id_rsa)" { send "\n"; exp_continue }
"Overwrite (y/n)?" { send "y\n"; exp_continue }
"no passphrase):" { send "\n"; exp_continue }
"passphrase again:" { send "\n"; exp_continue }
}
eof
fi
# 本地的密钥开始加入被控制主机
for ((j=0;j<${#host_address[*]};j++));do
echo "########################## ${host_address[j]}正在被添加公钥 ##########################"
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 10
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub ${host_username}@${host_address[j]}
## 开始进连续捕获
expect {
"connecting (yes/no)?" { send "yes\n"; exp_continue }
"s password:" { send "${host_passwd}\n"; exp_continue }
}
eof
echo "############# ${host_address[j]}配置完毕 #############"
echo "";echo "";echo ""
done
flag_ssh=0
# 测试ssh配置是否成功
for ((j=0;j<${#host_address[*]};j++));do
if [ `ssh ${host_username}@${host_address[j]} -o ConnectTimeout=5 "exit";echo $?` == 0 ];then
echo -e "\033[32m Success: ${host_address[j]}连接成功 \033[0m"
else
echo -e "\e[31m Failed: ${host_address[j]}的ssh连接失败,请检查! \e[0m"
flag_ssh=1
fi
done
echo "";echo "";echo ""
if [ ${flag_ssh} == 1 ];then
echo "############# 已退出 #############"
exit
fi
# 配置防火墙和selinux
echo "########################## 开始配置防火墙和selinux ##########################"
systemctl stop firewalld;systemctl disable firewalld
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
for ((j=0;j<${#host_address[*]};j++));do
ssh ${host_username}@${host_address[j]} "systemctl stop firewalld;systemctl disable firewalld"
ssh ${host_username}@${host_address[j]} "sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config"
done
echo "############# 配置完毕 #############"
echo "";echo "";echo ""
# 修改配置域名映射
echo "########################## 开始配置域名映射 ##########################"
ssh root@${host_address[j]} "cat << eof > /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
eof"
for ((k=0;k<${#host_address[*]};k++));do
echo "${host_address[k]} ${host_hostname[k]}" >> /etc/hosts
done
echo "";echo "";echo ""
sleep 5
# 首次域名ssh连接
for ((j=0;j<${#host_hostname[*]};j++));do
/usr/bin/expect << eof
# 设置捕获字符串后,期待回复的超时时间
set timeout 10
spawn ssh ${host_username}@${host_hostname[j]} "exit"
## 开始进连续捕获
expect {
"connecting (yes/no)?" { send "yes\n"; exp_continue }
"s password:" { send "${host_passwd}\n"; exp_continue }
}
eof
echo "############# ${host_hostname[j]}配置完毕 #############"
done
echo "";echo "";echo ""
# 配置chrony时间服务器
echo "########################## 开始配置chrony ##########################"
if [ `systemctl restart chronyd;echo $?` != 0 ];then
echo "本地 安装chrony"
yum install -y chrony && systemctl restart chronyd
if [ `echo $?` != 0 ];then
echo "安装失败,请排错!"
exit
fi
fi
# 本地配置
sed -i 's/#allow 192.168.0.0\/16/allow 192.168.10.0\/16/' /etc/chrony.conf
sed -i 's/#local stratum 10/local stratum 10/' /etc/chrony.conf
systemctl restart chronyd && systemctl enable chronyd &> /dev/null
sleep 5
timedatectl set-ntp true
# 客户端配置
for ((j=0;j<${#host_address[*]};j++));do
if [ `ssh ${host_username}@${host_address[j]} "systemctl restart chronyd;echo $?"` != 0 ];then
echo "${host_address[j]} 安装chrony"
ssh ${host_username}@${host_address[j]} "yum install -y chrony && systemctl restart chronyd"
if [ `echo $?` != 0 ];then
echo "安装失败,请排错!"
exit
fi
fi
echo "${host_address[j]}配置chrony"
ssh ${host_username}@${host_address[j]} "sed -i '/^server/d' /etc/chrony.conf"
ssh ${host_username}@${host_address[j]} "sed -i '2a\server '"${host_controller}"' iburst\' /etc/chrony.conf"
ssh ${host_username}@${host_address[j]} "systemctl restart chronyd && systemctl enable chronyd &> /dev/null"
sleep 5
ssh ${host_username}@${host_address[j]} "timedatectl set-ntp true && chronyc sources -v | sed -n '/^\^\*/p'"
if [ -z "`ssh root@${host_address[j]} "chronyc sources -v | sed -n '/^\^\*/p'"`" ];then
echo -e "\e[31m此节点${host_address[j]}的chrony配置有误,请手动调试\e[0m"
exit
fi
echo "############# ${host_address[j]}配置完毕 #############"
done
echo "";echo "";echo ""
# 安装ansbile
echo "安装ansbile"
yum install -y epel-release && yum install -y ansible && ansible --version && echo "";echo "";echo -e "\033[32m 安装Success \033[0m"
# 在/etc/ansible/hosts文件中添加主机租
echo "在/etc/ansible/hosts文件中添加主机租"
echo "[clients]" >> /etc/ansible/hosts
for ((k=0;k<${#host_address[*]};k++));do
echo "${host_hostname[k]}" >> /etc/ansible/hosts
done
# 测试
echo "测试ansible命令"
ansible clients -a "uptime"
使用shell脚本搭建ansible集群
最新推荐文章于 2025-02-28 08:00:00 发布