I have been spending quite a bit of time lately with PowerCLI Core, especially with one of my pet projects. One of the limitations that PowerCLI Core has today is that the Storage cmdlets which includes vSAN and VVol functionality has not been ported over yet. This means that if you need to do something with VM Storage Policies for example, it would not be possible with PowerCLI Core and you would have to use the Windows PowerCLI version instead.
While investigating for an alternative solution to PowerCLI Core to get access to the Storage Policy Based Management (SPBM) APIs, I was pleasantly surprised to learn that pyvmomi (vSphere SDK for Python) had recently added support for the SPBM APIs in their 6.0.0.2016.4 release last year. I had accidentally stumbled onto this news while looking through the pyvmomi Github issues, specifically this one here. I was surprise to see there was no mention of this enhancement in the pyvmomi release notes.
This is great news for pyvmomi consumers and given this was news to me, I am guessing it might be news for others so figure I would also share the info. While looking into using the SPBM APIs from pyvmomi, I did not see any sample scripts showing how to use the SPBM API. Given I needed to write a script for my project, I figure I would also create a couple of examples to help others get started.
Here are two sample SPBM pyvmomi scripts:
- list_vm_storage_policy.py - List all VM Storage Policies
- update_vm_storage_policy.py - Update an existing VM Storage Policy
Here is an example of running the first script which lists all VM Storage Policies that are defined for a given vCenter Server:
./list_vm_storage_policy.py -s 192.168.1.51 -u '*protected email*' -p 'VMware1!'
Here is an example of running the second script which updates an existing VM Storage Policy called "Silver" with the following configuration changes to the policy FTT=1, Stripe Width=2 and Force Provisioning=False:
./update_vm_storage_policy.py -s 192.168.1.51 -u '*protected email*' -p 'VMware1!' -n Silver -r "{'VSAN.hostFailuresToTolerate':1,'VSAN.stripeWidth':2,'VSAN.forceProvisioning':False}"
You can confirm the change was applied by running the first script again or you could also login to your vSphere Web Client to see the changes as shown in the screenshot below.
For more information on how to get started with SPBM API, be sure to check out the SPBM API Programming Guide as well as the SPBM API Reference Guide.
Scott S. says
Wow, I can't believe you just posted this 4 hours ago. It's exactly what I was looking for! Thanks!
Rahul Garg says
Thanks ... I was looking for this only.
Xing says
Hi William,
Are there any commands that could be used to show the VM storage policy uuid with RVC or any other ways?
coolsunnyvale says
Do u have any samples for pbmcreate() .
shwetank says
I am recently trying to fetch vm storage policies and getting the below error:
Traceback (most recent call last):
File "/Users/mshwetank/Desktop/test/./test_sp.py", line 109, in
main()
File "/Users/mshwetank/Desktop/test/./test_sp.py", line 85, in main
pm = pbmContent.profileManager
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyVmomi/VmomiSupport.py", line 706, in
self.f(*(self.args + (obj,) + args), **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyVmomi/VmomiSupport.py", line 512, in _InvokeMethod
return self._stub.InvokeMethod(self, info, args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pyVmomi/SoapAdapter.py", line 1397, in InvokeMethod
raise obj # pylint: disable-msg=E0702
pyVmomi.VmomiSupport.SecurityError: (vmodl.fault.SecurityError) {
dynamicType = ,
dynamicProperty = (vmodl.DynamicProperty) [],
msg = '',
faultCause = ,
faultMessage = (vmodl.LocalizableMessage) []
}
I am getting the error at the below line in list_vm_storage_policy.py:
profileIds = pm.PbmQueryProfile(resourceType=pbm.profile.ResourceType(
resourceType="STORAGE"), profileCategory="REQUIREMENT"
)
Any help on this would be greatly appreciated.
Nick says
Didn't seem to find any 'disconnect' action in your sample script. Any idea how to do that? Or the pbm endpoint will just get disconnected along with the ordinary vcenter service instance?