shell脚本一键部署cobbler批量装机服务器

基于CentOS 7.5操作系统

函数库:https://blog.csdn.net/anqixiang/article/details/89964048

#!/bin/bash
#AUTHOR:AN
#VERSION:1.0.0
#DATE:2019-07-27
#MODIFY:
#FUNCTION:一键部署cobbler服务器
#DESCRIBE:基于CentOS-7.5操作系统搭建
#NOTICES:

######################定义变量######################
local_dir=`dirname $0`
function_dir=$local_dir//myfunction.lib

#加载函数库
if [ -f "$function_dir" ];then
 	source $function_dir
else
	echo -e "\033[31m函数库不存在\033[0m"
	exit 71
fi

######################需注意的地方######################
cobbler_packet=/root/Cobbler.zip					#软件包
des_dir=/opt										#解压的目标目录
root_pwd="1qaz2wsx@123"								#装完系统后的root密码
timeout=2000										#超时时间
#dhcp
local_ip=`ip a s |awk '/inet /{print $2}' |awk -F\/ '{print $1}' |sed -n '2p'`		#提取本机IP地址
network=`echo ${local_ip%.*}`						#当前网络地址
gateway="${network}.1"								#网关
nameserver="223.5.5.5"								#DNS服务器
dynamic="${network}.100 ${network}.200" 			#dhcp地址池

#############################################################
#停止防火墙,关闭selinux
Init(){
	#停止防火墙
	systemctl stop firewalld.service
	systemctl disable firewalld.service
	#关闭selinux
	Sel_Value=`grep '^SELINUX=' /etc/selinux/config |awk -F= '{print $2}'`
	[ "Sel_Value" != "disabled" ] && sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
	setenforce 0 &> /dev/null
}

#安装软件包
InstallSoft(){
	soft_packets=("unzip" "dhcp" "tftp-server" "pykickstart" "httpd" )
	YUMMUL "${soft_packets[*]}"
	yum -y install fence-agents
	
	unzip  $cobbler_packet -d / && unzip /Cobbler/cobbler.zip  -d  $des_dir	
	rpm -ivh $des_dir/cobbler/python2-django-1.6.11.6-16.el7.noarch.rpm --nodeps --force
	rpm -ivh $des_dir/cobbler/python2-simplejson-3.10.0-1.el7.x86_64.rpm --nodeps --force
	rpm -ivh $des_dir/cobbler/python-cheetah-2.4.4-5.el7.centos.x86_64.rpm --nodeps
	yum -y install $des_dir/cobbler/cobbler-*	
	
	#释放引导文件
	tar -xvPf  /Cobbler/cobbler_boot.tar.gz
	#或者用下面的命名在线下载引导文件
	#cobbler get-loaders
}

#修改配置文件
modify_conf(){
	#修改cobbler主配置文件
	cobbler_conf=/etc/cobbler/settings			#cobbler主配置文件
	[ ! -f ${cobbler_conf}.save ] && cp ${cobbler_conf} ${cobbler_conf}.save	
	sed -i "272 s/127.0.0.1/${local_ip}/" $cobbler_conf
	sed -i "384 s/127.0.0.1/${local_ip}/" $cobbler_conf
	sed -i '242 s/0/1/' $cobbler_conf
	sed -i '292 s/0/1/' $cobbler_conf
	sed -ri "/default_password_crypted/s#(.*: ).*#\1\"`openssl passwd -1 -salt 'cobbler' "${root_pwd}"`\"#"  $cobbler_conf		#修改默认密码
	
	#修改dhcp配置
	dhcp_conf=/etc/cobbler/dhcp.template
	[ ! -f ${dhcp_conf}.save ] && cp ${dhcp_conf} ${dhcp_conf}.save
	sed -i "21c subnet ${network}.0 netmask 255.255.255.0 {"  $dhcp_conf
	sed -i "22 s/192.168.1.5/$gateway/"   $dhcp_conf			 			#设置网关
	sed -i "23 s/192.168.1.1/$nameserver/"   $dhcp_conf						#设置dns服务器地址
	sed -i "25 s/192.168.1.100 192.168.1.254/$dynamic/"  $dhcp_conf	#设置dhcp服务器IP地址租用的范围
	
	#修改启动菜单,设置超时时间
	menu_conf=/etc/cobbler/pxe/pxedefault.template
	[ ! -f ${menu_conf}.save ] && cp ${menu_conf} ${menu_conf}.save
	sed -i "/TOTALTIMEOUT/ s/6000/${timeout}/" $menu_conf
	sed -i '8,11d' $menu_conf
}

#启动相关服务
start_service(){
	echo "[ ss -nutlp |grep tftp &>/dev/null ] || systemctl start tftp" >> /etc/rc.local		#解决tftp开机不启动的问题
	chmod +x /etc/rc.local
	STR_SVC cobblerd
	STR_SVC httpd
	STR_SVC tftp
	STR_SVC rsyncd
	cobbler sync						#先同步配置文件才能启动dhcpd服务
	STR_SVC dhcpd
	cobbler check
}

#导入镜像[写绝对路径]
import_images(){
	image_dir=$1										#镜像路径
	mount_dir=/system									#挂载目录
	[ ! -d $mount_dir ] && mkdir $mount_dir  || umount $mount_dir
	mount $image_dir $mount_dir
	
	image_name=`basename $image_dir`		#提取镜像名
	name=`echo $image_name |awk -F- '{print $1 "-" $2}'`
	cobbler import --path=$mount_dir --name=${name} --arch=x86_64
	cobbler check
}

#帮助信息
HELP(){
	cat << EOF
cobbler version 1.1.0
Usage: cobbler [-h] [-all] [-init] [-soft] [-conf][-start][-import 镜像绝对路径]
=======================================================================
optional arguments:
	-h				提供帮助信息
	-all			一键式安装(适用于首次安装)
	-init			关闭防火墙、设置selinux
	-soft			安装软件包
	-conf			修改配置文件
	-start			启动服务
	-import			导入镜像
EXAMPLE:
	bash cobbler.sh -import ubuntu-16.04.4-server-amd64.iso
EOF
}
#############################主程序#############################
[ $# -eq 0 ] && HELP
case $1 in 
-h)
	HELP ;;
-all)
	Init 
	InstallSoft
	modify_conf
	start_service;;
-init)
	Init;;
-soft)
	InstallSoft;;
-conf)
	modify_conf;;
-start)
	start_service;;
-import)	
	if [ $# -eq 2 ];then
		name=$2 
		suffix=`echo ${name##*.}`
		[ ${suffix} != 'iso' ] && cecho 31 "Invalid option:bash `basename $0` [-h]" && exit 71	
		import_images ${name} && exit 0
	fi
	cecho 31 "Invalid option:bash `basename $0` [-h]";;	
*)
	cecho 31 "Invalid option:bash `basename $0` [-h]"
esac

你可能感兴趣的:(Shell)