Shell 腳本自動安裝 Cobbler (知識點+踩坑點)

Shell 腳本自動安裝 Cobbler (知識點+踩坑點)

前言:

Cobbler作爲一個預備工具,使批量部署Red Hat/Centos/Fedora系統更容易,同時也支持Suse和Debian系統的部署。網上有許多cobbler 安裝教程,但對於用shell腳本自動安裝cobbler 的教程幾乎沒有,於是我花了一些時間寫出了這個腳本,方便自己及他人安裝系統使用!
PS:本人比較懶,一般能用腳本自動化安裝的服務,就不想一步步敲命令!不知道有沒有和我想法一樣的朋友?

腳本環境

1.linux centos 7 系統
2.系統可連接外網
3.網絡模式:NAT模式

實驗步驟

1.上傳cobbler 和slow 腳本到Linux系統/root目錄下

方法一:掛載 方法二:通過Xftp軟件上傳

Shell 腳本自動安裝 Cobbler (知識點+踩坑點)

2.腳本cobbler.sh詳解

#!/bin/bash

#獲取本機ip地址
ip=`ifconfig ens33 | grep "netmask" | awk '{print $2}'`
#獲取本機網段
net=`ifconfig ens33 | grep "netmask" | awk '{print $2}' | cut -c 1-10`

知識點:

1.獲取本機IP辦法是:先過濾出含有IP地址的行,再用awk過濾出含有IP的列
2.獲取網段的方法是在獲取IP後,用cut命令命令截取網段部分
3.cut -c 1-10 表示截取前10位字符

安裝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
}

安裝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 '102d' /etc/cobbler/settings
sed -i "101idefault_password_crypted: \"$pass\"" /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 '102d' 是刪除102行,sed -i "101i。。。" 在101行插入內容,需要注意的是,sed -i 後面加雙引號纔可插入變量$pass中的內容,單引號無法實現

  1. 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

3.腳本slow.sh詳解

#!/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

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
}

知識點:

由於涉及服務比較多,最後再逐一檢查一下所有服務狀態,以防出錯!

函數彙總

main()
{
soft &&check_service
}

執行函數

main

坑點及解決過程

1.可能有人會問,爲什麼要用2個腳本,很不方便啊 !原因如下:

在同一個腳本中使用 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個腳本,才能順利執行命令。

2.epel 源和 cobbler相關包一塊下載會衝突

錯誤示例: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
Shell 腳本自動安裝 Cobbler (知識點+踩坑點)

解決辦法:將命令分開在2個函數中,再通過判斷查看執行狀態(2個函數是install_epel 和install_cobbler_softs)。

總結:

1.此腳本花費了我一週多時間才完成,儘管如此,腳本還是有很多瑕疵,歡迎批評指出!
2.此腳本僅供參考,腳本方法不唯一。
3.不建議直接複製腳本內容使用,可去我的資料下載原腳本文件 https://down.51cto.com/data/2461608
4.對cobbler自動裝機過程不熟悉的,可先閱讀我的博客 https://blog.51cto.com/13760351/2151911

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章