ESXi kickstart with Python
I recently spent some time building kickstart files. Something I hadn’t done much since 2008. The most noticeable change in the process is I’m older and fatter. I re/learnt a few things, and did my first ever python script.
To see which python modules are available, view /lib/python2.7 on your ESXi host.
Mixing busybox and python.
I found it easier to start off with busybox, then call the python script.
%firstboot –interpreter=busybox
# For troubleshooting only
vim-cmd hostsvc/enable_esx_shell
vim-cmd hostsvc/start_esx_shell
wget -O myscript.py http://webserver/myscript.py
chmod u+x myscript.py
/bin/python myscript.py
%firstboot script is running now. |
%firstboot – Runs at the very end of the first boot after installation. All services should be available. It’s the last thing that runs before the console shows the ESXi host name, IP. Look for “Running 001.firstboot_001”.
Deploy multiple ESXi hosts using kickstart and embedded CSV.
First, customise an ESXi boot CD from William Lam.
Create the CSV file (HOSTS.CSV) in the format Hostname,MAC,IP,Subnet Mask,Gateway.
1HostA,00:0c:29:aa:bb:cc,192.168.0.10,255.255.255.0,192.168.0.1
2HostB,00:0c:29:aa:bb:cd,192.168.0.11,255.255.255.0,192.168.0.1
Create the python script (SETNET.PY) to check the CSV for the matching MAC address:
1#!/usr/bin/python
2import os, commands, csv, subprocess
3
4MAC=subprocess.check_output("esxcli network ip interface list |grep MAC",shell=True)
5
6MACADDR = MAC.split()
7
8with open(‘/vmfs/volumes/datastore1/HOSTS.CSV’, ‘rb’) as f:
9 reader = csv.reader(f)
10 for row in reader:
11 if MACADDR[2] == row[1]:
12 os.system("esxcli system hostname set –fqdn=" + row[0])
13 os.system("esxcli network ip interface ipv4 set –interface-name=vmk0 –ipv4=" + row[2] + " –netmask=" + row[3] + " –type=static")
14print "End of loop"
Include the following in your ks.cfg:
1%post --interpreter=busybox
2
3cp /vmfs/volumes/CDROM/BUILD/HOSTS.CSV /vmfs/volumes/datastore1
4cp /vmfs/volumes/CDROM/BUILD/SETNET.PY /vmfs/volumes/datastore1
5
6%firstboot --interpreter=busybox
7
8sleep 30;
9
10chmod u+x /vmfs/volumes/datastore1/SETNET.PY
11/bin/python /vmfs/volumes/datastore1/SETNET.PY