0°

Shell 脚本自动安装 Cobbler (知识点+踩坑点)

vcWxvtfUtq+wstewIENvYmJsZXIgKNaqyra14yuyyL/TteMp" />
前言:

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=

1
1`

ifconfig ens33 | grep "netmask" | awk '{print $2}'

1
1`

#获取本机网段net=

1
1`

ifconfig ens33 | grep "netmask" | awk '{print $2}' | cut -c 1-10

1
1`

知识点:

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 [

1
1`

yum –disablerepo=* –enablerepo=epel repolist | grep -c epel

1
1`

-eq 0 ]thenwget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repoecho -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 -yif [ $? -ne 0 ]thenecho -e "\033[31m install failed. \033[0m"exit 0fiecho -e "\033[32m cobbler-softs finish! \033[0m"}

关闭防火墙,安全性

firewall(){systemctl stop firewalldsetenforce 0}

cobbler配置

configure_cobbler(){if [ $? -eq 0 ];thenpass=

1
1`

openssl passwd -1 -salt 'abc123' 'abc123'

1
1`

sed -i '102d' /etc/cobbler/settingssed -i "101idefault_password_crypted: "$pass"" /etc/cobbler/settingssed -i "s/^server: 127.0.0.1/server: $ip/g" /etc/cobbler/settingssed -i "s/^next_server: 127.0.0.1/next_server: $ip/" /etc/cobbler/settingssed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settingsfi}

知识点:

1.添加密码方法是先插入新内容,再删除旧内容。2.sed -i '102d' 是删除102行,sed -i "101i。。。" 在101行插入内容,需要注意的是,sed -i 后面加双引号才可插入变量$pass中的内容,单引号无法实现

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 ];thensed -i "21s/192.168.1/$net/g" /etc/cobbler/dhcp.templatesed -i "22s/192.168.1.5/$net.1/g" /etc/cobbler/dhcp.templatesed -i "23s/192.168.1.1/$net.2/g" /etc/cobbler/dhcp.templatesed -i "25s/192.168.1.100 192.168.1.254/$net.100 $net.200/" /etc/cobbler/dhcp.templatefi}

知识点:

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/tftpsystemctl start xinetd}

重启所有服务

restart_services(){echo -e "\033[36m Restart cobbler's services \033[0m"systemctl restart cobblerdsystemctl restart httpdsystemctl restart cobblerdsystemctl restart xinetdsystemctl 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=

1
1`

systemctl status cobblerd | grep "active (running)" | wc -l

1
1`

http=

1
1`

netstat -ntap | grep :80 | wc -l

1
1`

if [ $http -ne 0 ]&& [ $cob -eq 1 ]; thencobbler 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 rsyncdelse echo "httpd,cobbler no start!"fi}

知识点:

1.要先判断http和cobbler服务是否开启,否则cobbler sync同步一定会报错。2.导入镜像文件会花费一点时间,这是正常状态,并非故障。3.只有同步后(cobbler sync)才能开启dhcp服务,否则会报错。

检查所有服务状态

check_service(){

检查httpd服务状态

http=

1
1`

netstat -ntap | grep :80 | wc -l

1
1`

if [ $http -ne 0 ];thenecho -e "\033\t[32m http is ok! \033[0m"else echo -e "\033\t[31m http error,check ! \033[0m"fi

检查cobbler 服务状态

cob=

1
1`

systemctl status cobblerd | grep "active (running)" | wc -l

1
1`

if [ $cob -eq 1 ];thenecho -e "\033\t[32m cobbler is ok! \033[0m"else echo -e "\033\t[31m cobbler error,check ! \033[0m"fi

检查iso镜像导入状态

os=

1
1`

cobbler distro list | wc -l

1
1`

if [ $os -eq 1 ];thenecho -e "\033\t[34m ISO file ok! \033[0m"else echo -e "\033\t[31m ISO file error,check ! \033[0m"fi

检查是否同步

sync=

1
1`

cobbler sync |wc -l

1
1`

if [ $sync -gt 1 ];thenecho -e "\033\t[34m cobbler sync ok! \033[0m"else echo -e "\033\t[31m cobbler sync error,check ! \033[0m"fi

检查dhcp服务状态

dhcp=

1
1`

systemctl status dhcpd | grep "active (running)" | wc -l

1
1`

if [ $dhcp -eq 1 ]; thenecho -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

1
1`

if [ $load -gt 1 ];thenecho -e "\033\t[34m get-loaders ok! \033[0m"else echo -e "\033\t[31m get-loaders error,check ! \033[0m"fi

检查tftp服务状态

tftp=

1
1`

systemctl status xinetd | grep "active (running)" | wc -l

1
1`

if [ $tftp -eq 1 ];thenecho -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 ];thenecho -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)。

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!