Use VMware Perl SDK to manage a vSphere infrastructure

Hello,

Here is my first API post on this blog. I used to post some API scripts on OVH forums and I want to use this media to share more of them and show you how easy it is to run scripts on top on VMware infrastructure. I had a chance to present VMware Perl SDK and how it helps sysadmin on VMworld a couple of years ago.

First of all, I’m going to show you how to connect to a vSphere infrastructure and then how to list all your virtual machines. This is a quick introduction but I think it is required to understand how it works.

Thanks for reading and I hope it will be useful for your internal needs.

Overview

I run my scripts on top on a OVH Dedicated Cloud, which is basically a vSphere infrastructure. If you need anything about VMware APIs and SDKs, you can find everything here. One of the best argument on this Perl SDK is that it can run on Linux box. You can also find a python SDK, but it is less powerful than the perl one, there are a lot of call missing.

All my scripts will require one connection to a vCenter, so you need to have a login and a password with the rights you need to perform all the operations you want. For this particular use case, “read only” rights are sufficient.

vSphere infrastructure

My infrastructure looks like this in my WebClient :

Capture d’écran 2016-07-11 à 10.41.21

I have 1 datacenter with 2 clusters and several VMs in my inventory. The goal of our first script is just to list all my VMs on my “Prod” cluster.

Install perl SDK

You need a login on myvmware to be able to download Perl SDK (here is the direct link for vSphere 6.0). To find it you need to go “All VMware product” and on vSphere download page, you have to go on the “Automation Tools and SDKs”. You’ll find a download link for the vSphere SDK for Perl”. Download the tar.gz archive.

Untar it and copy paste the entire directory :

vmware-vsphere-cli-distrib/lib/VMware/share/VMware

in you perl lib directory.

Hint : to known where you perl lib directory is located :

$ perl -e 'print " @INC\n"'

So my perl lib directory is :

/usr/lib/perl5

To install the VMware SDK for perl, I had to copy the content of the tar.gz I downloaded from myvmware website to this directory :

$ cp vmware-vsphere-cli-distrib/lib/VMware/share/VMware /usr/lib/perl5/

The content should look like this :

$ ls /usr/lib/perl5/VMware/
LookupService.pm VICommon.pm VIExt.pm VIM25Runtime.pm VIM2Runtime.pm VIMRuntime.pm pyexe SSOConnection.pm VICredStore.pm VILib.pm VIM25Stub.pm VIM2Stub.pm VIRuntime.pm

Connect to vCenter

First of all, I need credentials to connect to my vCenter, all calls I’ll do by scripts will be done with this user.

Hint : create a specific user to use API calls, you’ll be able later to know where actions and connections come from in your vSphere client Task/Event tab.

To connect on vCenter, it requires 3 lines, a server, a username and a password :

Opts::set_option('server', 'pcc-178-33-102-10.ovh.com');
Opts::set_option('username', 'apiuser');
Opts::set_option('password', 'XXxxXXxxXX');

In my case, my vCenter is my Dedicated Cloud : pcc-178-33-102-10.ovh.com. It could be an IP, your on premise vCenter, whatever. Just a vCenter.

Then, you just have to connect with this :

Util::connect();

In a perl script, it looks like :

#!/usr/bin/perl
use strict;
use Data::Dumper;

use VMware::VIRuntime;

Opts::set_option('server', 'pcc-178-33-102-10.ovh.com');
Opts::set_option('username', 'apiuser');
Opts::set_option('password', 'XXxxXXxxXX');


print "Connecting \n"; 

Util::connect(); 

## Later, we are going to make actions there

Util::disconnect(); 
print "Disconnected \n";

Don’t forget to disconnect at the end 😉

Listing all VMs in my Prod cluster

From there, we can connect to a vCenter, we just need to add on our first script, a few lines to get our VMs.

Getting an object view in a vCenter

First, you need to know how to find an object in a VMware inventory. You will need to use the “find_entity_views” call :

my $DatacenterViews = Vim::find_entity_views(
    'view_type' => 'Datacenter'
);
!$DatacenterViews and die('Failed to get DatacenterViews');

With this call you will list all the datacenter you have in your inventory. In a perl script :

#!/usr/bin/perl
use strict;
use Data::Dumper;

use VMware::VIRuntime;

Opts::set_option('server', 'pcc-178-33-102-10.ovh.com');
Opts::set_option('username', 'apiuser');
Opts::set_option('password', 'XXxxXXxxXX');


print "Connecting \n"; 

Util::connect(); 


my $DatacenterViews = Vim::find_entity_views(
    'view_type' => 'Datacenter'
);
!$DatacenterViews and die('Failed to get DatacenterViews');

print Dumper $DatacenterViews;

Util::disconnect(); 
print "Disconnected \n";

You will dump all Datacenters with all their properties.

To get only one specific object, you can use the “find_entity_view” call with a filter. In my case, I work on the Datacenter name in my Dedicated Cloud, which is “pcc-178-33-102-10_datacenter114”.

my $DatacenterView = Vim::find_entity_view(
    'view_type' => 'Datacenter',
    'filter' => {
        'name' => 'pcc-178-33-102-10_datacenter114'
    } 
);
!$DatacenterView and die('Failed to get DatacenterView');

Getting all VMs in my “Prod” cluster

Now we understand how filters and search work. We can work on finding all VMs in my cluster.

We are going to use the “find_entity_views” call and add a filter based on my cluster name.

First we need to get the view of my “Prod” cluster with the filter we already know.

my $ClusterView  = Vim::find_entity_view(
    'view_type' => 'ClusterComputeResource', 
    'filter'    => { 'name' => 'Prod' } 
);

I introduce a new property of the “find_entity_views” method : begin_entity.

If you remember my inventory, all my VMs are located under my cluster “Prod” :

Capture d’écran 2016-07-11 à 10.41.21

So I’ll ask to “find_entity_views” to search all VMs under my cluster :

my $VMViews  = Vim::find_entity_views(
    'view_type'    => 'VirtualMachine',
    'begin_entity' => $ClusterView ,
);

Here we are. Here is the full perl script :

#!/usr/bin/perl
use strict;
use Data::Dumper;

use VMware::VIRuntime;

Opts::set_option('server', 'pcc-178-33-102-10.ovh.com');
Opts::set_option('username', 'apiuser');
Opts::set_option('password', 'XXxxXXxxXX');


print "Connecting \n"; 

Util::connect();

my $ClusterView  = Vim::find_entity_view(
    'view_type' => 'ClusterComputeResource', 
    'filter'    => { 'name' => 'Prod' } 
);
!$ClusterView and die ("Failed to get Cluster");

my $VMViews  = Vim::find_entity_views(
    'view_type'    => 'VirtualMachine',
    'begin_entity' => $ClusterView,
);
!$VMViews and die ("Failed to get VMs");

foreach my $VM (@$VMViews)
{
    print $VM->name . "\n";
}


Util::disconnect(); 
print "Disconnected \n";

Easy isn’t it ?

Author: fluatovh

I'm today Senior Cloud Architect at OVH since 2010 and I manage the teams in charge of deployment and improvement of the OVH Dedicated Cloud product since its creation. I'm a VMware Architect and a VMware specialist since more than 8 years. We run today thousands of vCenter in production in a completely automated way and we host hundreds of thousands VMs on top on these VMware infrastructures for OVH Customers. @fluatovh

Leave a Reply

Your email address will not be published. Required fields are marked *