By using the vSphere Automation REST API, 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 POST https://<vcenter_ip_address_or_fqdn>/api/vcenter/vm-template/library-items HTTP request. You can review the information about a VM template by using the GET https://<vcenter_ip_address_or_fqdn>/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 GET https://<vcenter_ip_address_or_fqdn>/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 GET https://<vcenter_ip_address_or_fqdn>/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 GET https://<vcenter_ip_address_or_fqdn>/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 GET https://<vcenter_ip_address_or_fqdn>/api/com/vmware/content/local-library and GET https://<vcenter_ip_address_or_fqdn>/api/com/vmware/content/library/id:<library_id> HTTP requests.

  5. Create a library item specification for the VM template.
    You can use the body of the POST https://<vcenter_ip_address_or_fqdn>/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 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 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 POST https://<vcenter_ip_address_or_fqdn>/api/vcenter/vm-template/library-items HTTP 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 https://<vcenter_ip_address_or_fqdn>/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>.