vmware.vmware_rest.vcenter_vmtemplate_libraryitems module – Creates a library item in content library from a virtual machine

Note

This module is part of the vmware.vmware_rest collection (version 4.4.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install vmware.vmware_rest. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: vmware.vmware_rest.vcenter_vmtemplate_libraryitems.

New in vmware.vmware_rest 2.2.0

Synopsis

  • Creates a library item in content library from a virtual machine. This operation creates a library item in content library whose content is a virtual machine template created from the source virtual machine, using the supplied create specification. The virtual machine template is stored in a newly created library item.

Requirements

The below requirements are needed on the host that executes this module.

  • vSphere 7.0.3 or greater

  • python >= 3.6

  • aiohttp

Parameters

Parameter

Comments

description

string

Description of the deployed virtual machine.

If unset, the deployed virtual machine has the same description as the source library item.

disk_storage

dictionary

Storage specification for the deployed virtual machine’s disks.

If both disk_storage_overrides and disk_storage are unset, the deployed virtual machine’s disks are created with the same storage spec as the corresponding disks in the source virtual machine template contained in the library item.

If disk_storage_overrides is unset and disk_storage is specified, all of the deployed virtual machine’s disks are created with the storage spec specified by disk_storage.

If disk_storage_overrides is specified and disk_storage is unset, disks with identifiers that are not in disk_storage_overrides are created with the same storage spec as the corresponding disks in the source virtual machine template contained in the library item.

If both disk_storage_overrides and disk_storage are specified, disks with identifiers that are not in disk_storage_overrides are created with the storage spec specified by disk_storage.

Valid attributes are:

  • datastore (str): Identifier for the datastore associated the deployed virtual machine’s disk.

This field is currently required.

If storage_policy is also specified and is incompatible with the datastore, then the disk will be flagged as being out of compliance with the specified storage policy.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_datastore_info. ([‘deploy’, ‘present’])

  • storage_policy (dict): Storage policy for the deployed virtual machine’s disk.

If unset, datastore must be specified and the deployed virtual machine’s disk is created with the default storage policy associated with the datastore. ([‘deploy’, ‘present’])

  • Accepted keys:

  • type (string): Policy type for a virtual machine template’s disk.

Accepted value for this field:

  • USE_SPECIFIED_POLICY

  • policy (string): Identifier for the storage policy to use.

This field is optional and it is only relevant when the value of type is USE_SPECIFIED_POLICY.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_storage_policies_info.

disk_storage_overrides

dictionary

Storage specification for individual disks in the deployed virtual machine. This is specified as a mapping between disk identifiers in the source virtual machine template contained in the library item and their storage specifications.

If both disk_storage_overrides and disk_storage are unset, the deployed virtual machine’s disks are created with the same storage spec as the corresponding disks in the source virtual machine template contained in the library item.

If disk_storage_overrides is unset and disk_storage is specified, all of the deployed virtual machine’s disks are created with the storage spec specified by disk_storage.

If disk_storage_overrides is specified and disk_storage is unset, disks with identifiers that are not in disk_storage_overrides are created with the same storage spec as the corresponding disks in the source virtual machine template contained in the library item.

If both disk_storage_overrides and disk_storage are specified, disks with identifiers that are not in disk_storage_overrides are created with the storage spec specified by disk_storage.

When clients pass a value of this structure as a parameter, the key in the field map must be the id of a resource returned by vmware.vmware_rest.vcenter_vm_hardware_disk.

guest_customization

dictionary

Guest customization spec to apply to the deployed virtual machine.

If unset, the guest operating system is not customized after deployment.

Valid attributes are:

  • name (str): Name of the customization specification.

If unset, no guest customization is performed. ([‘deploy’])

hardware_customization

dictionary

Hardware customization spec which specifies updates to the deployed virtual machine.

If unset, the deployed virtual machine has the same hardware configuration as the source virtual machine template contained in the library item.

Valid attributes are:

  • nics (dict): Map of Ethernet network adapters to update.

If unset, all Ethernet adapters will remain connected to the same network as they were in the source virtual machine template. An Ethernet adapter with a MacAddressType of MANUAL will not change. An Ethernet adapter with a MacAddressType of GENERATED or ASSIGNED will receive a new address.

When clients pass a value of this structure as a parameter, the key in the field map must be the id of a resource returned by vmware.vmware_rest.vcenter_vm_hardware_ethernet. ([‘deploy’])

  • disks_to_remove (list): Idenfiers of disks to remove from the deployed virtual machine.

If unset, all disks will be copied.

When clients pass a value of this structure as a parameter, the field must contain the id of resources returned by vmware.vmware_rest.vcenter_vm_hardware_disk. ([‘deploy’])

  • disks_to_update (dict): Disk update specification for individual disks in the deployed virtual machine.

If unset, disks in the deployed virtual machine will have the same settings as the corresponding disks in the source virtual machine template contained in the library item.

When clients pass a value of this structure as a parameter, the key in the field map must be the id of a resource returned by vmware.vmware_rest.vcenter_vm_hardware_disk. ([‘deploy’])

  • cpu_update (dict): CPU update specification for the deployed virtual machine.

If {@term.unset}, the deployed virtual machine has the same CPU settings as the source virtual machine template contained in the library item. ([‘deploy’])

  • Accepted keys:

  • num_cpus (integer): Number of virtual processors in the deployed virtual machine.

If {@term.unset}, the deployed virtual machine has the same CPU count as the source virtual machine template contained in the library item.

  • num_cores_per_socket (integer): Number of cores among which to distribute CPUs in the deployed virtual machine.

If {@term.unset}, the deployed virtual machine has the same number of cores per socket as the source virtual machine template contained in the library item.

  • memory_update (dict): Memory update specification for the deployed virtual machine.

If {@term.unset}, the deployed virtual machine has the same memory settings as the source virtual machine template contained in the library item. ([‘deploy’])

  • Accepted keys:

  • memory (integer): Size of a virtual machine’s memory in MB.

If {@term.unset}, the deployed virtual machine has the same memory size as the source virtual machine template.

library

string

Identifier of the library in which the new library item should be created.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.content_library_info. Required with state=[‘present’]

name

string / required

Name of the deployed virtual machine. This parameter is mandatory.

placement

dictionary

Information used to place the deployed virtual machine.

This field is currently required. In the future, if this field is unset, the system will use the values from the source virtual machine template contained in the library item.

If specified, each field will be used for placement. If the fields result in disjoint placement, the operation will fail. If the fields along with the placement values of the source virtual machine template result in disjoint placement, the operation will fail.

Valid attributes are:

  • folder (str): Virtual machine folder into which the deployed virtual machine should be placed.

This field is currently required.

If unset, the system will attempt to choose a suitable folder for the virtual machine; if a folder cannot be chosen, the virtual machine deployment operation will fail.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_folder_info. ([‘deploy’, ‘present’])

  • resource_pool (str): Resource pool into which the deployed virtual machine should be placed.

If unset, the system will attempt to choose a suitable resource pool for the virtual machine; if a resource pool cannot be chosen, the virtual machine deployment operation will fail.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_resourcepool_info. ([‘deploy’, ‘present’])

  • host (str): Host onto which the virtual machine should be placed. If host and resource_pool are both specified, resource_pool must belong to host. If host and cluster are both specified, host must be a member of cluster.

This field may be unset if resource_pool or cluster is specified. If unset, the system will attempt to choose a suitable host for the virtual machine; if a host cannot be chosen, the virtual machine deployment operation will fail.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_host_info. ([‘deploy’, ‘present’])

  • cluster (str): Cluster onto which the deployed virtual machine should be placed. If cluster and resource_pool are both specified, resource_pool must belong to cluster. If cluster and host are both specified, host must be a member of cluster.

If resource_pool or host is specified, it is recommended that this field be unset.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_cluster_info. ([‘deploy’, ‘present’])

powered_on

boolean

Specifies whether the deployed virtual machine should be powered on after deployment.

If unset, the virtual machine will not be powered on after deployment.

Choices:

  • false

  • true

session_timeout

float

added in vmware.vmware_rest 2.1.0

Timeout settings for client session.

The maximal number of seconds for the whole operation including connection establishment, request sending and response.

The default value is 300s.

source_vm

string

Identifier of the source virtual machine to create the library item from.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_vm_info. Required with state=[‘present’]

state

string

Choices:

  • "deploy"

  • "present" ← (default)

template_library_item

string

identifier of the content library item containing the source virtual machine template to be deployed.

The parameter must be the id of a resource returned by vmware.vmware_rest.content_library_item_info. Required with state=[‘deploy’]

vcenter_hostname

string / required

The hostname or IP address of the vSphere vCenter

If the value is not specified in the task, the value of environment variable VMWARE_HOST will be used instead.

vcenter_password

string / required

The vSphere vCenter password

If the value is not specified in the task, the value of environment variable VMWARE_PASSWORD will be used instead.

vcenter_rest_log_file

string

You can use this optional parameter to set the location of a log file.

This file will be used to record the HTTP REST interaction.

The file will be stored on the host that runs the module.

If the value is not specified in the task, the value of

environment variable VMWARE_REST_LOG_FILE will be used instead.

vcenter_username

string / required

The vSphere vCenter username

If the value is not specified in the task, the value of environment variable VMWARE_USER will be used instead.

vcenter_validate_certs

boolean

Allows connection when SSL certificates are not valid. Set to false when certificates are not trusted.

If the value is not specified in the task, the value of environment variable VMWARE_VALIDATE_CERTS will be used instead.

Choices:

  • false

  • true ← (default)

vm_home_storage

dictionary

Storage location for the virtual machine template’s configuration and log files.

If unset, the virtual machine template’s configuration and log files are placed on the default storage backing associated with the library specified by

Valid attributes are:

  • datastore (str): Identifier of the datastore for the deployed virtual machine’s configuration and log files.

This field is currently required.

When storage_policy is also specified and is incompatible with the datastore, then the deployed virtual machine will be flagged as being out of compliance with the specified storage policy.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_datastore_info. ([‘deploy’, ‘present’])

  • storage_policy (dict): Storage policy for the deployed virtual machine’s configuration and log files.

If unset, datastore must be specified and the deployed virtual machine’s configuration and log files are created with the default storage policy associated with the datastore. ([‘deploy’, ‘present’])

  • Accepted keys:

  • type (string): Policy type for the virtual machine template’s configuration and log files.

Accepted value for this field:

  • USE_SPECIFIED_POLICY

  • policy (string): Identifier for the storage policy to use.

This field is optional and it is only relevant when the value of type is USE_SPECIFIED_POLICY.

When clients pass a value of this structure as a parameter, the field must be the id of a resource returned by vmware.vmware_rest.vcenter_storage_policies_info.

Notes

Note

  • Tested on vSphere 7.0.3

Examples

- name: Create a VM
  vmware.vmware_rest.vcenter_vm:
    placement:
      cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
      datastore: "{{ lookup('vmware.vmware_rest.datastore_moid', '/my_dc/datastore/local') }}"
      folder: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/vm') }}"
      resource_pool: "{{ lookup('vmware.vmware_rest.resource_pool_moid', '/my_dc/host/my_cluster/Resources') }}"
    name: test_vm1
    guest_OS: RHEL_7_64
    hardware_version: VMX_11
    memory:
      hot_add_enabled: true
      size_MiB: 1024
    disks:
    - type: SATA
      backing:
        type: VMDK_FILE
        vmdk_file: '[local] test_vm1/{{ disk_name }}.vmdk'
    - type: SATA
      new_vmdk:
        name: second_disk
        capacity: 32000000000
    cdroms:
    - type: SATA
      sata:
        bus: 0
        unit: 2
    nics:
    - backing:
        type: STANDARD_PORTGROUP
        network: "{{ lookup('vmware.vmware_rest.network_moid', '/my_dc/network/VM Network') }}"
  register: my_vm

- name: Create a content library based on a DataStore
  vmware.vmware_rest.content_locallibrary:
    name: my_library_on_datastore
    description: automated
    publish_info:
      published: true
      authentication_method: NONE
    storage_backings:
    - datastore_id: "{{ lookup('vmware.vmware_rest.datastore_moid', '/my_dc/datastore/local') }}"
      type: DATASTORE
    state: present
  register: nfs_lib

- name: Create a VM template on the library
  vmware.vmware_rest.vcenter_vmtemplate_libraryitems:
    name: golden-template
    library: '{{ nfs_lib.id }}'
    source_vm: '{{ my_vm.id }}'
    placement:
      cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
      folder: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/vm') }}"
      resource_pool: "{{ lookup('vmware.vmware_rest.resource_pool_moid', '/my_dc/host/my_cluster/Resources') }}"
  register: mylib_item

- name: Get the list of items of the NFS library
  vmware.vmware.content_library_item_info:
    library_id: '{{ nfs_lib.id }}'
  register: lib_items

- name: Use the name to identify the item
  set_fact:
    my_template_item: "{{ lib_items.value | selectattr('name', 'equalto', 'golden-template')|first }}"

- name: Deploy a new VM based on the template
  vmware.vmware_rest.vcenter_vmtemplate_libraryitems:
    name: vm-from-template
    library: '{{ nfs_lib.id }}'
    template_library_item: '{{ my_template_item.id }}'
    placement:
      cluster: "{{ lookup('vmware.vmware_rest.cluster_moid', '/my_dc/host/my_cluster') }}"
      folder: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/vm') }}"
      resource_pool: "{{ lookup('vmware.vmware_rest.resource_pool_moid', '/my_dc/host/my_cluster/Resources') }}"
    state: deploy
  register: my_new_vm

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

id

string

moid of the resource

Returned: On success

Sample: "9c6df1f5-faba-490c-a8e6-edb72f787ab8"

value

dictionary

Create a VM template on the library

Returned: On success

Sample: {"cpu": {"cores_per_socket": 1, "count": 1}, "disks": {"16000": {"capacity": 16106127360, "disk_storage": {"datastore": "datastore-1122"}}, "16001": {"capacity": 32000000000, "disk_storage": {"datastore": "datastore-1122"}}}, "guest_OS": "RHEL_7_64", "memory": {"size_MiB": 1024}, "nics": {"4000": {"backing_type": "STANDARD_PORTGROUP", "mac_type": "ASSIGNED", "network": "network-1123"}}, "vm_home_storage": {"datastore": "datastore-1122"}, "vm_template": "vm-1132"}

Authors

  • Ansible Cloud Team (@ansible-collections)