Automating ESXi Deployment with UDA 2 and bash script

Recently I’ve been asked to deploy a huge amount of ESXi 4.1 Hosts using the Ultimate Deployment Appliance (UDA 2).

Let’s start off by saying that the UDA is a fantastic tool but for mass deployment without user-intervention you really don’t want to manually create a template for each host and you  so I figured that bash scripts could help me out on this one.

Please note that this article assumes that you already have the UDA installed and configured with 1 basic ESXi4.1 template in order to have the corresponding Linux directories pre-created.

Let’s first look at the pre-required data that the customer needs to deliver for the ESXi Hosts:

  • IP addresses
  • Host names
  • Other network related information like DNS, Subnet and Gateway
  • Mac-addresses

 

Back to the UDA, PXElinux is used to boot Linux from a network server and uses the following host detection methods as I also described in my previous blog about Setting up vSphere ESXi 4.1 Scripted Installation.

Filename for the PXE Configuration File

For the filename of the PXE configuration file, choose one of the following:

– 01-mac_address_of_target_ESXi_host. For example, 01-23-45-67-89-0a-bc

– The target ESXi host IP address in hexadecimal notation.

– default

The initial boot file, pxelinux.0 (or gpxelinux.0) tries to load a PXE configuration file. It tries with the MAC address of the target ESXi host, prefixed with its ARP type code (01 for Ethernet). If that fails, it tries with the hexadecimal notation of target ESXi system IP address. Ultimately, it tries to load a file named default.

Creating the “01-mac_address_of_target_ESXi_host”-file

Step 1 would be to create a “01-mac_address_of_target_ESXi_host”-file for each new Host which contains the following information:

default menu.c32
prompt 0
timeout 100
label Master-ESXi
menu label Master-ESXi Master-ESXi
# no password
KERNEL esx41i.ESXi41.mboot.c32
APPEND esx41i.ESXi41.vmkboot.gz ks=http://<uda-ip-address/kickstart/Master-ESXi/ESXi$FILECOUNTERFULL.cfg — esx41i.ESXi41.vmkernel.gz — esx41i.ESXi41.sys.vgz — esx41i.ESXi41.cim.vgz — esx41i.ESXi41.ienviron.vgz — esx41i.ESXi41.install.vgz

Please note that the “Master-ESXi” label and directory are corresponding to the Template that is defined in the UDA (like you can see on the first screenshot) and that the contents is referring to an unique .cfg file, more on this later.
 
As mentioned before, the customer delivered all the Mac-addresses and it’s important for the script that these Mac-addresses are sorted  and placed on a seperate line because of the logics in the script that states that Mac-address on line 1 is corresponding with the first ESX host. For example, if I want to deploy ESXi001, ESXi002 to ESXi005 than the macinput file should look like this:

00-00-00-00-00-00-01
00-00-00-00-00-00-02
00-00-00-00-00-00-03
00-00-00-00-00-00-04
00-00-00-00-00-00-05

Now let’s take a look at the script that generates all those “01-mac_address_of_target_ESXi_host”-files and reads the macinput file that you need to created based on customer input.

#!/bin/bash
# reads from the $macinput file

echo -n “Enter the file name: “; read macinput;
echo

index=0
FILECOUNTER=1
FILECOUNTERFULL=`printf “%03d” $FILECOUNTER`

while read line ; do
FILENAME=”$line”
echo MAC Address $FILENAME is pointing to ESXi$FILECOUNTERFULL.cfg
cat >> /var/public/tftproot/pxelinux.cfg/01-$FILENAME << FILEEND
default menu.c32
prompt 0
timeout 100
label Master-ESXi
menu label Master-ESXi Master-ESXi
# no password
KERNEL esx41i.ESXi41.mboot.c32
APPEND esx41i.ESXi41.vmkboot.gz ks=http://<uda-ip-address>/kickstart/Master-ESXi/ESXi$FILECOUNTERFULL.cfg — esx41i.ESXi41.vmkernel.gz — esx41i.ESXi41.sys.vgz — esx41i.ESXi41.cim.vgz — esx41i.ESXi41.ienviron.vgz — esx41i.ESXi41.install.vgz

FILEEND

index=$(($index+1))
(( FILECOUNTER++ ))
FILECOUNTERFULL=`printf “%03d” $FILECOUNTER`

done < $macinput

echo “Total MAC Address files created: ${index}”
 
Let’s walk through the script and get the important things described:
  • Script prompts for the macinput file (remember, 1 mac address per line and sorted in order of ESX Host naming convention)
  • FILECOUNTER is set to 1 and FILECOUNTERFULL is converting this to 001. Basically FILECOUNTERFULL is deciding part of Hostname in this script, in my example we are creating ESXi hosts that are named ESXi001, ESXi002 to ESXi005. If you like to create ESXHOST05, ESXHOST06 to ESXHOST10 then you need to change the FILECOUNTER to start from 5, change the  ESXi$FILECOUNTERFULL.cfg to ESXHOST$FILECOUNTERFULL.cfg and change the printf “%03d” to printf “%02d”.
  • For every line (every mac address) it reads it’s creating a “01-mac_address_of_target_ESXi_host”-file which is stored in the /var/public/tftproot/pxelinux.cfg directory (so that it will be picked up by PXELinux during the boot process)

 

Creating the unique Kickstart script

Step 2 would be to create a unique kickstart script that contains all the ESXi Host information like IP address, hostname etc.

Let’s take a look at that script:

 

#!/bin/bash
echo “Creating Kickstart Scripts for ESXi001 – ESXi005”
IPCOUNTER=10
IPLIMIT=15
FILECOUNTER=1
FILECOUNTERFULL=`printf “%03d” $FILECOUNTER`

while [  “$IPCOUNTER” -lt “$IPLIMIT” ]; do
cat >> /var/public/www/kickstart/Master-ESXi/ESXi$FILECOUNTERFULL.cfg << FILEEND

accepteula
rootpw secret
autopart –firstdisk –overwritevmfs
install url http://<uda-ip-address>/esx41i/ESXi41
network –bootproto=static –ip=192.168.10.$IPCOUNTER –gateway=192.168.1.1 –nameserver=192.168.1.240,192.168.1.241 –netmask=255.255.255.0 –hostname=ESXi$FILECOUNTERFULL –addvmportgroup=0

FILEEND

echo $FILECOUNTERFULL

(( IPCOUNTER++ ))
(( FILECOUNTER++ ))
FILECOUNTERFULL=`printf “%03d” $FILECOUNTER`

done

 

Let's walk through the script and get the important things described:
  • IPCOUNTER  is set to 10 which means that the first kickstart script will contain IP address 192.168.1.10 since we are using this code along the way: –ip=192.168.10.$IPCOUNTER
  • IPLIMIT is set to 15 which means that the last kickstart script will contain IP address 192.168.1.15
  • FILECOUNTER is set to 1 and FILECOUNTERFULL is converting this to 001, this needs to be the same as in the script that is creating “01-mac_address_of_target_ESXi_host”-files and again is referring to the hostnames that will be defined.
  • Note the install url http://<uda-ip-address>/esx41i/ESXi41The first directory “esx41i” is created by the UDA itself based on Operating System “VMWare ESX 4.1i Installable”The second directory “ESXi41” is created based on the Flavor which you can decide for yourself.

 

To conclude I would like to mention that I’m not a Linux scripting guy and there are probably better ways to script this. Nevertheless this worked out for me and I really had a benefit of it, that’s why I’m sharing these scripts with you guys 🙂

Leave a comment

3 Comments

  1. Nice job Kenneth! Thank you for sharing!

    Jas

  2. Robert

     /  December 14, 2010

    Wow nice script, how did you come up with something that awesome 😉

  1. ESXi 4.1 Unattended Installation an Overview « Adventures in a Virtual World

Leave a Reply

%d bloggers like this: