VMware {code} Community
jwashburn_vs
Contributor
Contributor

Where to start for newbies - Question/Rant/Mostly Whining

Coming from a Operations background.  Windows/UI driven workflow.  I am trying to broaden my horizons and learn Python and use it for VMWare.  The only "code" I have written before are hobbled together Powershell or PowerCLI scripts.

Now with that out of the way, this is where I am at in my journey.  I have gone through Automate the Boring stuff and I am 90% of the way through Learn Python 3 the Hard Way.  None of the stuff I have been learning in those courses seem to relate to anything that I need (apart from understanding functions and classes) in a vmware environment.  At least not yet anyway.

So where I am struggling is making the leap from the example provided with the SDK (https://github.com/vmware/vsphere-automation-sdk-python)  which basically authenticates to vCenter and lists out what VMs you have, to the next step.    Understanding the docs is pretty difficult at this point. 

I want to start with something basic, like list the hosts in the cluster or get some cluster health stats.  Any ideas on where to look next?  So much of what I am reading and finding online comes from authors that are so familiar with what they are doing, they skip much of the basics because they presume everyone else has that knowledge.

Reply
0 Kudos
7 Replies
anusha25
VMware Employee
VMware Employee

jwashburn_vs​,

I totally understand! I was in a similar situation when I started a year ago, even now I cannot say I know it completely, but here are few things that might help you with Python in general and/or vSphere Automation SDK Python

  1. pip and PyPI: You might have come across commands like pip install xyz_package, pip being the package manager for Python, can install the package requested by you. The package can be a local file, a github repo or hosted online. If you don't provide a location, by default the packages will be searched here​. This is a good tutorial for pip
  2. virtual environment: There are many versions of a package available. Between versions, there might be breaking changes. Suppose you have written a piece of code that is compatible with xyz 1.0. After a while, you write another program with xyz 2.0. Now there is a possibility that your first program might not work as expected because of some breaking change in the dependent package. How will you ensure that a certain program/project adheres to a specific package version? Obviously, you cannot update package globally, that is, one version fits all doesn't work. To cater to this need, there is something called as a virtual environment. One environment per project is ideal. This way you will not have conflicting versions across projects.
  3. PYTHONPATH: Along with the python executables, you might have to add certain directories specific to your project to this path. This will allow python to look for modules/packages in that path

Coming to the vSphere Automation SDK, if the README seems daunting, follow these steps for installation: (If you are on windows, kindly look for windows equivalent commands)

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

$ python3 -m venv venv

$ source venv/bin/activate

$ cd vsphere-automation-sdk-python

$ pip install --upgrade pip setuptools

$ pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git

$ export PYTHONPATH=${PWD}:$PYTHONPATH

The python libraries are installed at venv/lib/python3.7/site-packages/com/vmware and venv/lib/python3.7/site-packages/vmware/vapi

Refer to the official documentation and then look that the corresponding code in the above mentioned library path.

Or you can start with a sample like list_vms​, look at the clients and packages it is using from the library and then look at the documentation. Do this exercise for a few samples, you will get the hang of it.

Well, that's not all!

vSphere Automation SDK is based on the REST APIs which is available for VC 6.5+ versions. pyvmomi is also a Python SDK that lets you manage ESXi and VCs. The Automation SDK is not as exhaustive as pyvmomi for the earlier features, so for operations like for hosts or clusters, you might have to use a combination of these. For example, clusters_sample and hosts_sample

This definitely does not cover all aspects that you might need for your development, but can be a starting point. Once you are comfortable with the terminologies and debugging the code, you will be in a better position to grasp the solutions posted on the internet.

Hope this helps Smiley Happy

Reply
0 Kudos
Wigmundo
Contributor
Contributor

I hope you don't mind, but I'm going to tag on the end of your reply because it was very helpful!

I've downloaded the vsphere-automation-sdk-python and copied it to my dev environment (extracted the contents to my python installation folder) and am now trying to get the basics up and running. Unfortunately I am very limited in my environment (its cut off from the real world and very difficult to get updates in/out (automatic pip installs etc.), so I'm trying to work with what I've got 😞 )

when trying the sample code (just listing VMs) I'm struggling to work out where it can find the 'vmware' namespace/folder on the first line!

from vmware.vapi.vsphere.client import create_vsphere_client

using vscode (with python) an 'import <vsphere-automation-sdk-python folder>.' gives the following:

appliances
backuprestore
common
compute_policy
contentlibrary
deferhistoryimport
lib
logforwarding
oauth
sample_template
samples
services
setup
sso
tagging
tests
vcenter

but no 'vmware', where can I find this? Am I close, or is there more I need to install/configure? Total newb as you can probably tell...

Reply
0 Kudos
anusha25
VMware Employee
VMware Employee

So you're saying you cannot do a pip install?

And you didn't do this pip install --upgrade git+https://github.com/vmware/vsphere-automation-sdk-python.git ?

In that case, you have not installed the SDK at all!

And by cut off from the real world, you mean your machine does not have internet access?

Reply
0 Kudos
Wigmundo
Contributor
Contributor

yes, not done pip install... thought it wasn't gona be that easy. 

i have pip, but no internet access and its difficult getting packages on and off the machine 😞 Are there more dependancies required? Is there an offline way? (vmware 6.7 btw) Thanks for any pointers.

Reply
0 Kudos
anusha25
VMware Employee
VMware Employee

Wigmundo

I had tried this sometime ago, but it involves lots of manual steps.

The vSphere Automation SDK has package dependencies - check here

So the packages you will need are

  • lxml >= 4.3.0
  • pyVmomi >= 6.7
  • suds-jurko (assuming your python version is >= 3.0)

You can download all of these on a machine that has internet access and then scp it to your machine. Say you are saving these under dependent_packages/ directory.

Then from your sdk directory, execute this command -

$pip install git+file://$PWD/ --find-links /path/to/dependent_packages/

P.S I think pip version has to be 20.x

Reply
0 Kudos
Wigmundo
Contributor
Contributor

Thank you thats great, I will try and get these three packages on the machine and give it a go.

Another quick question, I was going to try and only use the REST api (i.e. not SOAP) are all these dependencies still required?

Reply
0 Kudos
anusha25
VMware Employee
VMware Employee

[not sure] Then probably you won't require pyVmomi, if you plan on excluding this, remove it from setup.py too. But I wouldn't recommend removing it.

Reply
0 Kudos