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