cobbler服务端部署脚本

'执行之前先检查自己的防火墙和selinux是否关闭,然后在看光盘是否挂载成功'
[root@yaoxiaorong ~]# vim cobbler.sh 
#!/bin/bash
server_IP=$(ip a|grep 'inet '|egrep -v '127.0.0.1|grep'|awk -F '[ /]+' '{print $3}')

#before installation close firewalld and selinux,then must reboot ,last mount centos CD-ROM
#setenforce 0
#sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
#systemctl stop firewalld
#systemctl disable firewalld
#mount /dev/cdrom /mnt

#wget 163.repo
rpm -e epel-release
cd /etc/yum.repos.d/
mv * /tmp/
curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
sed -i 's/\$releasever/7/g' /etc/yum.repos.d/163.repo
sed -i 's/enabled=0/enabled=1/g' /etc/yum.repos.d/163.repo
yum clean all &>/dev/null
if [ $? -ne 0 ];then
    exit 1
fi
yum clean all && yum -y install epel-release && yum -y install httpd dhcp tftp python-ctypes cobbler xinetd cobbler-web pykickstart

for soft in httpd dhcp tftp cobbler xinetd cobbler-web pykickstart;do
    rpm -qa|grep $soft &>/dev/null
    if [ $? -ne 0 ];then
        yum -y install $soft
    fi
done

#server start and enable
systemctl start httpd
systemctl start cobblerd
systemctl enable httpd
systemctl enable cobblerd

#update server ip and tftp ip
sed -i "/^server/s/^/#/g" /etc/cobbler/settings
sed -i "/^#server/a server: $server_IP" /etc/cobbler/settings
sed -i "s/^next_server: .*/next_server: $server_IP/" /etc/cobbler/settings
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp

#wget defect file
cobbler get-loaders

#rsyncd start
systemctl start rsyncd
systemctl enable rsyncd

mypassword=$(openssl passwd -1 -salt "$RANDOM" 'yaoxiaorong!')
sed -i '/default_password_crypted/s/^/#/g' /etc/cobbler/settings
sed -i "/^#default_password_crypted/a default_password_crypted: \"$mypassword\"" /etc/cobbler/settings

#reboot cobbler
systemctl restart cobblerd
sleep 10
ss -antl

cobbler check

#configure cobbler dncp
sed -i '/^manage_dhcp/s/0/1/g' /etc/cobbler/settings
sed -n '/^manage_dhcp/p' /etc/cobbler/settings

#configure dhcp
sed -i 's/subnet 192.168.1.0/subnet 192.168.228.0/g'  /etc/cobbler/dhcp.template
sed -i 's/option routers             192.168.1.5;/option routers             192.168.228.21;/g' /etc/cobbler/dhcp.template
sed -i 's/option domain-name-servers 192.168.1.1;/option domain-name-servers 192.168.228.21;/g' /etc/cobbler/dhcp.template
sed -i 's/range dynamic-bootp        192.168.1.100 192.168.1.254;/range dynamic-bootp        192.168.228.100 192.168.228.250;/g' /etc/cobbler/dhcp.template

#restart cobblerd
systemctl restart cobblerd

sleep 10
cobbler sync

netstat -anulp|grep dhcp

#centos cd-rom
cobbler import --path=/mnt --name=centos-7 --arch=x86_64

cobbler list

#rootpw change to your own home rootpw,your own home rootpw on /root/anaconda-ks.cfg,you need witer there
cat > /var/lib/cobbler/kickstarts/centos-7-x86_64.ks <<'EOF'
auth --enableshadow --passalgo=sha512
bootloader --location=mbr
clearpart --all --initlabel
part /boot --asprimary --fstype="ext4" --size=500
part swap --fstype="swap" --size=4096
part / --fstype="ext4" --grow --size=15000
text
firewall --disabled
firstboot --disable
keyboard us
lang en_US
url --url=http://192.168.228.21/cobbler/ks_mirror/centos-7-x86_64
$yum_repo_stanza
$SNIPPET('network_config')
reboot

rootpw --iscrypted $6$vBj.ryD5bKP/Gftg$5B5dgKMuxz2keM2iUglMOx0gT0Otvoohf0ZaNVafSRwDJA.tfL.jDGNDGjHh21p6awnEQ4KUE7PRENVz4H62v.
selinux --disabled
skipx
timezone Asia/Shanghai --isUtc --nontp
install
zerombr

%packages
@^minimal
@core
kexec-tools

%end

%addon com_redhat_kdump --enable --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

cobbler validateks

cobbler profile list

cobbler profile edit --name centos-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos-7-x86_64.ks

cobbler profile edit --name centos-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

cobbler profile report

cobbler sync

systemctl restart xinetd
systemctl restart cobblerd
systemctl restart httpd

ss -antl

'然后在客户端直接安装虚拟机即可'