【问题标题】:Need python interface for moving a machine to another folder需要python接口将机器移动到另一个文件夹
【发布时间】:2017-01-31 11:19:37
【问题描述】:

我试图在 python 中找到代码支持,以便在 Datacenter 的文件夹之间移动机器但没有成功,我在 pysphere 中看到您可以在克隆阶段定义文件夹,而不是在机器已经克隆之后。

This 似乎是我的问题的解决方案,但它在 powershell 中,有人知道在 python 中对它的包装支持吗

【问题讨论】:

    标签: python-2.7 virtual-machine vsphere pyvmomi pysphere


    【解决方案1】:

    你可以用 pyVmomi 做到这一点。我会避免使用 pysphere,因为 pyVmomi 由 VMWare 维护,并且 pysphere 已经 4 年或更长时间没有更新了。

    这里说的是一些使用 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_folderFolder 实例,我想将我的 VirtualMachine 移动到该实例中。接下来我找到我要移动的VirtualMachine。最后我执行Task,它将为我移动虚拟机。该任务需要将要移动到文件夹中的对象列表的参数,在这种情况下,它是一个单项列表,其中仅包含我要移动的一个 vm。如果需要,您可以从这里监控任务。

    请记住,如果您使用FindByInventoryPath,则有许多隐藏文件夹在 GUI 中是不可见的。我发现有时使用ManagedObjectBrowser 很有帮助。

    有用的文档链接:

    【讨论】:

    • 这非常有用,谢谢!只是为了澄清其他人发现这一点,以上示例中的 LivingRoom 是您的数据中心的名称。
    • @Rekovni 是的。看来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    相关资源
    最近更新 更多