By using the API or HTTP requests, you can create a local content library item from a VM in your vCenter Server inventory.

When you run the operation, the API creates a clone of your VM and stores it as a VM template in your local content library. If the operation is successful, the API returns the ID of the newly created library item.

To create a library item that contains a VM template, you can use the com.vmware.vcenter.vm_template_client.LibraryItems class or the POST https://<vCenter_Server_IP>/rest/vcenter/vm-template/library-items HTTP request . You can review the information about a VM template by using the GET https://<vCenter_Server_IP>/rest/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 vAPI session to your vCenter Server.
  • Verify that you created a local library by using the vSphere Client, API, or HTTP request.

Procedure

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

    You can use the com.vmware.vcenter_client.Host.list() method or the GET https://<vCenter_Server_IP>/rest/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 com.vmware.vcenter_client.Datastore.list() method or the GET https://<vCenter_Server_IP>/rest/vcenter/datastore HTTP request.

  3. Get the ID of the VM that you want to clone to a VM template.

    You can use the com.vmware.vcenter_client.VM.list() method or the GET https://<vCenter_Server_IP>/rest/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 com.vmware.content_client.LocalLibrary.list() and com.vmware.content_client.LocalLibrary.get(library_id) methods or the following HTTP requests: GET https://<vCenter_Server_IP>/rest/com/vmware/content/local-library and GET https://<vCenter_Server_IP>/rest/com/vmware/content/library/id:<library_id>.

  5. Create a VM Template library item specification.
    You can use the com.vmware.vcenter.vm_template_client.LibraryItems.CreateSpec class or the body of the POST https://<vCenter_Server_IP>/rest/vcenter/vm-template/library-items HTTP request.
    1. Specify the local library, source VM, and the name of the VM Template library item by using the library, source_vm, and name parameters. You must use the IDs of the local library and source VM.
    2. Specify the place of your VM template in your inventory.
      You can use the LibraryItems.CreatePlacementSpec class or the placement parameters in the body of the request. To specify the ESXi 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.
      You can use the com.vmware.vcenter.vm_template_client.LibraryItems.CreateSpecVmHomeStorage and com.vmware.vcenter.vm_template_client.LibraryItems.CreateSpecDiskStorage classes or the vm_home_storage and disk_storage parameters in the body of the request. You must use the ID of the datastore.
    4. Include the placement and storage specifications in the library item specification.
  6. Create a VM Template library item.
    You can use the com.vmware.vcenter.vm_template_client.LibraryItems.create(spec) method or send the POST https://<vCenter_Server_IP>/rest/vcenter/vm-template/library-items request.

Results

If the operation is successful, the API returns the ID of the VM Template library item. For information about the available responses, see the API Reference documentation.

What to do next

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