VMware {code} Community
bauzo
Contributor
Contributor

Adding a ESX Host to VC

Hi - I need a script to create a new CLuster in VC and one to add ESX Hosts to such cluster.

Does anyne know if such a script(s) already exists? If not I am not a perl programmer but willing to give it a try, could anyone point me in the right direction?

Many thanks.

0 Kudos
2 Replies
ssurana
VMware Employee
VMware Employee

Hi Bauzo,

Below is the code snippet to create a new cluster and then add a host to it. I have hard-coded all the values (Datacenter name, Host IP, etc.) but you can change these to suit your needs. I added few comments in the script so that it becomes self explanatory in terms of what it does. I have only used the minimal set of options wherever required, for additional option refer to the SDK and you should be able to add complexity to it.

sub create_cluster {

.# search for the datacenter where you want to create the cluster. 'DC1' in my example

my $datacenter_views =

Vim::find_entity_views (view_type => 'Datacenter',

filter => { name => 'DC1'});

unless (@$datacenter_views)

if ($#{$datacenter_views} != 0)

my $datacenter = shift @$datacenter_views;

my $host_folder_view = Vim::get_view(mo_ref => $datacenter->hostFolder);

.# create a ClusterConfig Spec. This will be with the default cluster settings. For additional setting you would need to add different parameters in the line below. refer to the SDK for the different options.

my $ccspec = ClusterConfigSpec->new();

my $new_cr;

eval {

.# actually create a a cluster named 'MyCluster'

$new_cr = $host_folder_view->CreateCluster(name => 'MyCluster',

spec => $ccspec);

Util::trace(0, "\nSuccessfully created Cluster : 'MyCluster'\n");

};

if ($@)

if (defined $new_cr) {

eval {

my $cr_ref = Vim::get_view(mo_ref => $new_cr);

.# create a HostConnectSpec Spec. Below I have only specified the mandatory options. for other possible options refer to the SDK

my $hcspec = HostConnectSpec->new(force => 1,

hostName => '10.100.0.1',

password => 'mypassword',

userName => 'root'

);

.# actually add the host '10.100.0.1' to the newly created cluster. Again there are more options that you can try these are the minimal set

$cr_ref->AddHost_Task(spec => $hcspec,

asConnected => 0

);

};

if ($@)

}

}

Hope the code helps. Do reply back with your queries/comments.

~ Sidharth

Scott_Hulbert
Contributor
Contributor

Many thanks for the script you have provided!

I am working on something similar. I am attempting to connect an ESX server to Virtual Center with the following script:

#!/usr/bin/perl -w

use strict;

use Getopt::Long;

use VMware::VIRuntime;

my %opts = (service_url => undef,

userid => undef,

password => undef);

GetOptions (\%opts,

"service_url=s",

"userid=s",

"password=s");

if( !defined ($opts && $opts && $opts ) ) { help(); exit (1); } Vim::login(service_url => $opts, user_name => $opts, password => $opts);

my $host_views = Vim::find_entity_views(view_type => 'HostSystem');

foreach (@$host_views) {

if ($_->runtime->connectionState->val eq 'connected') {

print "Host " . $_->name . " is connected.\n";

next;

} elsif ($_->runtime->connectionState->val eq 'notResponding') {

print "Host " . $_->name . " not responding. Disconnecting...\n";

eval { $_->DisconnectHost(); };

if ($@) {

  1. disconnect failed - skip this host

print "Disconnect has failed for host " . $_->name . ".\n";

next;

} else {

print "Disconnected host " . $_->name . ".\n";

}

}

eval { $_->ReconnectHost( cnxSpec => new HostConnectSpec(force => 1) ); };

if ($@) {

  1. connect failed - report and continue

print "Connect failed for host " . $_->name . ".\n";

} else {

print "Connected host " . $_->name . ".\n";

}

}

Vim::logout();

sub help {

my $help_text = <<'END';

USAGE:

reconnect_hosts.pl --service_url <SDK service URL> --userid <VC user login> --password <VC password>

Example:

reconnect_hosts.pl --service_url --userid administrator --password mypassword

END

print $help_text;

}

This script will connect to the Virtual Center and check for disconnected hosts and then attempt to re-connect them. However the "force" does not seem to work. When I re-connect manually it asks for the ESX "root" user ID and PW but I do not know how to integrate that into this script..

ANY HELP would be GREATLY appreciated!

Scott

0 Kudos