Cobbler做为一个预备工具,使批量部署Red Hat/Centos/Fedora系统更容易,同时也支持Suse和Debian系统的部署。网上有许多cobbler 安装教程,但对于用shell脚本自动安装cobbler 的教程几乎没有,因而我花了一些时间写出了这个脚本,方便本身及他人安装系统使用!
PS:本人比较懒(高效率!),通常能用脚本自动化安装的服务,就不想一步步敲命令!不知道有没有和我想法同样的朋友?linux
1.linux centos 7 系统
2.系统可链接外网
3.网络模式:NAT模式web
#!/bin/bashshell
#获取本机ip地址
ip=`ifconfig ens33 | grep "netmask" | awk '{print $2}'`
#获取本机网段
net=`ifconfig ens33 | grep "netmask" | awk '{print $2}' | cut -c 1-10`centos
知识点:安全
1.获取本机IP办法是:先过滤出含有IP地址的行,再用awk过滤出含有IP的列
2.获取网段的方法是在获取IP后,用cut命令命令截取网段部分
3.cut -c 1-10 表示截取前10位字符bash
安装epel源网络
install_epel()
{
echo -e "\033[36m Installing epel... \033[0m"
if [ `yum --disablerepo=* --enablerepo=epel repolist | grep -c epel` -eq 0 ]
then
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
echo -e "\033[32m install epel finish! \033[0m"
else
echo -e "\033[32m epel already exists \033[0m"
fi
}ide
安装cobbler全部相关包函数
install_cobbler_softs()
{工具echo -e "\033[36m Installing cobbler-softs... \033[0m"
yum install cobbler cobbler-web dhcp tftp-server pykickstart httpd rsync xinetd tree -y
if [ $? -ne 0 ]
then
echo -e "\033[31m install failed. \033[0m"
exit 0
fi
echo -e "\033[32m cobbler-softs finish! \033[0m"
}
关闭防火墙,安全性
firewall()
{
systemctl stop firewalld
setenforce 0
}
cobbler配置
configure_cobbler()
{
if [ $? -eq 0 ];then
pass=`openssl passwd -1 -salt 'abc123' 'abc123' `
sed -i "101idefault_password_crypted: \"$pass\"" /etc/cobbler/settings
sed -i '102d' /etc/cobbler/settings
sed -i "s/^server: 127.0.0.1/server: $ip/g" /etc/cobbler/settings
sed -i "s/^next_server: 127.0.0.1/next_server: $ip/" /etc/cobbler/settings
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
fi
}
知识点:
1.添加密码方法是先插入新内容,再删除旧内容。
2.sed -i "101i。。。" 在101行插入内容,须要注意的是,sed -i 后面加双引号才可插入变量$pass中的内容,单引号没法实现。sed -i '102d' 是删除102行,或者 sed -i "101c default_password_crypted: \"$pass\"" /etc/cobbler/settings (推荐使用)
3.sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings 表示manage_dhcp: 1 替换 manage_dhcp: 0 ,不要弄反了!
dhcp模板配置
configure_dhcp_template()
{
if [ $? -eq 0 ];then
sed -i "21s/192.168.1/$net/g" /etc/cobbler/dhcp.template
sed -i "22s/192.168.1.5/$net.1/g" /etc/cobbler/dhcp.template
sed -i "23s/192.168.1.1/$net.2/g" /etc/cobbler/dhcp.template
sed -i "25s/192.168.1.100 192.168.1.254/$net.100 $net.200/" /etc/cobbler/dhcp.template
fi
}
知识点:
1.sed -i 后面要加双引号,不要用单引号,不然没法插入变量值!
2.最后一行是分配IP的地址池,这里是100-200,可根据实际状况修改!
tftpd配置
configure_tftpd()
{
echo -e "\033[36m Configure tftpd \033[0m"
sed -i '14s/yes/no/' /etc/xinetd.d/tftp
systemctl start xinetd
}
重启全部服务
restart_services()
{
echo -e "\033[36m Restart cobbler's services \033[0m"
systemctl restart cobblerd
systemctl restart httpd
systemctl restart cobblerd
systemctl restart xinetd
systemctl enable rsyncd
}
函数汇总
main()
{
install_epel&& install_cobbler_softs && firewall && configure_cobbler &&configure_tftpd &&configure_dhcp_template &&restart_services &&./slow.sh
}
知识点:
1.这里全部函数之间采用&&符号连接,表示上一条命令执行完成才会执行下一条
2.加&& 符号是必要的,不然软件包没下载完就配置了,必然出错,通过屡次验证,不加&&,出错率很高!
执行全部函数
main
#!/bin/bash
安装镜像,系统引导文件,同步
soft()
{
cob=`systemctl status cobblerd | grep "active (running)" | wc -l `
http=`netstat -ntap | grep :80 | wc -l`
if [ $http -ne 0 ]&& [ $cob -eq 1 ]; then
cobbler sync && systemctl restart dhcpd && mount /dev/sr0 /mnt && cobbler import --path=/mnt/ --name=CentOS-7-x86_64 --arch=x86_64 && cobbler get-loaders && systemctl start rsyncd
else echo "httpd,cobbler no start!"
fi
}
知识点:
1.要先判断http和cobbler服务是否开启,不然cobbler sync同步必定会报错。
2.导入镜像文件会花费一点时间,这是正常状态,并不是故障。
3.只有同步后(cobbler sync)才能开启dhcp服务,不然会报错。
检查全部服务状态
check_service()
{
检查httpd服务状态
http=`netstat -ntap | grep :80 | wc -l`
if [ $http -ne 0 ];then
echo -e "\033\t[32m http is ok! \033[0m"
else echo -e "\033\t[31m http error,check ! \033[0m"
fi
检查cobbler 服务状态
cob=`systemctl status cobblerd | grep "active (running)" | wc -l `
if [ $cob -eq 1 ];then
echo -e "\033\t[32m cobbler is ok! \033[0m"
else echo -e "\033\t[31m cobbler error,check ! \033[0m"
fi
检查iso镜像导入状态
os=`cobbler distro list | wc -l `
if [ $os -eq 1 ];then
echo -e "\033\t[34m ISO file ok! \033[0m"
else echo -e "\033\t[31m ISO file error,check ! \033[0m"
fi
检查是否同步
sync=`cobbler sync |wc -l`
if [ $sync -gt 1 ];then
echo -e "\033\t[34m cobbler sync ok! \033[0m"
else echo -e "\033\t[31m cobbler sync error,check ! \033[0m"
fi
检查dhcp服务状态
dhcp=`systemctl status dhcpd | grep "active (running)" | wc -l `
if [ $dhcp -eq 1 ]; then
echo -e "\033\t[32m dhcp is ok! \033[0m"
else echo -e "\033\t[31m dhcp error,check ! \033[0m"
fi
检查系统引导文件下载状态
load=\cobbler get-loaders | grep "already exists" | wc -l`
if [ $load -gt 1 ];then
echo -e "\033\t[34m get-loaders ok! \033[0m"
else echo -e "\033\t[31m get-loaders error,check ! \033[0m"
fi
检查tftp服务状态
tftp=`systemctl status xinetd | grep "active (running)" | wc -l`
if [ $tftp -eq 1 ];then
echo -e "\033\t[32m tftp is ok! \033[0m"
else echo -e "\033\t[31m tftp error,check ! \033[0m"
fi
}
知识点:
因为涉及服务比较多,最后再逐一检查一下全部服务状态,以防出错!
函数汇总
main()
{
soft &&check_service
}
执行函数
main
在同一个脚本中使用 cobbler sync && systemctl restart dhcpd && mount /dev/sr0 /mnt && cobbler import --path=/mnt/ --name=CentOS-7-x86_64 --arch=x86_64 && cobbler get-loaders && systemctl start rsyncd 这些命令或其中的部分命令,都没执行,直接退出脚本!
我采起了判断,循环,等待等这些办法,都不能执行这些命令,甚至执行其中一条,都不行。最后发现只有分开成2个脚本,才能顺利执行命令。
错误示例:rpm -ivh https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm &&yum makecache && yum install cobbler cobbler-web dhcp tftp-server pykickstart httpd rsync xinetd tree -y
1.此脚本花费了我一周多时间才完成,尽管如此,脚本仍是有不少瑕疵,欢迎批评指出!
2.此脚本仅供参考,脚本方法不惟一。
3.不建议直接复制脚本内容使用,可去个人资料下载原脚本文件 https://down.51cto.com/data/2461608
4.对cobbler自动装机过程不熟悉的,可先阅读个人博客 http://www.javashuo.com/article/p-kjkgclwu-c.html