问题描述
我试图在 python
中找到一个代码支持,用于在 Datacenter 的文件夹之间移动机器但没有成功,我在 pysphere
中看到你可以在克隆阶段,而不是在机器已经克隆之后.
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.
这我的问题的解决方案,但它在 powershell 中,有人知道在 python 中对它的包装支持吗
This seems as a solution for my problem but it is in powershell, do anybody know a wrapping support for it in python
推荐答案
你可以用 pyVmomi 做到这一点.我会避免使用 pysphere,因为 pyVmomi 是由 VMWare 维护的,而 pysphere 已经 4 年或更长时间没有更新了.
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.
也就是说这里有一些使用 pyVmomi 的示例代码
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])
在此示例中,我通过连接到 vCenter 创建了一个 ServiceInstance
,接下来我获取了 SearchIndex
的一个实例.SearchIndex
有多种方法可用于定位您的托管对象.在本例中,我决定使用 FindByInventoryPath
方法,但您可以使用任何适合您的方法.首先,我找到名为 new_folder
的 Folder
实例,我想将我的 VirtualMachine
移动到该实例中.接下来我找到我想要移动的 VirtualMachine
.最后,我执行 Task
将为我移动虚拟机.该任务采用要移动到文件夹中的对象列表的参数,在这种情况下,它是一个仅包含我要移动的虚拟机的单个项目列表.如果需要,您可以从这里监控任务.
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.
请记住,如果您使用 FindByInventoryPath
,则有许多隐藏文件夹在 GUI 中是不可见的.我发现使用 ManagedObjectBrowser 有时非常有用.
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.
有用的文档链接:
- 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
这篇关于需要 python 接口将机器移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!