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.
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.
My infrastructure looks like this in my WebClient :
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 :
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 :
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 :
In a perl script, it looks like :
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 :
With this call you will list all the datacenter you have in your inventory. In a perl script :
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”.
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.
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” :
So I’ll ask to “find_entity_views” to search all VMs under my cluster :
Here we are. Here is the full perl script :
Easy isn’t it ?