Skip to content

Instantly share code, notes, and snippets.

@singlecheeze
Last active November 2, 2022 21:29
Show Gist options
  • Save singlecheeze/a36312c590eb1ca33f131203290248f6 to your computer and use it in GitHub Desktop.
Save singlecheeze/a36312c590eb1ca33f131203290248f6 to your computer and use it in GitHub Desktop.
vsphere-automation-sdk-python on RHEL 7

Here is the order of operations I followed (All as root):

This is the python 3.6 document to reference (Changing the applicable commands for rh-python38 instead of 36):
https://developers.redhat.com/blog/2018/08/13/install-python3-rhel#installation_prerequisites

Optional (For GCC, make, and git): $ yum install @development

This installs python 3.8 into /opt/rh/:

$ yum clean all
$ subscription-manager refresh
$ subscription-manager repos --list
$ subscription-manager repos --enable rhel-7-server-optional-rpms --enable rhel-server-rhscl-7-rpms
$ yum info rh-python38
$ yum install rh-python38
$ which python # before scl enable
/usr/bin/python
 
$ scl enable rh-python38 bash

$ which python
/opt/rh/rh-python38/root/usr/bin/python

To alias (Only for your user session, NOT for the whole system) python/python3/python3.8/pip/pip3/pip3.8 run:

$ scl enable rh-python38 bash

This runs:

$ cd /opt/rh/rh-python38
$ ll
total 4
-rw-r--r--.  1 root root 472 Jan 30  2020 enable
dr-xr-xr-x. 15 root root 202 Nov  2 16:14 root

$ cat enable 
export PATH=/opt/rh/rh-python38/root/usr/local/bin:/opt/rh/rh-python38/root/usr/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/opt/rh/rh-python38/root/usr/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export MANPATH=/opt/rh/rh-python38/root/usr/share/man:$MANPATH
export PKG_CONFIG_PATH=/opt/rh/rh-python38/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
export XDG_DATA_DIRS="/opt/rh/rh-python38/root/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"

Otherwise, run python 3.8 from:

$ /opt/rh/rh-python38/root/usr/bin/python

vSphere-automation-sdk-python: https://github.com/vmware/vsphere-automation-sdk-python

Then, I grabbed the latest .zip of the vmware SDK: https://github.com/vmware/vsphere-automation-sdk-python/archive/refs/tags/v8.0.0.1.zip

$ wget https://github.com/vmware/vsphere-automation-sdk-python/archive/refs/tags/v8.0.0.1.zip
$ unzip v8.0.0.1.zip
$ cd vsphere-automation-sdk-python-8.0.0.1/
$ pip install -U pip setuptools==62.0.0 wheel
$ pip install -U lib/*/*.whl
$ pip install -U `pwd`

Next, to check that the SDK is working:

$ cd
$ nano vcenter-test.py

Copy/Paste into the file, replacing DNS name/IP of vCenter and creds:

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()

# Disable cert verification for demo purpose.
# This is not recommended in a production environment.
session.verify = False

# Disable the secure connection warning for demo purposes.
# This is not recommended in a production environment.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Connect to a vCenter Server using username and password
vsphere_client = create_vsphere_client(server='<vc_ip>', username='<vc_username>', password='<vc_password>', session=session)

# List all VMs inside the vCenter Server
print(vsphere_client.vcenter.VM.list())

Invoke SDK from python:

$ python3 vcenter-test.py 

Expected Output:

[Summary(vm='vm-1065', name='JetNASMini', power_state=State(string='POWERED_OFF'), cpu_count=8, memory_size_mib=8192), Summary(vm='vm-1072', name='RHEL8_Dummy3', power_state=State(string='POWERED_ON'), cpu_count=2, memory_size_mib=2048)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment