Using the vSphere SDK
Objective
Actions within your infrastructure can be automated using the vSphere SDK.
This guide explains the implementation and usage in various programming languages.
Instructions
Python
Setting up the environment
OS
This example uses a VM installed with Debian 9, deployed from templates available for your Hosted Private Cloud infrastructure.
Requirements
It is necessary to install these packages:
apt-get install python git python-pip
vSphere SDK
Download the vSphere SDK with the following command:
git clone https://github.com/vmware/vsphere-automation-sdk-python.git
The directory "/vsphere-automation-sdk-python" will be created. Switch to this folder to perform the installation command:
pip install --upgrade --force-reinstall -r requirements.txt --extra-index-url file:///<absolute_path_to_sdk>/lib
In this example, it is the following command:
pip install --upgrade --force-reinstall -r requirements.txt --extra-index-url file:///root/vsphere-automation-sdk-python/lib
Once the SDK is installed, you can make use of scripts.
Script example
Connection
This example tests the connection and disconnection to vCenter. It will also help to verify if everything is properly installed.
Listing the VMs of a Private Cloud infrastructure
This example lists all VMs of a Private Cloud infrastructure.
Perl
Setting up the environment
OS
This example uses a VM installed with Ubuntu 18.04, deployed from templates available for your Hosted Private Cloud infrastructure.
Requirements
It is necessary to install these packages:
apt-get install lib32z1 lib32ncurses5 uuid uuid-dev libssl-dev perl-doc libxml-libxml-perl libcrypt-ssleay-perl libsoap-lite-perl libdata-compare-perl libmodule-build-perl libuuid-perl libsocket6-perl libnet-inet6glue-perl libarchive-zip-perl
cpan install Crypt::OpenSSL::RSA UUID::Random Exception::Class Crypt::X509 List::MoreUtils
vSphere SDK
Download the vSphere SDK using this link:
https://my.vmware.com/group/vmware/get-download?downloadGroup=VS-PERL-SDK67
Make sure to download the version that is compatible with your operating system.
In this example the file downloaded is: "VMware-vSphere-Perl-SDK-6.7.0-8156551.x86_64.tar.gz"
Extract the file you just downloaded using this command:
tar –zxvf VMware-vSphere-Perl-SDK-6.7.0-8156551.x86_64.tar.gz
Start the installer using the following commands:
cd vmware-vsphere-cli-distrib
./vmware-install.pl
After reading the terms, accept them and continue by pressing Enter.
After the installation, additional modules will be installed. Hit Enter to continue the installation.
In order to complete the installation, you will need to select a directory in which the SDK will install. By default, the directory is "/usr/bin".
Script example
Connection
This example tests the connection and disconnection to vCenter. It will also help to verify if everything is properly installed.
Listing the VMs of a Private Cloud infrastructure
This example lists all VMs of a Private Cloud infrastructure.
Using samples
In this example, a script that is already created and present in the directory "vmware-vsphere-cli-distrib/apps/vm/" is called.
Here is a list of the scripts already available in this directory:
ls vmware-vsphere-cli-distrib/apps/vm/
guestinfo.pl sharesmanager.pl snapshotmanager.pl vdiskcreate.pl vmclone.pl vmcontrol.pl vmcreate.pl vminfo.pl vmmigrate.pl vmreconfig.pl vmregister.pl vmsnapshot.pl vmtemplate.pl
To create a snapshot "test" of the VM "Debian1", use this command (replacing the example placeholders with your credentials):
perl snapshotmanager.pl --server pcc-149-202-xxx-xxx.ovh.com --username damien --password MyPassword --operation create --vmname Debian1 --snapshotname test
Go further
Join our community of users on https://community.ovh.com/en/.