自动配置ceph集群节点安装环境的shell脚本
#!/bin/bashPASSWD="root"DIR=/root/scriptif ! -d "$DIR" ; then mkdir -p $DIRelse rm -rf $DIR mkdir -p $DIRfi#配置主机文件/etc/hostsecho -e "n*n"while :doread -p "输入ceph节点起始IP地址(例如192.168.1.201): " Start_IPread -p "输入的起始IP地址$Start_IP正确吗(y/n): " yn if $yn = "Y" | $yn = "y" ;thenecho -e "n输入的起始IP地址正确,继续进行下一项工作"breakelseecho -e "n输入的起始IP地址有误,将再次输入"fidoneecho -e "n*n"while :doread -p "输入ceph节点终止IP地址(例如192.168.1.205):" End_IPread -p "输入的终止IP地址$End_IP正确吗(y/n): " ynif $yn = "Y" | $yn = "y" ;thenecho -e "n输入的终止IP地址正确,继续进行下一项工作"breakelseecho -e "n输入的终止IP地址有误,将再次输入"fidoneecho "n*n"while :doread -p "输入集群节点名称以何字符串打头(例如ceph、node等):" Noderead -p "确认以$Node打头来命名节点吗(y/n): " ynif $yn = "Y" | $yn = "y" ;thenecho -e "n已确认使用$Node来命名集群节点,继续进行下一项工作"breakelseecho -e "n不想使用$Node来命名节点"fidoneecho "n*"unset ip_numip_num=1S_IP=$(echo $Start_IP|awk -F. 'print $4')E_IP=$(echo $End_IP|awk -F. 'print $4')IP=$Start_IP%.*ip_num=$($E_IP-$S_IP+$ip_num) echo -e "n-n"echo -e "n本次要使用的网段为:$IPn"echo -e "n起始地址最后一个数为:$S_IPn"echo -e "n终止地址最后一个数为:$E_IPn"echo -e "n本次要使用的地址个数为:$ip_numn"echo -e "n本次使用字符串'"$Node"' 打头来命名集群节点n"echo -e "n-n"B=($(seq -f"%02g" 1 $ip_num)M=$Nodej=0for (i=0;i<$ip_num;i+);do A=$($S_IP+$j) j=$($j+1) S=$IP.$A T=$M$B$i echo "$S $T" >> $DIR/hostsdone#配置自动免密登录echo -e "n#n"echo -e "n开始配置自动免密登录n"echo -e "n#n"#生成临时文件cat $DIR/hosts|awk 'print $1' >$DIR/ip.txtcat $DIR/hosts|awk 'print $2' >$DIR/host.txtsed -i '/s*$/d' $DIR/host.txt sed -i '/$/d' $DIR/ip.txt #创建ip和host数组iparray=($(cat $DIR/ip.txt)hostarray=($(cat $DIR/host.txt)ansible_host=hostnameansible_ip=more /etc/sysconfig/network-scripts/ifcfg-en* | grep IPADD |awk -F'"' 'print $2'echo "$ansible_ip $ansible_host" >>$DIR/hostscat $DIR/hosts|awk 'print $1' >$DIR/allip.txtcat $DIR/hosts|awk 'print $2' >$DIR/allhost.txtsed -i '/s*$/d' $DIR/allhost.txtsed -i '/$/d' $DIR/allip.txt alliparray=($(cat $DIR/allip.txt)allhostarray=($(cat $DIR/allhost.txt)#安装sshpassrpm -qa | grep sshpass >/dev/nullif $? -ne 0 thenecho -e "n sshpass包没有安装,请安装.n"yum -y install sshpasssleep 10elseecho -e "n sshpass包已经安装了n"fi#配置自动免密登录rm -rf /root/.ssh/id_rsa && rm -rf /root/.ssh/known_hostsssh-keygen -t rsa -f /root/.ssh/id_rsa -N ''for ip in $alliparray doping -c2 -w1 -i0.5 $ip &>/dev/nullif $? -eq 0 thensshpass -p$PASSWD ssh-copy-id -f -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $ipecho -e "n*n"ssh $ip dateecho -e "n*n"elseecho -e "n IP地址$ip不可达,请处理后单独拷贝这台密钥.n"exit 1fidonefor host in $allhostarray do ping -c2 -w1 -i0.5 $host &>/dev/null sleep 2 if $? -eq 0 ;then sshpass -p$PASSWD ssh-copy-id -f -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $host echo -e "n*n" ssh $host date echo -e "n*n" else echo -e "n主机$host不可达,请处理后单独拷贝这台密钥.n" exit 1 fidone#向各节点拷贝hosts文件#unset hostfor host in $allhostarray dossh -t root$host 'if ! -f "/etc/hosts.bak" then cp /etc/hosts,.bak && rm -rf /etc/hosts else rm -rf /etc/hosts fi'scp $DIR/hosts root$host:/etc/echo -e "n#n"echo "$host的hosts文件内容如下"ssh -t root$host 'more /etc/hosts'echo -e "n#n"done#获取和配置yum源rpm -qa | grep wget >/dev/nullif $? -ne 0 thenecho "wget包没有安装,请安装."yum -y install wget net-toolselseecho -e "n#n"echo "wget包已经安装了"fiif ! -d "/etc/yum.repos.d/repobak" then mkdir /etc/yum.repos.d/repobak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repobakelse rm -rf /etc/yum.repos.d/*.repofi#下载centos base、epelwget -O /etc/yum.repos.d/CentOS-Base.repo wget -O /etc/yum.repos.d/epel-7.repo #创建ceph的yum源echo 'cephname=ceph packages for $basearchbaseurl=enable