1 Reply Latest reply on Mar 26, 2020 10:30 PM by anusha25

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

    jwashburn_vs Lurker

      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.

        • 1. Re: Where to start for newbies - Question/Rant/Mostly Whining
          anusha25 Lurker
          VMware Employees


          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