vmware / pyvmomi

VMware vSphere API Python Bindings
Apache License 2.0
2.19k stars 766 forks source link

Is it possible to select compute resources when cloning a machine from a template? #1050

Closed flyzstu closed 9 months ago

flyzstu commented 9 months ago

Is your feature request related to a problem? Please describe.

I learned that the CPU and memory can be set when cloning a machine via a template. I want to know how to set up node. That is, if I want to clone a machine to a specific node, how should I code that? Below is part of my code (mainly from the pyvmomi-community-samples project)

#!/usr/bin/env python
"""
Written by Dann Bohn
Github: https://github.com/whereismyjetpack
Email: dannbohn@gmail.com

Clone a VM from template example
"""
from pyVmomi import vim
from tools import cli, service_instance, pchelper

# from add_nic_to_vm import add_nic

def wait_for_task(task):
    """ wait for a vCenter task to finish """
    task_done = False
    while not task_done:
        if task.info.state == 'success':
            return task.info.result

        if task.info.state == 'error':
            print("there was an error")
            print(task.info.error)
            task_done = True

def clone_vm(
        content, template, vm_name, datacenter_name, vm_folder, datastore_name,
        cluster_name, resource_pool, power_on, datastorecluster_name):
        ...
        cspec = vim.vm.ConfigSpec()
        cspec.numCPUs = 4  # if you want 4 cpus
        cspec.numCoresPerSocket = 2  # if you want dual-processor with dual-cores
        cspec.memoryMB = 1024  # 1GB of memory

        relospec = vim.vm.RelocateSpec()
        relospec.datastore = datastore
        relospec.pool = resource_pool

        clonespec = vim.vm.CloneSpec()
        clonespec.location = relospec
        clonespec.powerOn = power_on
        clonespec.config = cspec

        print("cloning VM...")
        task = template.Clone(folder=destfolder, name=vm_name, spec=clonespec)
        wait_for_task(task)
        print("VM cloned.")

def main():
    """
    Let this thing fly
    """
    parser = cli.Parser()
    parser.add_required_arguments(cli.Argument.VM_NAME, cli.Argument.TEMPLATE)
    # if no locationis provided, thefirst available datacenter, datastore, etc. will be used
    parser.add_optional_arguments(cli.Argument.DATACENTER_NAME, cli.Argument.VMFOLDER,
                                  cli.Argument.DATASTORE_NAME, cli.Argument.DATASTORECLUSTER_NAME,
                                  cli.Argument.CLUSTER_NAME, cli.Argument.RESOURCE_POOL,
                                  cli.Argument.POWER_ON, cli.Argument.OPAQUE_NETWORK_NAME)
    args = parser.get_args()
    si = service_instance.connect(args)

    content = si.RetrieveContent()
    template = pchelper.get_obj(content, [vim.VirtualMachine], args.template)

    if template:
        clone_vm(
            content, template, args.vm_name, args.datacenter_name, args.vm_folder,
            args.datastore_name, args.cluster_name, args.resource_pool, args.power_on,
            args.datastorecluster_name)
    else:
        print("template not found")

# start this thing
if __name__ == "__main__":
    main()

Describe the solution you'd like

If you have a way to select a specific host node when cloning a VM, please let me know, I'd be grateful!

Describe alternatives you've considered

No response

Additional context

No response