Run VMware vSphere 6 vSphere API with Python script (pyvmomi)

This article is a transcription of "System Management in the Cloud Era" article.

Posted in the previous article " VMware vSphere 6 VMDirectPath I / O devices cannot be added to a VM with PowerCLI " As a part of dealing with the problem that the PCI device cannot be correctly connected to the virtual machine with PowerCLI, we will explain how to use the python library (pyvmomi) that executes the vSphere API.

1. Install pyvmomi

Here, we will describe the environment construction command based on Ubuntu 14.04 for verification with Bash on Ubuntu on Windows 10. The procedure is based on the README on the pyvmomi Github site.

First, install the python-pip package and then install pyvmomi using pip. After installation, check the version of pyvmomi installed with pip just in case.

# apt-get install pip
# pip install pyvmomi
Downloading/unpacking pyvmomi
  Downloading pyvmomi- (218kB): 218kB downloaded
  Running (path:/tmp/pip_build_root/pyvmomi/ egg_info for package pyvmomi

Downloading/unpacking requests>=2.3.0 (from pyvmomi)
  Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded
Downloading/unpacking six>=1.7.3 (from pyvmomi)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: pyvmomi, requests, six
  Running install for pyvmomi

  Found existing installation: requests 2.2.1
    Not uninstalling requests at /usr/lib/python2.7/dist-packages, owned by OS
  Found existing installation: six 1.5.2
    Not uninstalling six at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed pyvmomi requests six
Cleaning up...
# pip list | grep pyvmomi
pyvmomi (

2. vSphere API operation using pyvmomi

pyvmomi is a library for executing the vSphere API with Python Script. Therefore, a Python Script is required to execute it. Here, we will execute up to the point where we use the sample script on Github.

There is " pyvmomi-community-samples " in the Github repository provided by VMware. Download this and try running it.

# git clone
Cloning into 'pyvmomi-community-samples'...
remote: Counting objects: 864, done.
remote: Total 864 (delta 0), reused 0 (delta 0), pack-reused 863
Receiving objects: 100% (864/864), 474.49 KiB | 395.00 KiB/s, done.
Resolving deltas: 100% (492/492), done.
Checking connectivity... done.
# cd pyvmomi-community-samples/samples
# ls                                                                                                tests                                      tools                                                                                                                                                                            

After downloading, try running </ code>. To check the arguments, first execute with -h </ code> and then pass the required arguments. Here, it is executed for the host where only 1VM exists.

# python -h
usage: [-h] -s HOST [-o PORT] -u USER [-p PASSWORD]

Standard Arguments for talking to vCenter

optional arguments:
  -h, --help            show this help message and exit
  -s HOST, --host HOST  vSphere service to connect to
  -o PORT, --port PORT  Port to connect on
  -u USER, --user USER  User name to use when connecting to host
  -p PASSWORD, --password PASSWORD
                        Password to use when connecting to host
# python -s (ESXi host name) -u (Login user name) -p (Login password)
('Name       : ', 'PerftestVM_20160108')
('Template   : ', False)
('Path       : ', '[esxi01_localDS] PerftestVM_20160108/PerftestVM_20160108.vmx')
('Guest      : ', 'CentOS 4/5/6/7 (64-bit)')
('Instance UUID : ', '521a4619-ad48-447d-b692-4e0b1af8d9ca')
('Bios UUID     : ', '564d26c0-4645-b99c-9a3f-eab82be1f824')
('State      : ', 'poweredOff')
('VMware-tools: ', 'toolsNotRunning')
IP         : None

I was able to get the information of the virtual machine on the ESXi host safely.

This completes the installation of pyvmomi and the operation check using the sample script. Next time, I will use this pyvmomi and the PCI device connection script posted on this GIST . , Challenge to set VM DirectPath I / O from script.

Recommended Posts