VMware Cloud Community
mwaack
Contributor
Contributor

Reading/modifying VM tags using the SDK?

I'm trying to read/edit tags associated with a virtual machine by using the vsphere sdk (with pyvmomi cause I prefer python, our VSphere has version 5.1). The vm object has a field "tag" which should contain the tag - but in fact it contains the custom attribute, not the tag.

A simple code excerpt:

from pyVim.connect import SmartConnect, Disconnect

def tags(vm):

  if hasattr(vm, 'childEntity'):

      vmList = vm.childEntity

      for c in vmList:

         tags(c)

  else:

      print vm.name

      for k in vm.tag:

        print k

def main():

      si = SmartConnect(host="<host>", user="<user>", pwd="<password>", port=<port>)

      content = si.RetrieveContent()

      datacenter = content.rootFolder.childEntity[0]

      vmFolder = datacenter.vmFolder

      vmList = vmFolder.childEntity

      for vm in vmList:

         tags(vm)

if __name__ == "__main__":

  main()

This code prints the custom attributes of the VMs (which is a bit surprising because of the naming of fields like vm.tag), but I'm looking for the tags and categories. Where can I find this information?

Mathias

Reply
0 Kudos
6 Replies
gm0meyl0ve
Contributor
Contributor

Did you ever find a resolution for this?

I'm looking for the same.

Thanks

Reply
0 Kudos
vThinkBeyondVM
VMware Employee
VMware Employee

is this not working :vSphere 5.5 Documentation Center

vSphere 5.5 Documentation Center

As per API reference, tag property is experimental.


----------------------------------------------------------------
Thanks & Regards
Vikas, VCP70, MCTS on AD, SCJP6.0, VCF, vSphere with Tanzu specialist.
https://vThinkBeyondVM.com/about
-----------------------------------------------------------------
Disclaimer: Any views or opinions expressed here are strictly my own. I am solely responsible for all content published here. Content published here is not read, reviewed or approved in advance by VMware and does not necessarily represent or reflect the views or opinions of VMware.

Reply
0 Kudos
Netwrix
Enthusiast
Enthusiast

Well, it should be possible with PowerCLI if you upgrade to 5.5, there are cmdlets Set-Tag, Get-Tag

And here is nice article about using them: Using vSphere tags powercli

Reply
0 Kudos
CiscoMiner
Contributor
Contributor

Any update on this?

The SDK / pyvmomi are now at version 6.0.  The Release Notes for SDK 6.0 mention new APIs for Tag management.  I can't find any information about them though.

The use case I'm trying to address is for VMs to be classified via custom Attributes and/or Tags.  I'm using a PropertyCollector / WaitForUpdatesEx(), and for common property types (e.g. 'summary.config.name') this works fine.  Need to be able to do the same sort of thing with custom attributes and/or Tags, but so far have been unable to find any means of doing this.

Reply
0 Kudos
kebr
Contributor
Contributor

Ya this is pretty frustrating, I'm using yavijava 6 and have the same issue.

Reply
0 Kudos
harishdinne
Contributor
Contributor

Did anyone found a soultion yet?

i am having the same issue.

Reply
0 Kudos