首先,創建并編輯自定義腳本(如用于系統初始化、軟件安裝等)??梢允褂?code>nano、vim
等工具,例如:
sudo nano /var/lib/cobbler/snippets/custom_init_script.sh
在腳本中編寫邏輯(以下為示例,用于安裝Nginx并啟動服務):
#!/bin/bash
# 更新軟件包索引
apt-get update -y
# 安裝Nginx
apt-get install -y nginx
# 啟動Nginx并設置開機自啟
systemctl start nginx
systemctl enable nginx
# 輸出日志
echo "$(date): Nginx installed and started successfully" >> /var/log/custom_init.log
保存并退出編輯器后,賦予腳本執行權限:
sudo chmod +x /var/lib/cobbler/snippets/custom_init_script.sh
Cobbler通過Snippet機制復用腳本片段,自定義腳本需放置在/var/lib/cobbler/snippets/
目錄下(該目錄用于存放可復用的腳本片段)。上述步驟中已將腳本保存至該目錄,無需額外移動。
Cobbler通過Kickstart文件實現無人值守安裝,自定義腳本需通過$SNIPPET
指令在Kickstart模板中引用。編輯對應的Kickstart模板(如/var/lib/cobbler/kickstarts/ubuntu_custom.ks
),在%post
(安裝后執行)或%pre
(安裝前執行) section中添加:
%post --nochroot
# 調用自定義腳本(--nochroot表示在chroot環境外執行,適用于需要訪問安裝介質的場景)
$SNIPPET('custom_init_script.sh')
%end
若腳本需要在chroot環境內執行(如修改系統文件),可直接省略--nochroot
:
%post
$SNIPPET('custom_init_script.sh')
%end
將自定義的Kickstart模板與Cobbler的Profile綁定,使模板在部署時生效。使用以下命令編輯Profile(如ubuntu-2204
):
sudo cobbler profile edit --name=ubuntu-2204 --kickstart=/var/lib/cobbler/kickstarts/ubuntu_custom.ks
此命令將ubuntu_custom.ks
設置為ubuntu-2204
Profile的Kickstart文件。
修改配置后,需同步Cobbler以應用更改:
sudo cobbler sync
同步完成后,通過PXE啟動客戶端或使用cobbler system add
命令創建系統條目,驗證自定義腳本是否執行(如檢查客戶端是否安裝Nginx、/var/log/custom_init.log
是否有日志記錄)。
chmod +x
),否則無法運行。/var/lib/cobbler/snippets/
目錄下的腳本,需將腳本放置于此。$SNIPPET
指令需放在%post
或%pre
section中,且腳本名稱需與Snippet文件名一致(不含.sh
后綴)。/var/log/cobbler/cobbler.log
)或系統日志(/var/log/syslog
)排查問題。通過以上步驟,即可在Ubuntu環境下使用Cobbler自定義腳本,實現自動化部署中的個性化配置。