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
(abridgement)
# pip install pyvmomi
Downloading/unpacking pyvmomi
  Downloading pyvmomi-6.0.0.2016.6.tar.gz (218kB): 218kB downloaded
  Running setup.py (path:/tmp/pip_build_root/pyvmomi/setup.py) 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 setup.py 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 (6.0.0.2016.6)

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 https://github.com/vmware/pyvmomi-community-samples.git
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
add_disk_to_vm.py               generate_html5_console.py                  sessions_list.py
add_vm_extra_config_tags.py     getallvms.py                               set_note.py
cdrom_vm.py                     getorphanedvms.py                          set_vcenter_motd.py
change_disk_mode.py             getvnicinfo.py                             soft_reboot.py
change_vm_cd_backend.py         hello_world_vcenter.py                     suds-to-pyvmomi.py
change_vm_nic_state.py          hello_world_vcenter_with_yaml_recorder.py  tests
change_vm_vif.py                __init__.py                                tools
clone_vm.py                     list_datastore_cluster.py                  upload_file_to_datastore.py
create_folder_in_datacenter.py  list_datastore_info.py                     upload_file_to_vm.py
create_random_marvel_vms.py     list_dc_datastore_info.py                  vcenter_details.py
create_snapshot.py              list_host_alarms.py                        virtual_machine_device_info.py
delete_disk_from_vm.py          list_vmwaretools_status.py                 virtual_machine_power_cycle_and_question.py
deploy_ovf.py                   make_dc_and_cluster.py                     vminfo_quick.py
destroy_vm.py                   pyvmomi-to-suds.py                         vm_perf_example.py
esxi_perf_sample.py             README.md                                  vSphereAutoRestartManager.py
execute_program_in_vm.py        reboot_vm.py                               waitforupdates.py
export_vm.py                    reconfigure_host_for_ha.py
find_by_uuid.py                 renamer.py

After downloading, try running getallvms.py </ 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 getallvms.py -h
usage: getallvms.py [-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 getallvms.py -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