Cobbler無人值守安裝

1. Cobbler介紹

Cobbler是一個Linux服務器安裝的服務,能夠經過網絡啓動(PXE)的方式來快速安裝、重裝物理服務器和虛擬機,同時還能夠管理DHCP,DNS等。node

Cobbler能夠使用命令行方式管理,也提供了基於Web的界面管理工具(cobbler-web),還提供了API接口,能夠方便二次開發使用。python

Cobbler是較早前的kickstart的升級版,優勢是比較容易配置,還自帶web界面比較易於管理。linux

Cobbler內置了一個輕量級配置管理系統,但它也支持和其它配置管理系統集成,如Puppet,暫時不支持SaltStack。ios

Cobbler集成的服務:git

PXE服務支持
DHCP服務管理
DNS服務管理(可選bind,dnsmasq)
電源管理
Kickstart服務支持
YUM倉庫管理
TFTP(PXE啓動時須要)
Apache(提供kickstart的安裝源,並提供定製化的kickstart配置)github

二、系統環境準備

[root@linux-node2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@linux-node2 ~]# uname -r
3.10.0-693.5.2.el7.x86_64
[root@linux-node2 ~]#  getenforce
Disabled
[root@linux-node2 ~]# systemctl status firewalld
Unit firewalld.service could not be found.
[root@linux-node2 ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $3}'
192.168.56.12
[root@linux-node2 ~]# hostname
linux-node2
#配置阿里雲epel源
[root@linux-node2 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

三、Cobbler安裝配置

3.1Cobbler安裝

[root@linux-node2 ~]# yum install -y httpd tftp dhcp cobbler cobbler-web pykickstart
[root@linux-node2 ~]# rpm -ql cobbler
/etc/cobbler                  # 配置文件目錄
/etc/cobbler/settings         # cobbler主配置文件,這個文件是YAML格式,Cobbler是python寫的程序。
/etc/cobbler/dhcp.template    # DHCP服務的配置模板
/etc/cobbler/tftpd.template   # tftp服務的配置模板
/etc/cobbler/rsync.template   # rsync服務的配置模板
/etc/cobbler/iso              # iso模板配置文件目錄
/etc/cobbler/pxe              # pxe模板文件目錄
/etc/cobbler/power            # 電源的配置文件目錄
/etc/cobbler/users.conf       # Web服務受權配置文件
/etc/cobbler/users.digest     # 用於web訪問的用戶名密碼配置文件
/etc/cobbler/dnsmasq.template # DNS服務的配置模板
/etc/cobbler/modules.conf     # Cobbler模塊配置文件
/var/lib/cobbler              # Cobbler數據目錄
/var/lib/cobbler/config       # 配置文件
/var/lib/cobbler/kickstarts   # 默認存放kickstart文件
/var/lib/cobbler/loaders      # 存放的各類引導程序
/var/www/cobbler              # 系統安裝鏡像目錄
/var/www/cobbler/ks_mirror    # 導入的系統鏡像列表
/var/www/cobbler/images       # 導入的系統鏡像啓動文件
/var/www/cobbler/repo_mirror  # yum源存儲目錄
/var/log/cobbler              # 日誌目錄
/var/log/cobbler/install.log  # 客戶端系統安裝日誌
/var/log/cobbler/cobbler.log  # cobbler日誌

3.2 cobbler配置

[root@linux-node2 ~]# systemctl start httpd
[root@linux-node2 ~]# systemctl start cobblerd
[root@linux-node2 ~]# cobbler check      #經過該命令能夠查看cobbler的服務是否正常,須要解決哪些問題
The following are potential configuration items that you may want to fix:

1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work.  This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely.  Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.
5 : enable and start rsyncd.service with systemctl
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

Restart cobblerd and then run 'cobbler sync' to apply changes.
# 看着上面的結果,一個一個解決。
#1.2問題server,Cobbler服務器的IP。
[root@linux-node2 ~]# vim /etc/cobbler/settings 
384 server: 192.168.56.12
272 next_server: 192.168.56.12
#3問題解決啓用tftp
[root@linux-node2 ~]# yum install -y xinetd
[root@linux-node2 ~]# vim /etc/xinetd.d/tftp 
disable                 = no
[root@linux-node2 ~]# systemctl start xinetd
#4問題解決bootload的插件安裝,會自動從官網下載
[root@linux-node2 ~]# cobbler get-loaders
task started: 2018-01-11_155827_get_loaders
task started (id=Download Bootloader Content, time=Thu Jan 11 15:58:27 2018)
downloading https://cobbler.github.io/loaders/README to /var/lib/cobbler/loaders/README
downloading https://cobbler.github.io/loaders/COPYING.elilo to /var/lib/cobbler/loaders/COPYING.elilo
downloading https://cobbler.github.io/loaders/COPYING.yaboot to /var/lib/cobbler/loaders/COPYING.yaboot
downloading https://cobbler.github.io/loaders/COPYING.syslinux to /var/lib/cobbler/loaders/COPYING.syslinux
downloading https://cobbler.github.io/loaders/elilo-3.8-ia64.efi to /var/lib/cobbler/loaders/elilo-ia64.efi
downloading https://cobbler.github.io/loaders/yaboot-1.3.17 to /var/lib/cobbler/loaders/yaboot
downloading https://cobbler.github.io/loaders/pxelinux.0-3.86 to /var/lib/cobbler/loaders/pxelinux.0
downloading https://cobbler.github.io/loaders/menu.c32-3.86 to /var/lib/cobbler/loaders/menu.c32
downloading https://cobbler.github.io/loaders/grub-0.97-x86.efi to /var/lib/cobbler/loaders/grub-x86.efi
downloading https://cobbler.github.io/loaders/grub-0.97-x86_64.efi to /var/lib/cobbler/loaders/grub-x86_64.efi
*** TASK COMPLETE ***
#5問題解決rsyncd服務依賴
[root@linux-node2 ~]# systemctl enable rsyncd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@linux-node2 ~]# systemctl start  rsyncd.service
#7問題解決設置新裝系統的默認root密碼123123。下面的命令來源於提示7。random-phrase-here爲干擾碼,能夠自行設定。
[root@linux-node2 ~]# openssl passwd -1 -salt '123123' '123123'
$1$123123$MAV.kVI/b3swmFLErPD2b0
[root@linux-node2 ~]# vim /etc/cobbler/settings 
101 default_password_crypted: "$1$123123$MAV.kVI/b3swmFLErPD2b0"

#重啓cobbler服務,再檢查
[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : debmirror package is not installed, it will be required to manage debian deployments and repositories    # 和debian系統相關,不須要
2 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them  # fence設備相關,不須要

Restart cobblerd and then run 'cobbler sync' to apply changes.

3.3配置DHCP

# 修改cobbler的dhcp模版,不要直接修改dhcp自己的配置文件,由於cobbler會覆蓋。
[root@linux-node2 ~]# vim /etc/cobbler/settings 
manage_dchp: 1   #啓用dchp服務
[root@linux-node2 ~]# vim /etc/cobbler/dhcp.template 
subnet 192.168.56.0 netmask 255.255.255.0 {
     option routers             192.168.56.2;
     option domain-name-servers 192.168.56.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.56.100 192.168.56.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

3.4同步cobbler配置

[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# cobbler sync
task started: 2018-01-11_161740_sync
task started (id=Sync, time=Thu Jan 11 16:17:40 2018)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/images
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying images
generating PXE configuration files
generating PXE menu structure
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout: 
received on stderr: 
running: service dhcpd restart
received on stdout: 
received on stderr: Redirecting to /bin/systemctl restart dhcpd.service

running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***

四、Cobbler導入鏡像

4.一、Cobbler命令

cobbler check    覈對當前設置是否有問題
cobbler list     列出全部的cobbler元素
cobbler report   列出元素的詳細信息
cobbler sync     同步配置到數據目錄,更改配置最好都要執行下
cobbler reposync 同步yum倉庫
cobbler distro   查看導入的發行版系統信息
cobbler system   查看添加的系統信息
cobbler profile  查看配置信息

4.二、Cobbler導入鏡像

#此處上傳的centos官網鏡像千萬千萬別用Minimal,雖然有幾百兆,可是沒法安裝kickstarts的文件指定的軟件包,已踩坑!!!
[root@linux-node2 ~]# mount -o loop /opt/CentOS-7-x86_64-DVD-1708.iso /mnt/    # 掛載CentOS7.4的系統鏡像。
mount: /dev/loop0 寫保護,將以只讀方式掛載
[root@linux-node2 ~]# cobbler import --path=/mnt/ --name=Centos-7.4-x86_64 --arch=x86_64
# --path 鏡像路徑
# --name 爲安裝源定義一個名字
# --arch 指定安裝源是32位、64位、ia64, 目前支持的選項有: x86│x86_64│ia64
# 安裝源的惟一標示就是根據name參數來定義,本例導入成功後,安裝源的惟一標示就是:CentOS-7.1-x86_64,若是重複,系統會提示導入失敗。
task started: 2018-01-11_163234_import
task started (id=Media import, time=Thu Jan 11 16:32:34 2018)
Found a candidate signature: breed=redhat, version=rhel6
Found a candidate signature: breed=redhat, version=rhel7
Found a matching signature: breed=redhat, version=rhel7
Adding distros from path /var/www/cobbler/ks_mirror/Centos-7.4-x86_64:
creating new distro: Centos-7.4-x86_64
trying symlink: /var/www/cobbler/ks_mirror/Centos-7.4-x86_64 -> /var/www/cobbler/links/Centos-7.4-x86_64
creating new profile: Centos-7.4-x86_64
associating repos
checking for rsync repo(s)
checking for rhn repo(s)
checking for yum repo(s)
starting descent into /var/www/cobbler/ks_mirror/Centos-7.4-x86_64 for Centos-7.4-x86_64
processing repo at : /var/www/cobbler/ks_mirror/Centos-7.4-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/Centos-7.4-x86_64
looking for /var/www/cobbler/ks_mirror/Centos-7.4-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/Centos-7.4-x86_64/repodata
*** TASK COMPLETE ***
[root@linux-node2 ~]# ll /var/www/cobbler/ks_mirror/     #導入鏡像的位置
總用量 4
drwxr-xr-x 8 root root 4096 9月   5 21:36 Centos-7.4-x86_64
drwxr-xr-x 2 root root   35 1月  11 16:34 config
[root@linux-node2 ~]# cobbler list     #查看鏡像
distros:
   Centos-7.4-x86_64

profiles:
   Centos-7.4-x86_64

systems:

repos:

images:

mgmtclasses:

packages:

files:

4.三、指定ks.cfg文件及調整內核參數

(1)默認的ks.cfg文件
[root@linux-node2 ~]# ll /var/lib/cobbler/kickstarts
總用量 56
-rw-r--r-- 1 root root  115 9月  17 01:33 default.ks
-rw-r--r-- 1 root root   22 9月  17 01:33 esxi4-ks.cfg
-rw-r--r-- 1 root root   22 9月  17 01:33 esxi5-ks.cfg
drwxr-xr-x 2 root root   54 1月  11 15:54 install_profiles
-rw-r--r-- 1 root root 1424 9月  17 01:33 legacy.ks
-rw-r--r-- 1 root root  292 9月  17 01:33 pxerescue.ks
-rw-r--r-- 1 root root 2825 9月  17 01:33 sample_autoyast.xml
-rw-r--r-- 1 root root 1856 9月  17 01:33 sample_end.ks
-rw-r--r-- 1 root root    0 9月  17 01:33 sample_esx4.ks
-rw-r--r-- 1 root root  324 9月  17 01:33 sample_esxi4.ks
-rw-r--r-- 1 root root  386 9月  17 01:33 sample_esxi5.ks
-rw-r--r-- 1 root root  386 9月  17 01:33 sample_esxi6.ks
-rw-r--r-- 1 root root 1913 9月  17 01:33 sample.ks
-rw-r--r-- 1 root root 3419 9月  17 01:33 sample_old.seed
-rw-r--r-- 1 root root 6658 9月  17 01:33 sample.seed
(2)查看全部profile的設置
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.ks
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm
[root@linux-node2 ~]# cobbler profile list    #查看可用的鏡像
   Centos-7.4-x86_64
(3)# 編輯profile,修改關聯的ks文件
[root@linux-node2 ~]# cobbler profile edit --name=Centos-7.4-x86_64 --kickstart=/var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg 
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg    #關聯後,此處會變成修改的配置
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm
(4)修改安裝系統的內核參數,修改網卡名稱
[root@linux-node2 ~]# cobbler profile edit --name=Centos-7.4-x86_64 --kopts='net.ifnames=0 biosdevname=0'
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {'biosdevname': '0', 'net.ifnames': '0'}     #此處會增長內核選項參數
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm
[root@linux-node2 ~]# cobbler sync    #同步

五、安裝系統

5.1去除vm的DHCP

Cobbler無人值守安裝

5.2開始安裝

新建一臺虛擬機(不解釋)內存給2G否則坑呀
Cobbler無人值守安裝
選擇Centos-7.4-x86_64
Cobbler無人值守安裝web

5.3.Centos-7.4-x86_64.cfg文件

# Cobbler for Kickstart Configurator for CentOS 7.4 by Jason Zhao
# System language
lang en_US
# System keyboard
keyboard us
# System timezone
timezone Asia/Shanghai
#Root password
rootpw  --iscrypted $default_password_crypted
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
url --url=$tree
#System bootloader configuration
bootloader --location=mbr 
#Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype xfs --size 1024 --ondisk sda
part swap --size 1024 --ondisk sda
part / --fstype xfs --size 1 --grow --ondisk sda
#System authorization --enablemd5
auth --useshadow --enablemd5
# Network information
$SNIPPET('network_config')
reboot
firewall --disabled
selinux --disabled
skipx

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
$SNIPPET('pre_anamon')
%end

%packages
@base
@core
tree
nmap
sysstat
lrzsz
dos2unix
telnet
iptraf
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
screen
%end

%post
systemctl disable postfix.service
%end

六、使用koan重裝系統

在新安裝的系統中安裝koan
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y koan
[root@localhost ~]# koan --server=192.168.56.12 --list=profiles   #查看可以使用的安裝包
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
Centos-7.4-x86_64
Centos-7.2-x86_64
[root@localhost ~]# koan --replace-self --server=92.168.56.12 --profile=Centos-7.4-x86_64
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
- reading URL: http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64
install_tree: http://192.168.56.12/cblr/links/Centos-7.4-x86_64
downloading initrd initrd.img to /boot/initrd.img_koan
url=http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/initrd.img
- reading URL: http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/initrd.img
downloading kernel vmlinuz to /boot/vmlinuz_koan
url=http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/vmlinuz
- reading URL: http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/vmlinuz
- ['/sbin/grubby', '--add-kernel', '/boot/vmlinuz_koan', '--initrd', '/boot/initrd.img_koan', '--args', '"ks=http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64 ksdevice=link kssendmac lang= text "', '--copy-default', '--make-default', '--title=kick1515987084']
- ['/sbin/grubby', '--update-kernel', '/boot/vmlinuz_koan', '--remove-args=root']
- reboot to apply changes
[root@localhost ~]# reboot   #重啓自動安裝

#那麼koan是怎麼實現重裝的呢?koan會在grub增長一個啓動項,從告訴系統從該啓動項進行啓動
[root@localhost grub2]# cat grub.cfg |grep "linux16"
    linux16 /vmlinuz_koan ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 ks=http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64 ksdevice=link kssendmac lang= text
    linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=UUID=e9311a84-0787-488b-ab34-3c80d81d86eb ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
    linux16 /vmlinuz-0-rescue-ee1532c207654bf4b416357cc26f7eee root=UUID=e9311a84-0787-488b-ab34-3c80d81d86eb ro crashkernel=auto rhgb quiet

Cobbler無人值守安裝
到此就能實現自動重裝了!shell

七、Cobbler的圖形界面

cobbler的圖形界面須要安裝cobbler_web,前面已經安裝,啓動httpd後,直接訪問:https://192.168.56.12/cobbler_web,默認的用戶名密碼是cobbler,cobbler
在圖形界面能夠對導入鏡像,kickstarts文件的修改與配置等操做。

Cobbler無人值守安裝

[root@linux-node2 ~]# cat /etc/cobbler/users.digest      #cobbler的密碼存放位置
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
[root@linux-node2 ~]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler    #修改cobbler界面的密碼
Changing password for user cobbler in realm Cobbler
New password: 123123
Re-type new password: 123123
[root@linux-node2 ~]# cat /etc/cobbler/users.digest 
cobbler:Cobbler:37af732f18fe2c533372649699188af3

Cobbler無人值守安裝

八、Cobbler的基本組件

Cobbler無人值守安裝

Disribution : 發行內核,initrd等東西
Repository: 建立倉庫,好比yum倉庫等。
system: 經過mac地址來定製化系統
profile: 對須要安裝某個系統的全部配置。

九、建立yum倉庫

cobbler不但能夠裝機,還能夠自建yum倉庫,這個倉庫能夠從公網的yum源進行同步到公司內網,節省帶寬。同步命令以下所示:vim

#添加阿里雲的epel源
cobbler repo add --name=Centos-7.x86-64-epel --mirror=https://mirrors.aliyun.com/epel/7Server/x86_64/ --arch=x86_64 --breed=yum

#添加阿里雲的openstack-newton源
cobbler repo add --name=openstack-newton --mirror=https://mirrors.aliyun.com/centos/7.4.1708/cloud/x86_64/openstack-newton/ --arch=x86_64 --breed=yum 
cobbler reposync --tries=3 --no-fail

#參數解釋:

repo add : 添加一個repo源
name : 爲這個yum源命名
mirror : 指定一個源的地址。
arch : 指定平臺
breed: 類型爲yum

#演示:
[root@linux-node2 opt]# cobbler repo add --name=openstack-newton --mirror=https://mirrors.aliyun.com/centos/7.4.1708/cloud/x86_64/openstack-newton/ --arch=x86_64 --breed=yum   #添加openstack-newton源
[root@linux-node2 opt]# cobbler profile edit --name=Centos-7.4-x86_64 --repos=openstack-newton    #關聯repos倉庫
[root@linux-node2 opt]# cobbler profile report
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : ['openstack-newton']             #添加成功
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

以上的操做也能夠在cobbler界面上,把yum源添加到Centos-7.4-x86_64的profile中。如圖:

Cobbler無人值守安裝

十、Cobbler定製安裝

(1)生成mac地址

Cobbler無人值守安裝

(2)定製系統

[root@linux-node2 ~]# cobbler system add --name=linux-node4 --mac=00:50:56:33:F1:45 \
> --profile=Centos-7.3-x86_64 \
> --ip-address=192.168.56.110 --subnet=255.255.255.0 \
> --gateway=192.168.56.2 --interface=eth0 \
> --static=1 --hostname=linux-node4.example.com \
> --name-servers="192.168.56.2" \
> --kickstart=/var/lib/cobbler/kickstarts/Centos-7.3-x86_64.cfg 
[root@linux-node2 ~]# cobbler system list
   linux-node4

參數解釋:

system add 添加一個系統定製
name 定義這個新添加的系統定製的名字
mac 指定MAC
profile 指定profile,經過cobbler profile list查看
ip-address 指定靜態IP
subnet 指定子網掩碼
gateway 指定網關
interface 指定網卡
static=1 設置爲靜態IP
hostname 這是主機名
name-servers 設置dns服務器
kickstart 設置kickstart,經過cobbler profile report來查看。

(3)開機自動安裝

建立一個虛擬機,mac地址爲00:50:56:33:F1:45,啓動後你就會發現自動進入安裝系統了,等安裝完之後,全部的配置都和咱們當初設置的同樣。
Cobbler無人值守安裝centos

十一、Cobbler的API

cobbler也是經過restful-api來調用對應的接口,下面請看兩個小腳本:
都是採用opython2.7版本運行的

[root@linux-node2 ~]# cat cobbler_list.py
#!/usr/bin/python
import xmlrpclib
server = xmlrpclib.Server("http://192.168.56.12/cobbler_api")
print server.get_distros()
print server.get_profiles()
print server.get_systems()
print server.get_images()
print server.get_repos()

使用api的方式,定製安裝系統,python腳本以下:

[root@linux-node2 ~]# cat cobbler-api.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmlrpclib

class CobblerAPI(object):
    def __init__(self,url,user,password):
        self.cobbler_user= user
        self.cobbler_pass = password
        self.cobbler_url = url

    def add_system(self,hostname,ip_add,mac_add,profile):
        '''
        Add Cobbler System Infomation
        '''
        ret = {
            "result": True,
            "comment": [],
        }
        #get token
        remote = xmlrpclib.Server(self.cobbler_url)
        token = remote.login(self.cobbler_user,self.cobbler_pass)

        #add system
        system_id = remote.new_system(token)
        remote.modify_system(system_id,"name",hostname,token)
        remote.modify_system(system_id,"hostname",hostname,token)
        remote.modify_system(system_id,'modify_interface', {
            "macaddress-eth0" : mac_add,
            "ipaddress-eth0" : ip_add,
            "dnsname-eth0" : hostname,
        }, token)
        remote.modify_system(system_id,"profile",profile,token)
        remote.save_system(system_id, token)
        try:
            remote.sync(token)
        except Exception as e:
            ret['result'] = False
            ret['comment'].append(str(e))
        return ret

def main():
    cobbler = CobblerAPI("http://192.168.56.12/cobbler_api","cobbler","123123")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.56.101',mac_add='00:50:56:33:F1:45',profile='CentOS-7.3-x86_64')
    print ret

if __name__ == '__main__':
main()
相關文章
相關標籤/搜索