Create a VM Template in a Content Library from a Virtual Machine

By using the vSphere Automation API or HTTP requests, you can create a VM template in a content library from an existing virtual machine in your vCenter Server inventory.

When you call the create function of the com.vmware.vcenter.vm_template.LibraryItems service, a VM template is created as a library item in your local content library. If the operation is successful, the LibraryItems service returns the ID of the newly created library item.

To create a library item that contains a VM template, you can use the ctreate function of the LibraryItems interface, or the POST https://<vCenter_Server_IP>/api/vcenter/vm-template/library-items HTTP request. You can review the information about a VM template by using the get function of the LibraryItems interface or the GET https://<vCenter_Server_IP>/api/vcenter/vm-template/library-items/<VM_Template_Item_ID> HTTP request. For information about how to create a VM template by using the vSphere Client, see the vSphere Virtual Machine Administration documentation.

For information about the available and mandatory parameters, see the API Reference documentation.

Prerequisites

  • Verify that you have administrative privileges on your vCenter Server instance.
  • Verify that you created a vSphere Automation session to your vCenter Server.
  • Verify that you created a local library by using the vSphere Client or the vSphere Automation APIs.

Procedure

  1. Get the ID of your ESXi host on which you want to store the VM template.

    You can use the list function of the com.vmware.vcenter.Host interface or the GET https://<vCenter_Server_IP>/api/vcenter/host HTTP request.

  2. Get the ID of the datastore on which you want to store the VM template files.

    You can use the list function of the com.vmware.vcenter_client.Datastore interface or the GET https://<vCenter_Server_IP>/api/vcenter/datastore HTTP request.

  3. Get the ID of the virtual machine that you want to save as a VM template.

    You can use the list method of the com.vmware.vcenter_client.VM interface or the GET https://<vCenter_Server_IP>/api/vcenter/vm HTTP request.

  4. Get the ID of your local library.

    You can get the list of the local libraries in your vCenter Server and review the information about each library by using the list and get(library_id) functions of the com.vmware.content_client.LocalLibrary or the following HTTP requests: GET https://<vCenter_Server_IP>/api/com/vmware/content/local-library and GET https://<vCenter_Server_IP>/api/com/vmware/content/library/id:<library_id>.

  5. Create a library item specification for the VM template.
    You can use the com.vmware.vcenter.vm_template.LibraryItems.CreateSpec class or the body of the POST https://<vCenter_Server_IP>/api/vcenter/vm-template/library-items HTTP request.
    1. Specify the local library, source virtual machine, and the name of the library item by using the library, source_vm, and name parameters. You must use the IDs of the local library and source virtual machine.
    2. Specify the placement information for your VM template.
      You can use the LibraryItems.CreatePlacementSpec class or the placement parameters in the body of the HTTP request. To specify the host, resource pool, cluster, and folder, you must use their IDs.
    3. Specify the datastore on which you want to store the log, configuration, and disk files of your VM template.
      To specify the storage backing for the VM template, you can use the com.vmware.vcenter.vm_template.LibraryItems.CreateSpecVmHomeStorage and com.vmware.vcenter.vm_template.LibraryItems.CreateSpecDiskStorage classes or the vm_home_storage and disk_storage parameters in the body of the HTTP request. You must use the ID of the datastore.
    4. Include the placement and storage specifications in the library item specification.
  6. Create a library item for storing the VM template.
    You can use the create(spec) function of the com.vmware.vcenter.vm_template.LibraryItems interface or send the POST https://<vCenter_Server_IP>/api/vcenter/vm-template/library-items request.

Results

If the operation is successful, the LibraryItems service returns the ID of the library item that contains the VM template. For information about the available responses, see the API Reference documentation.

What to do next

  • Review the information stored in the library item by using the get(VM_template_item_ID) function of the com.vmware.vcenter.vm_template.LibraryItems interface or the GET https://<vCenter_Server_IP>/api/vcenter/vm-template/library-items/<library_item_id> HTTP request. If you did not save the ID of the library item holding the VM template, you can check the UUID by using the vSphere Client. The URN ends with the ID of the library item and has the following format: urn:vapi:com.vmware.content.library.Item:<VMTemplateItemID>.