Cobbler 自动部署CentOS

  1 #!/bin/bash
  2 
  3 #  配置管理IP
  4 #vi /etc/sysconfig/network-scripts/ifcfg-ens192
  5 
  6 #  配置pxe IP    192.168.0.1/24    不能更换成其他IP
  7 #vi /etc/sysconfig/network-scripts/ifcfg-ens224
  8 
  9 #  配置主机名    pxe-server
 10 #hostname pxe-server
 11 #echo pxe-server > /etc/hostname
 12 
 13 #  创建存放iso的目录    /mnt/iso
 14 #mkdir -p /mnt/iso
 15 
 16 #  下载最新版 CentOS6 和 CentOS7 iso文件(随着版本的更新,镜像网站将停止老版本的下载支持,需要自己搞定iso下载)
 17 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD1.iso
 18 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-6.10-x86_64-bin-DVD2.iso
 19 #wget -P /mnt/iso ftp://10.12.28.8/ops/Linux-ISO/CentOS-7-x86_64-Everything-1908.iso
 20 
 21 ###############       上面的部分手工执行      #####################
 22 
 23 #  配置阿里云YUM源
 24 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 25 
 26 #  安装EPEL
 27 yum install -y epel-release
 28 
 29 #  配置阿里云EPEL源
 30 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
 31 
 32 #  安装常用软件
 33 yum install -y vim wget lftp net-tools bash-completion jq git sysstat lrzsz
 34 
 35 #  禁用防火墙
 36 systemctl  stop  firewalld && systemctl  disable  firewalld
 37 
 38 #  关闭SELinux
 39 setenforce  0  &&  getenforce
 40 sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
 41 
 42 #  优化SSH访问速度
 43 echo 'UseDNS no' >> /etc/ssh/sshd_config
 44 systemctl restart sshd.service
 45 echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config
 46 
 47 #  定义操作系统密码
 48 passwd='cnblogs.C0M'
 49 
 50 #  定义Cobbler Server 地址
 51 server=192.168.0.1
 52 
 53 #  定义TFTP Server地址
 54 next_server=$server
 55 
 56 #  DHCP定义:网络地址
 57 subnet=192.168.0.0
 58 
 59 #  DHCP定义:掩码
 60 netmask=255.255.255.0
 61 
 62 #  DHCP定义:网关
 63 gateway=$server
 64 
 65 #  DHCP定义:DNS
 66 dns=$server
 67 
 68 #  DHCP定义:地址段起
 69 ip_start=192.168.0.101
 70 
 71 #  DHCP定义:地址段止
 72 ip_stop=192.168.0.200
 73 
 74 #  安装cobbler及相关服务
 75 yum install -y cobbler cobbler-web pykickstart dhcp
 76 
 77 #  设置cobbler动态修改配置,比改配置文件要方便
 78 sed -i  's/allow_dynamic_settings: 0/allow_dynamic_settings: 1/' /etc/cobbler/settings
 79 
 80 #  启动相关服务,并设置为开机自启动,因为dhcp还没有配置,暂时先不启动
 81 systemctl start cobblerd.service && systemctl enable cobblerd.service
 82 systemctl start httpd.service    && systemctl enable httpd.service
 83 systemctl start rsyncd.service   && systemctl enable rsyncd.service
 84 systemctl start tftp.socket      && systemctl enable tftp.socket
 85 systemctl enable dhcpd.service
 86 
 87 #  DHCP配置:网络地址
 88 sed -i 's@192.168.1.0@'$subnet'@' /etc/cobbler/dhcp.template
 89 
 90 #  DHCP配置:掩码
 91 sed -i 's@255.255.255.0@'$netmask'@' /etc/cobbler/dhcp.template
 92 
 93 #  DHCP配置:网关
 94 sed -i 's@192.168.1.5;@'$gateway';@' /etc/cobbler/dhcp.template
 95 
 96 #  DHCP配置:DNS
 97 sed -i 's@192.168.1.1;@'$dns';@' /etc/cobbler/dhcp.template
 98 
 99 #  DHCP配置:地址段起
100 sed -i 's@192.168.1.100@'$ip_start'@' /etc/cobbler/dhcp.template
101 
102 #  DHCP配置:地址段止
103 sed -i 's@192.168.1.254@'$ip_stop'@' /etc/cobbler/dhcp.template
104 
105 #  设置cobbler server对外能访问的ip
106 cobbler setting edit --name=server --value=$server
107 
108 #  设置next_server,即tftp的服务器地址
109 cobbler setting edit --name=next_server --value=$server
110 
111 #  设置有cobbler自动管理dhcp
112 cobbler setting edit --name=manage_dhcp --value=1
113 
114 #  在tftp配置文件中设置启用tftp
115 sed -i '14s/yes/no/' /etc/xinetd.d/tftp
116 
117 #  在线从cobbler官网下载  boot loaders
118 cobbler get-loaders
119 
120 #  cobbler配置完毕,执行下面命令重新加载配置,并自动配置相关服务
121 cobbler sync
122 
123 #  创建centos ISO挂载目录
124 mkdir -p /mnt/centos/centos6
125 mkdir -p /mnt/centos/centos7
126 
127 #  挂载centos ISO
128 mount /mnt/iso/CentOS-6.10-x86_64-bin-DVD1.iso /mnt/centos/centos6
129 mount /mnt/iso/CentOS-7-x86_64-Everything-1908.iso /mnt/centos/centos7
130 
131 # 创建centos ks文件
132 echo '
133 #platform=x86, AMD64, or Intel EM64T
134 #version=DEVEL
135 # Install OS instead of upgrade
136 install
137 # System keyboard
138 keyboard us
139 # System language
140 lang en_US
141 # Root password
142 rootpw --plaintext '$passwd'
143 # System authorization information
144 auth  --useshadow  --passalgo=sha512
145 # Use graphical install
146 graphical
147 firstboot --disable
148 # SELinux configuration
149 selinux --disabled
150 # Firewall configuration
151 firewall --disabled
152 # Reboot after installation
153 reboot
154 # System timezone
155 timezone Asia/Shanghai
156 # System bootloader configuration
157 bootloader --location=mbr
158 # Clear the Master Boot Record
159 zerombr
160 # Partition clearing information
161 clearpart --all
162 
163 # Use network installation
164 url --url=$tree
165 # Disk partitioning information
166 part /boot --fstype="ext4" --size=1024
167 part swap --fstype="swap" --size=2048
168 part / --fstype="ext4" --grow --size=1
169 
170 %packages
171 @base
172 %end
173 ' > /var/lib/cobbler/kickstarts/ks6.cfg
174 
175 echo '
176 #platform=x86, AMD64, or Intel EM64T
177 #version=DEVEL
178 # Install OS instead of upgrade
179 install
180 # System keyboard
181 keyboard us
182 # System language
183 lang en_US
184 # Root password
185 rootpw --plaintext '$passwd'
186 # System authorization information
187 auth  --useshadow  --passalgo=sha512
188 # Use graphical install
189 graphical
190 firstboot --disable
191 # SELinux configuration
192 selinux --disabled
193 # Firewall configuration
194 firewall --disabled
195 # Reboot after installation
196 reboot
197 # System timezone
198 timezone Asia/Shanghai
199 # System bootloader configuration
200 bootloader --location=mbr
201 # Clear the Master Boot Record
202 zerombr
203 # Partition clearing information
204 clearpart --all
205 
206 # Use network installation
207 url --url=$tree
208 # Disk partitioning information
209 part /boot --fstype="xfs" --size=1024
210 part swap --fstype="swap" --size=2048
211 part / --fstype="xfs" --grow --size=1
212 
213 %packages
214 @base
215 %end
216 ' >/var/lib/cobbler/kickstarts/ks7.cfg
217 
218 #  cobbler导入centos
219 cobbler import --name=centos610 --path=/mnt/centos/centos6 --kickstart=/var/lib/cobbler/kickstarts/ks6.cfg
220 cobbler import --name=centos77 --path=/mnt/centos/centos7 --kickstart=/var/lib/cobbler/kickstarts/ks7.cfg
221 
222 echo -e "Cobbler部署完毕,可以自动安装centos6.10 和centos7.7。
另外还可以使用Cobbler Web进行管理 
   https://$server/cobbler_web 
   用户名 cobbler 
   密码 cobbler"
原文地址:https://www.cnblogs.com/www1707/p/12629167.html