Does any one have an example of a clone script that also changes the VLAN of the virtual nic? I want to clone ten VM's and have each new VM be in a different VLAN.
You can easily update any clone script to include the portgroup assignment change, you can even start out with VMware's version of the vmclone.pl and go from there.
Take a look at this script if you want to update just the portgroup for a given VM:
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".
I have that script and my clone script. My problem with that solution is it requires me to wait until the CloneVM_Task to finish. Is there something I can do in the config spec or the relocate spec that will change the VLAN.
Here is my clone code
#!/usr/bin/perl -w
use strict;
use warnings;
#use Getopt::Long;
use Getopt::Std;
use VMware::VIRuntime;
use VMware::VILib;
my %options=();
getopts("f:n:v:",\%options);
print "-f $options\n" if defined $options;
print "-n $options\n" if defined $options;
print "-v $options\n" if defined $options;
Vim::login(service_url => 'https://dcvc01.democentral.ibm.com/sdk/WebServices', user_name => 'username', password => 'password');
################################################
my $vm_view = Vim::find_entity_view (view_type => 'VirtualMachine', filter =>{ 'name'=> $options{v}});
die "VM not found.\n" unless ($vm_view);
################################################
my $ClusterComputeResource_views = Vim::find_entity_view (view_type => 'ClusterComputeResource', filter =>{ 'name'=> 'DC_BLUE_01'}); #Find all ESX Hosts
my @host_array; #Array to hold ESX Host names
my $HostSystem_views = Vim::find_entity_views (view_type => 'HostSystem',begin_entity => $ClusterComputeResource_views, properties => ); # Array to hold ESX host objects.
my @sorted_HostSystem_views = sort { lc($a->name) cmp lc($b->name) } @$HostSystem_views; # alphabetical sort of ESX host Objects
foreach(@sorted_HostSystem_views) {
if (($_->summary->runtime->connectionState->val eq "connected" ) && ($_->summary->overallStatus->val ne "red") && ($_->summary->runtime->inMaintenanceMode eq "0")) { #If ESX Host is connected to VC and the overall status is not red and the host is not in maint mode.
push @host_array,$_->name; #Push hostname to array.
print $_->name . " - ";
print $_->summary->overallStatus->val . " - ";
print $_->summary->runtime->connectionState->val . " - ";
print $_->summary->runtime->inMaintenanceMode . "\n";
}
}
my $number_of_hosts = scalar(@host_array);
print $number_of_hosts . "\n";
my $host_number = int(rand($number_of_hosts - 1));
print $host_number . "\n";
print $host_array[$host_number] . "\n";
my $host = $host_array[$host_number];
my $host_view = Vim::find_entity_view (view_type => 'HostSystem', filter =>{ 'name'=> $host}); #get the view of a host
die "Host not found.\n" unless ($host_view); #die if you can ot find the host
################################################
my %ds_hash = (); #hash to hold datastores
my $dc = Vim::find_entity_views(view_type => 'Datacenter');#get the datacenter object
my @ds_array = (); #Array to hold all datastores
foreach(@$dc) {#foreach datacenter get all datatstores
if(defined $_->datastore) {
@ds_array = (@ds_array, @{$_->datastore});
}
}
my $datastores = Vim::get_views(mo_ref_array => \@ds_array);#get managed object reference for datastores
@ds_array = ();#clear ds_array
foreach(@$datastores) {#foreach datastore if it matches ESXStore-Blue then make a the name the key of a hash anf the free spaces the keys value.
if($_->summary->accessible) {
if($_->summary->name =~ /ESXStore\-Blue/) {
@ds_array = (@ds_array, $_);
$ds_hash{$_->summary->name} = ($_->summary->freeSpace);
}
}
}
my $mostfreespace = 0;
my $mostfreespacekey;
foreach my $key (sort { $ds_hash{$b} <=> $ds_hash{$a} } (keys(%ds_hash))) {#get the name of the datastore with the most freespace
if ( $ds_hash{$key} > $mostfreespace ) {
$mostfreespace = $ds_hash{$key};
$mostfreespacekey = $key;
}
}
print $mostfreespacekey . "\n";
my $datastore_view;
foreach(@$datastores) {
if($_->summary->accessible) {
if($_->summary->name eq $mostfreespacekey) {
$datastore_view = $_;
}
}
}
################################################
my $relocate_spec = VirtualMachineRelocateSpec->new (datastore => $datastore_view, host => $host_view, pool => $vm_view->resourcePool);
my $folder_view = Vim::find_entity_view (view_type => 'Folder', filter =>{ 'name'=> $options{f}});
die "Folder not found.\n" unless ($folder_view);
my $clone_spec = VirtualMachineCloneSpec->new(powerOn => 0,
config => $config_spec,
template => 0,
location => $relocate_spec);
$vm_view->CloneVM_Task(folder => $folder_view, name => $options, spec => $clone_spec);
################################################
################################################
Yes, take a look at my perl script which shows you how to do that. You need to edit your VirtualDeviceConfigSpec http://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.vm.device.VirtualDevic...
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
If you find this information useful, please award points for "correct" or "helpful".