问题
I am trying to find a code support in python
for moving a machine between Datacenter's folders without success, I saw in pysphere
that you can define the folder just in the clone stage and not after machine already cloned.
This seems as a solution for my problem but it is in powershell, do anybody know a wrapping support for it in python
回答1:
You can do this with pyVmomi. I would avoid pysphere because pyVmomi is maintained by VMWare and pysphere hasnt been updated in 4 years or more.
That said here is some sample code that uses pyVmomi
service_instance = connect.SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=int(args.port))
search_index = service_instance.content.searchIndex
folder = search_index.FindByInventoryPath("LivingRoom/vm/new_folder")
vm_to_move = search_index.FindByInventoryPath("LivingRoom/vm/test-vm")
move_task = folder.MoveInto([vm_to_move])
In this example I create a ServiceInstance
by connecting to a vCenter, next I grab an instance of the SearchIndex
. The SearchIndex
has several methods that can be used to locate your managed objects. In this example I decided to use the FindByInventoryPath
method, but you could use any that will work for you. First I find the instance of the Folder
named new_folder
that I want to move my VirtualMachine
into. Next I find the VirtualMachine
I want to move. Finally I execute the Task
that will move the vm for me. That task takes a param of the list of objects to be moved into the folder, and in this case its a single item list containing only the one vm I want to move. From here you can monitor the task if you want.
Keep in mind that if you use the FindByInventoryPath
there are many hidden folders that are not visible from the GUI. I find that using the ManagedObjectBrowser is very helpful at times.
Helpful doc links:
- https://github.com/vmware/pyvmomi/blob/master/docs/vim/SearchIndex.rst
- https://github.com/vmware/pyvmomi/blob/master/docs/vim/Folder.rst
- https://github.com/vmware/pyvmomi/blob/master/docs/vim/Task.rst
来源:https://stackoverflow.com/questions/41956255/need-python-interface-for-moving-a-machine-to-another-folder