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 "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

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

转载于:https://blog.51cto.com/13760351/2397130

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值