偷得浮生 偷得浮生

记录精彩的人生

目录
cobbler服务自动化部署SHELL
/  

cobbler服务自动化部署SHELL

#!/bin/bash
#
#********************************************************************
#Author:zhangzhuo
#QQ: 1191400158
#Date: 2021-02-10
#FileName:install_cobbler_centos7.sh
#URL: https://www.zhangzhuo.ltd
#Description:The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
.  /etc/init.d/functions 

variable(){
#初始变量设置
#cobbler服务器地址
cobbler_server=192.168.10.71
#dhcp分配网段及子网掩码
subnet=192.168.10.0
netmask=255.255.255.0
#dhcp分配网关
routers=192.168.10.2
#dhcp服务器地址
dns=114.114.114.114
#dhcp地址池
range="192.168.10.100 192.168.10.200"
}

#安装软件
install_yum(){
yum install cobbler cobbler dhcp -y
[ $? = 0 ] && action "必备软件安装成功" || action "软件安装失败请检查YUM源" false
systemctl enable --now cobblerd.service httpd.service tftp.service &>/dev/null
systemctl is-active tftp.service &>/dev/null && action "TFTP服务启动成功" || action "TFTP服务启动失败" false
systemctl is-active httpd.service &>/dev/null && action "HTTP服务启动成功" || action "HTTP服务启动失败" false 
systemctl is-active cobblerd.service &>/dev/null && action "cobbler服务启动成功" || { action "cobbler服务启动失败" false; }
}
#配置cobbler服务
set_cobbler(){
variable
sed -ri 's/^(default_password_crypted:).*/\1 "$1$yB26sGrf$JPTQcfvTWXKBZRs0o9F7B1"/' /etc/cobbler/settings
sed -ri "s/^(next_server:).*/\1 $cobbler_server/" /etc/cobbler/settings
sed -ri "s/^(server:).*/\1 $cobbler_server/" /etc/cobbler/settings
sed -ri 's/^(manage_dhcp:).*/\1 1/' /etc/cobbler/settings
sed -ri 's/^(pxe_just_once:).*/\1 1/' /etc/cobbler/settings
systemctl restart cobblerd.service
systemctl is-active cobblerd.service &>/dev/null && action "cobbler服务重启成功" || { action "cobbler服务重新启动失败" false;exit; }
}
#配置dhcp服务
set_dhcp(){
variable
sed -ri "s/^(subnet) .* (netmask) 255.255.255.0\ (\{)/\1 ${subnet} \2 ${netmask} \3/" /etc/cobbler/dhcp.template 
sed -ri "s/[^a-z](option routers[^0-9]*)[0-9]+.*/\1${routers};/" /etc/cobbler/dhcp.template
sed -ri "s/[^a-z](option domain-name-servers[^0-9]*)[0-9]+.*/\1${dns};/" /etc/cobbler/dhcp.template
sed -ri "s/[^a-z](option subnet-mask[^0-9]*)[0-9]+.*/\1${netmask};/" /etc/cobbler/dhcp.template
sed -ri "s/[^a-z](range dynamic-bootp[^0-9]*)[0-9]+.*/\1${range};/" /etc/cobbler/dhcp.template
cobbler sync &>/dev/null
systemctl enable --now dhcpd.service &>/dev/null
}
#准备启动文件
get_cobbler(){
cobbler get-loaders &>/dev/null && action "启动文件下载完成" || { action "启动文件下载失败" false;exit; }
sed -ri 's/(ENU TITLE Cobbler \| ).*/\1 https:\/\/zhangzhuo.ltd/' /etc/cobbler/pxe/pxedefault.template
cobbler sync &>/dev/null
systemctl is-active dhcpd.service &>/dev/null && action "DHCP服务启动成功" || action "DHCP服务启动失败" false
action "cobbler服务安装完"
}

cobbler_install(){
install_yum
set_cobbler
set_dhcp
get_cobbler
}


create_ks(){
cat <<EOF >/var/lib/cobbler/kickstarts/ks-centos6.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$NxU3dKmV\$fRJv0NpyNJv6sQSK2SEh//
# System language
lang en_US.UTF-8
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System

authconfig --enableshadow --passalgo=sha512
# Firewall configuration
firewall --disabled
# Network information
network --onboot yes  --bootproto=dhcp --device=eth0 --noipv6
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url=\$tree
# System bootloader configuration
bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder=sda
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024

%post
%end

%packages
@development
@mail-server

%end
EOF
cat <<EOF >/var/lib/cobbler/kickstarts/ks-centos7.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted \$1\$NxU3dKmV\$fRJv0NpyNJv6sQSK2SEh//
# System language
lang en_US
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=eth0
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url=\$tree
# System bootloader configuration
bootloader --append="net.ifnames=0" --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024  

%post
%end

%packages
@development
@mail-server

%end
EOF
cat <<EOF >/var/lib/cobbler/kickstarts/ks-centos8.cfg
#version=RHEL8
ignoredisk --only-use=sda
# Partition clearing information
# Use graphical install
text
zerombr
reboot
clearpart --all --initlabel
selinux --disabled
firewall --disabled
# Use CDROM installation media
url --url=\$tree
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

# Network information
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
network  --bootproto=dhcp --device=eth0 --ipv6=auto --activate
network  --hostname=localhost.localdomain
# Root password
rootpw --iscrypted \$6\$q76Q7kMy.HLaa0Jc\$pN1iBqDhYaNZNqLxV.8Qz8QwxgRYB3NXt7/1ikn4buFXhI8GrYUj3hzYhN5Bdw5LzmmZAbXEg9KNx8cC67J73.
# Run the Setup Agent on first boot
firstboot --enable
# Do not configure the X Window System
skipx
# System services
services --disabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc --nontp
#Disk partitioning information
part /boot --fstype="ext4" --size=2048
part / --fstype="ext4" --size=10240
part /data --fstype="ext4" --size=2048
part swap --fstype="swap" --size=1024  


%packages
@^minimal-environment
vim
tree
wget
curl
%end

%addon com_redhat_kdump --disable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
EOF
[ -e /var/lib/cobbler/kickstarts/ks-centos6.cfg ] && action "centos6应答文件已经生成" || action "centos6应答文件生成失败" false
[ -e /var/lib/cobbler/kickstarts/ks-centos7.cfg ] && action "centos7应答文件已经生成" || action "centos7应答文件生成失败" false
[ -e /var/lib/cobbler/kickstarts/ks-centos8.cfg ] && action "centos8应答文件已经生成" || action "centos8应答文件生成失败" false
}

cobbler_put_os(){
read -p "上传系统名称:(推荐使用centos-8.2) " OS_NAME
read -p "系统磁盘挂载路径:(示范/mnt/centos8.2) " FILE
echo "上传过程较慢请耐心等待....."
echo "上传位置为/var/www/cobbler/ks_mirror/${OS_NAME}-x86_64"
cobbler import --name=${OS_NAME}-x86_64 --path=${FILE} --arch=x86_64 && action "上传完成" || action "上传失败" false
}

ks_install(){
[ -e /var/lib/cobbler/kickstarts/ks-centos8.cfg ] || { action "请先生成应答文件" false;return; }
[ -e /var/lib/cobbler/kickstarts/ks-centos7.cfg ] || { action "请先生成应答文件" false;return; }
[ -e /var/lib/cobbler/kickstarts/ks-centos6.cfg ] || { action "请先生成应答文件" false;return; }
echo "现有镜像:"
echo "`cobbler profile list`"
read -p "请输入要应用的系统镜像名称:" NAME_OS
{ cobbler profile list | grep $NAME_OS; } || { action "没有这个镜像" false;return;}
read -p "请确认centos版本(6,7,8):" VERSION
if [ $VERSION -eq 6 ] &>/dev/null;then
    i=6
elif [ $VERSION -eq 7 ] &>/dev/null;then
    i=7
elif [ $VERSION -eq 8 ] &>/dev/null;then
    i=8
else
   action "输入错误" false ; return
fi
cobbler profile remove --name=${NAME_OS}
cobbler profile add --name=${NAME_OS}  --distro=${NAME_OS} --kickstart=/var/lib/cobbler/kickstarts/ks-centos${i}.cfg && action "应用成功" || action "应用失败" false

}







PS3="请选择相应的编号(1-5):"
MENU=" 
cobbler安装
cobbler上传系统镜像
cobbler自定义应答文件生成
cobbler使用应答文件
退出
"

select menu in $MENU;do
case $REPLY in
1)
    cobbler_install
    ;;
2)
    cobbler_put_os
    ;;
3)
    create_ks
    ;;
4)
    ks_install  
    ;;
5)
    break
    ;;
*)
    echo -e "输入错误,请输入正确的数字(1-5)"
    ;;
esac
done

注意事项:

  • 部署只能部署在centos7系统
  • 上传镜像需注意无法上传centos8.3镜像,有报错信息。有解决方案自行百度
  • 上传镜像后直接可以进行系统部署,部署方式为默认最小化,部署后root密码为123456
  • 如要使用自定义应答文件请正确选择版本否则部署会报错