How to SDK

Getting Started with the vSphere Automation SDK for Perl

We are going to keep the ‘Getting Started’ series going on using the vSphere Automation SDKs. This time, we will be taking a look at the SDK for Perl.

If you’ve missed any of the prior posts, here are the others in the series:

Let’s take a look at how to easily get started with this SDK on a Windows 10 system. Before diving in though, there are a couple prerequisites we’ll need to download and install.

Prerequisites

The first thing we’ll want to do before getting started with this SDK is to clone the repository to the local system. We can do that with the following command:

git clone https://github.com/vmware/vsphere-automation-sdk-perl.git

Example: Clone vSphere Automation SDK for Perl Repo Locally

We also want to make sure Perl is available on the local system. The supported versions are Active Perl 5.14+ or Stawberry Perl 5.24.1.1+. For these examples, I’ll be using Active Perl which can be downloaded directly from their site: Active Perl

Example: Installing Active Perl

We will then need to make sure a couple Perl packages are installed and available. These can be installed with the following commands:

ppm install XML::LibXML
ppm install UUID::Random
ppm install Crypt::OpenSSL::RSA
ppm install Exception::Class
ppm install Crypt::X509
ppm install Data::Compare

Example: Installing Additional Packages as Prerequisites

Next, this SDK also depends on some capabilities from the vSphere Perl SDK. We will need to login to the My VMware portal, download the executable, then install it from the following link: vSphere Perl SDK

We have one more step concerning the vSphere Perl SDK install. We also need to make sure the ‘python27.dll’ file is available in the vSphere Perl SDK’s folder as follows: ..\Perl\lib\VMware

The following code can be used to easily copy that file to the proper location:

cp 'C:\Program Files (x86)\VMware\VMware vSphere CLI\bin\python27.dll' 'C:\Program Files (x86)\VMware\VMware vSphere CLI\Perl\lib\VMware\'

Example: Copy Python27.dll to Appropriate Place for Perl Usage

Last part of the prerequisites is to configure the environmental variable PERL5LIB to look for modules in the newly cloned SDK directory. The following directories need to be specified:

  • ..\lib\runtime
  • ..\lib\sdk
  • ..\samples

The following code was used to configure the PERL5LIB environmental variable for my lab:

set PERL5LIB=%PERL5LIB%;C:\Users\Kyle Ruddy\Documents\GitHub\vsphere-automation-sdk-perl\lib\runtime;C:\Users\Kyle Ruddy\Documents\GitHub\vsphere-automation-sdk-perl\lib\sdk;C:\Users\Kyle Ruddy\Documents\GitHub\vsphere-automation-sdk-perl\samples

Example: Configuring the PERL5LIB Variable

We are now all setup to start using the vSphere Automation SDK for Perl!

Running Samples

There are a handful of samples that are already included with this SDK. The samples are separated by the API namespaces into different directories and then presented as scripts. We’ll be looking at running some of the samples from the vCenter directory.

The first example we’ll run is the List VMs sample. This sample can be found in the ‘..\samples\Vcenter\Vm’ directory. There are a couple sample scripts here, which also include deleting a VM and performing some power actions.

Example: Displaying Example Perl Scripts

If we run the ‘list_vms.pl’ sample as is, we will receive back the appropriate parameters to successfully complete the call. Those parameters would be username, password, and server name.

Performing the List VMs sample in my environment, with all of the required parameters, looks like the following:

perl list_vms.pl --username [email protected] --password VMware1! --server vcsa01.corp.local

Example: Running the ListVMs Sample Script

There’s another example in the VM folder which allows us to modify the power state for a given VM. However, this sample will take a given VM through all of the available power states. Again, running the sample without any parameters will give us the required parameters. We can see that we’ll need to also add parameters for the VM name and cleanup. The cleanup parameter is used to return the VM to the last state it was in.

Performing the List VMs sample in my environment, with all of the required parameters, looks like the following:

perl power_life_cycle.pl --username [email protected] --password VMware1! --server vcsa01.corp.local --vmname svc02 --cleanup false

Example: Running the VM Power Lifecycle Script

The last example we’ll take a look at is the creation of a new VM. We’ll take advantage of one of this new API’s features and create a new VM using defaults. This means the API fills in any parameters we do not specify with values based on what Guest OS has been specified. It’s also import to note that the Guest OS has been hardcoded in the sample to be ‘Windows_9_64’. This can be found on line 71 of the ‘default_vm.pl’ script. Additional Guest OS types can be found in the API documentation: Guest_OS Types

We’ll start by moving to the following directory: ..\samples\Vcenter\Vm\Create\DefaultVM Running the ‘default_vm.pl’ script will show us a number of parameters we’ll need to specify. These parameters include names for the following: VM, Datacenter, Cluster, VM folder, datastore. There’s also the authentication and cleanup parameters. In this case, giving the cleanup parameter a ‘True’ value will remove the newly created VM.

Performing the Create New Default VM sample in my environment, with all of the required parameters, looks like the following:

perl default_vm.pl --username [email protected] --password VMware1! --server vcsa01.corp.local --vmname demovm01 --datacenter DemoDC --clustername Demo --vmfolder Dev --datastore vsanDatastore --cleanup False

Example: Usage of the CreateDefaultVm Sample

Summary

This post shows you how to easily get started with the vSphere Automation SDK for Perl. It takes you through the setup of your local development environment, as well as running some of the individual samples. You can then take whichever is most relevant to you and apply to your environment or, pull parts of this code out and use it to automate or integrate as needed.

With our SDKs now being open sourced, we are intent on making sure these samples are of a high quality. If you notice areas of improvement, or come up with some new samples, please feel free to raise issues or pull requests on our GitHub repository.