Skip to content

Instantly share code, notes, and snippets.

@alias454
Last active February 3, 2018 01:40
Show Gist options
  • Select an option

  • Save alias454/dfc4e571a3dc99e2ecec290763246246 to your computer and use it in GitHub Desktop.

Select an option

Save alias454/dfc4e571a3dc99e2ecec290763246246 to your computer and use it in GitHub Desktop.
Generate Groups of VMs from VMware shell
#
# Generate Groups of VMs from VMware shell
#
#!/bin/ash
usage="\n$(basename "$0") [-h] [-n int] [-b basename] [-t tpl_name] [-p /path/to/datastore] \n
Example usage: $(basename "$0") -n 6 -b node -t CENTOS_TPL -p /vmfs/volumes/Datastore
"
while getopts ":hn:b:t:p:" opt; do
case "$opt" in
n) NUM=$OPTARG
;;
b) BASENAME=$OPTARG
;;
t) TPL=$OPTARG
;;
p) TPL_PATH=$OPTARG
;;
h) echo -e "$usage"
exit
;;
:) printf "\nmissing argument for -%s\n" "$OPTARG" >&2
echo -e "$usage" >&2
exit 1
;;
\?) printf "\nillegal option: -%s\n" "$OPTARG" >&2
echo -e "$usage" >&2
exit 1
;;
esac
done
#set path if you want
#TPL_PATH=/vmfs/volumes/Datastore
if [ -z "${NUM//[0-9]/}" -a ! -z "$NUM" ]; then
if [ -d "$TPL_PATH/$TPL" ]; then
cd $TPL_PATH
echo -e "\nCreating $NUM VM(s) from template: $TPL with basename: $BASENAME\n"
for n in $(seq 1 $NUM); do
NEW=$(printf "%s%02d" "$BASENAME" "$((n-1))")
cp -a $TPL $NEW
vmkfstools -E $NEW/$TPL.vmdk $NEW/$NEW.vmdk
for i in $NEW/$TPL.*; do
ext=$(echo $i |cut -d . -f2)
mv $i $NEW/$NEW.$ext
done
sed -i s/$TPL/$NEW/g $NEW/$NEW.vmx
vim-cmd solo/registervm $TPL_PATH/$NEW/$NEW.vmx
done
else
printf "\nCannot locate template at - %s\nTry running vim-cmd vmsvc/getallvms\n" "$TPL_PATH/$TPL" >&2
echo -e "$usage" >&2
exit 1
fi
else
printf "\nSupplied option not a number - %s\n" "$NUM" >&2
echo -e "$usage" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment