一键自动部署cobbler环境

#/bin/bash
echo "********************************************************"
echo "*  使用脚本注意事项:"
echo "1、设备支持访问外网"
echo "2、请将镜像上传至/root/目录下"
echo "3、将ks文件放在/root/目录下并重命名为ks.cfg"
echo "4、使用前请修改脚本中的server和导入镜像的名称(Profile_Name)"
echo "********************************************************"
Server=192.168.15
Profile_Name=test
Server_GW=${Server}.1
Server_sub=${Server}.0
Server_RG=${Server}.254
Server_IP=`hostname -I`

#关闭selinux和防火墙,配置epel源
env_state(){
  if [ `getenforce` != Disabled ]
  then
    systemctl stop firewalld
    systemctl disable firewalld &> /dev/null
    sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config
    echo "selinux、firewalld没有关闭,已调整为disabled状态,请重启机器"
    exit 1
  fi
  rm -rf /etc/yum.repos.d/epel*
  curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
  yum clean all &> /dev/null
  yum makecache fast &> /dev/null
  echo "检查yum源配置,请稍后......"
  sleep 1
  val=$?
  if [ $val -ne 0 ]
  then
    echo "yum 配置有问题!请手动配置一下yum源"
    exit 1
  else
    yum -y install net-tools &> /dev/null
  fi
}

#将DHCP服务托管于cobbler
ins_dhcp(){
  if rpm -q dhcp &> /dev/null
  then
    echo "DHCP服务已安装,需卸载已存在服务"
    yum remove -y dhcp &> /dev/null
    rm -rf /etc/dhcp/
  fi
  yum -y install dhcp > /dev/null
  #systemctl start dhcpd
  #systemctl enable dhcpd &> /dev/null
  sed -i "/^subnet/s/192.168.1.0/${Server_sub}/" /etc/cobbler/dhcp.template
  sed -i "/option routers/s/192.168.1.5/${Server_GW}/" /etc/cobbler/dhcp.template
  sed -i "/option domain-name-servers/d" /etc/cobbler/dhcp.template
  sed -i "/range dynamic-bootp/s/192.168.1.100 192.168.1.254/${Server_GW} ${Server_RG}/" /etc/cobbler/dhcp.template

}

ins_tftp(){
  if rpm -q tftp-server &> /dev/null
  then
    echo "TFTP服务已安装,需卸载已存在服务"
    yum remove -y tftp-server &> /dev/null
    rm -rf /etc/xinetd.d/tftp
  fi
  yum -y install xinetd tftp-server > /dev/null
  sed -i '/disable/s/yes/no/' /etc/xinetd.d/tftp
  systemctl start tftp xinetd
  systemctl enable tftp xinetd &> /dev/null
  netstat -anplu | grep :69 &> /dev/null || exit 1
  return 0

}

ins_http(){
  if rpm -q httpd &> /dev/null
  then
    echo "Httpd服务已安装,需卸载已存在服务"
    yum remove -y httpd &> /dev/null
    rm -rf /etc/httpd/ &> /dev/null
  fi
  yum -y install httpd > /dev/null
  systemctl start httpd
  systemctl enable httpd &> /dev/null
  ss -ntl |grep :80 &> /dev/null || exit 1
  return 0
}

ins_conf_cobbler(){
  if rpm -q cobbler &> /dev/null
  then
    echo "cobbler服务已安装,需卸载已存在服务"
    yum remove -y cobbler &> /dev/null
    rm -rf /etc/cobbler/ &> /dev/null
  fi
  yum -y install cobbler pykickstart rsync > /dev/null
  systemctl start cobblerd rsyncd
  systemctl enable cobblerd rsyncd &> /dev/null
  systemctl restart httpd
  cobbler check > /dev/null
  sleep 3
  sed  -i '/^pxe/s/0/1/' /etc/cobbler/settings
  sed -i '/^manage_dhcp/s/0/1/' /etc/cobbler/settings
  sed  -i "/^server/s/127.0.0.1/${Server_IP}/" /etc/cobbler/settings
  sed  -i "/^next_server/s/127.0.0.1/${Server_IP}/" /etc/cobbler/settings
  sed  -i -r "/^default_password_crypted/s/(default_password_crypted: \")(.*)(\")/\1\3/" /etc/cobbler/settings
  cobbler get-loaders &> /dev/null
  if [ `ls /var/lib/cobbler/loaders|wc -l` != 10 ]
  then
    echo "引导文件下载失败,请手动下载!"
    exit 1
  fi
  if ins_dhcp
  then
    echo "DHCP服务配置完成"
  fi
  systemctl restart cobblerd
  sleep 2
  cobbler check
  ss -ntl |grep 25151 &> /dev/null || exit 1
  return 0

}

if env_state
then
  echo "安装环境准备完成"
fi
if ins_tftp
then
  echo "TFTP服务配置完成"
fi
if ins_http
then
  echo "http服务配置完成"
fi
if ins_conf_cobbler
then
  echo "cobbler服务配置完成,开始导入镜像:"
  mv /root/ks.cfg /var/lib/cobbler/kickstarts/${Profile_Name}.cfg &> /dev/null
  mount /root/*.iso /mnt/
  cobbler import --path=/mnt --name=${Profile_Name} --kickstart=/var/lib/cobbler/kickstarts/${Profile_Name}.cfg
  cobbler sync
  echo
  if cobbler profile list
  then
    echo "cobbler部署完毕!"
  fi
fi
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。