Terraform Provider for VMware vSphere

repository·main·Indexed 20 days ago

https://github.com/vmware/terraform-provider-vsphere

A Terraform plugin that enables the management and interaction of VMware vSphere infrastructure. It provides data sources for discovering vSphere alarms, compute clusters, content libraries, and more. Requires Terraform version 0.13 or higher and compatible VMware vSphere versions.

Tokens
94.7K
Snippets
253
Records
415
Agent score
69%

What's inside terraform-provider-vsphere

  1. Manage VMware vSphere resource pools with vsphere_resource_pool

    main

    The vsphere_resource_pool resource allows you to create and manage resource pools on DRS-enabled vSphere clusters or standalone ESXi hosts. You can use these pools to partition CPU and memory resources for virtual machines or nested resource pools.

    Key Concepts

    • Parenting: Every resource pool must have a parent_resource_pool_id. This can be the root resource pool of a cluster, a standalone host, or another resource pool (for nesting).
    • Moving Pools: When moving a resource pool from one parent to another, both the old and new parents must share a common root resource pool.
    • Targeting VMs: To assign a virtual machine to a specific resource pool, set the resource_pool_id argument within the vsphere_virtual_machine resource.
    resource "vsphere_resource_pool" "resource_pool" {
      name                    = "resource-pool-01"
      parent_resource_pool_id = data.vsphere_compute_cluster.compute_cluster.resource_pool_id
    }
    
    resource "vsphere_virtual_machine" "vm" {
      # ... other configuration ...
      resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
      # ... other configuration ...
    }
  2. Manage vSphere standard switches with vsphere_host_virtual_switch

    main

    The vsphere_host_virtual_switch resource manages standard virtual switches (vSwitches) directly on an ESXi host. These switches serve as the underlying infrastructure for standard port groups, which can be managed using the vsphere_host_port_group resource.

    Key considerations:

    • Changing the number_of_ports requires a host reboot, which Terraform will not perform automatically.
    • Virtual switch names must be unique on the host.
    • This resource is used for standard switches, not distributed switches.
    resource "vsphere_host_virtual_switch" "switch" {
      name           = "vSwitchTerraformTest"
      host_system_id = data.vsphere_host.host.id
    
      network_adapters = ["vmnic0", "vmnic1"]
    
      active_nics  = ["vmnic0"]
      standby_nics = ["vmnic1"]
    }
  3. Manage vSphere Distributed Switches with vsphere_distributed_virtual_switch

    main

    The vsphere_distributed_virtual_switch resource allows you to create and manage vSphere Distributed Switches (VDS) within vCenter Server. A VDS provides centralized management and monitoring for networking configurations across all associated hosts. It can be used to host distributed port groups (via the vsphere_distributed_port_group resource) and supports advanced features like high availability and traffic shaping.

    Important Requirement: This resource requires a connection to vCenter; it is not available for direct ESXi host connections.

    resource "vsphere_distributed_virtual_switch" "vds" {
      name          = "vds-01"
      datacenter_id = data.vsphere_datacenter.datacenter.id
    
      uplinks         = ["uplink1", "uplink2"]
      active_uplinks  = ["uplink1"]
      standby_uplinks = ["uplink2"]
    }
  4. Manage ESXi host port groups with vsphere_host_port_group

    main

    The vsphere_host_port_group resource manages port groups on ESXi hosts that are connected to standard switches. These port groups are bound to a virtual switch managed by the vsphere_host_virtual_switch resource.

    Note that port groups on standard switches are tracked differently than Distributed Port Groups (dvPortGroups) in vCenter; they are identified by a key on the host rather than a vCenter managed object ID.

    resource "vsphere_host_port_group" "pg" {
      name                = "portgroup-01"
      host_system_id      = data.vsphere_host.host.id
      virtual_switch_name = vsphere_host_virtual_switch.host_virtual_switch.name
    }
  5. Manage vSphere distributed port groups with vsphere_distributed_port_group

    main

    The vsphere_distributed_port_group resource manages distributed port groups on a vSphere Distributed Switch (VDS). These port groups serve as networks for virtual machines, allowing for specific networking policies to be applied to that individual network.

    Note: This resource requires vCenter and is not available on direct ESXi host connections.

    resource "vsphere_distributed_port_group" "pg" {
      name                            = "pg-01"
      distributed_virtual_switch_uuid = vsphere_distributed_virtual_switch.vds.id
    
      vlan_id = 1000
    }
  6. Manage VMware vSphere cluster virtual machine groups with vsphere_compute_cluster_vm_group

    main

    The vsphere_compute_cluster_vm_group resource manages groups of virtual machines within a specific vSphere cluster. These groups are primarily used as inputs for defining rules in the cluster, such as vsphere_compute_cluster_vm_dependency_rule and vsphere_compute_cluster_vm_host_rule resources.

    Important Requirements:

    • This resource requires a vCenter connection; it is not available for direct ESXi connections.
    • The name argument shares a namespace with vsphere_compute_cluster_host_group. Ensure names are unique across both resource types to avoid conflicts.
    • To update an existing VM group, you must first import it using the terraform import command.
    resource "vsphere_compute_cluster_vm_group" "cluster_vm_group" {
      name                = "terraform-test-cluster-vm-group"
      compute_cluster_id  = data.vsphere_compute_cluster.cluster.id
      virtual_machine_ids = ["${vsphere_virtual_machine.vm.*.id}"]
    }
  7. Extract OVF/OVA configuration with vsphere_ovf_vm_template

    main

    The vsphere_ovf_vm_template data source allows you to submit an OVF or OVA template to vSphere to extract its hardware settings (such as CPU, memory, and guest ID). These extracted attributes can then be passed directly into a vsphere_virtual_machine resource to ensure the deployed VM matches the template's specifications.

    data "vsphere_ovf_vm_template" "example" {
      name              = "template-name"
      resource_pool_id  = data.vsphere_resource_pool.default.id
      datastore_id      = data.vsphere_datastore.datastore.id
      host_system_id    = data.vsphere_host.host.id
      remote_ovf_url    = "https://example.com/template.ova"
      ovf_network_map   = {
        "VM Network" : data.vsphere_network.network.id
      }
    }
  8. Configure Storage DRS automation levels

    main

    Storage DRS automation levels can be set globally or overridden for specific subsystems. Settings can be either manual (makes recommendations only) or automated (executes migrations automatically).

    Global and Subsystem Settings:

    • sdrs_automation_level: Global automation level for all VMs. Default: manual.
    • sdrs_space_balance_automation_level: Override for correcting disk space imbalances.
    • sdrs_io_balance_automation_level: Override for correcting I/O load imbalances.
    • sdrs_rule_enforcement_automation_level: Override for correcting affinity rule violations.
    • sdrs_policy_enforcement_automation_level: Override for correcting storage and VM policy violations.
    • sdrs_vm_evacuation_automation_level: Override for generating recommendations for datastore evacuation.
  9. Configure vApp Properties for OVF/OVA Templates

    main

    For templates originating from OVF/OVA files, you can supply configuration parameters using the vapp block. This uses the properties map to set GuestInfo or ISO transport parameters.

    Important Usage Notes:

    • Boolean Values: While Terraform uses bool (true/false), vSphere OVF properties often require the strings "True" or "False" (title case). It is recommended to define these as strings in your Terraform variables to ensure correct formatting.
    • Compatibility: You can only set existing user-configurable keys that were present in the original template. You cannot set properties on VMs created from scratch or those lacking vApp configuration.
    variable "ssh_enabled" {
      type        = string
      description = "Enable SSH on the virtual appliance. One of `True` or `False`."
      default     = "False"
    }
    
    resource "vsphere_virtual_machine" "vm" {
      # ... other configurations ...
      vapp {
        properties = {
          "ssh_enabled" = var.ssh_enabled
        }
      }
    }
  10. Use vSphere Storage DRS with Virtual Machines

    main

    The vsphere_virtual_machine resource supports vSphere Storage DRS by allowing you to assign a virtual machine to a datastore cluster instead of a single datastore. Use the datastore_cluster_id argument to achieve this.

    Important Note on Race Conditions: When managing datastore clusters, member datastores, and virtual machines in the same configuration, a race condition can occur. The vsphere_virtual_machine resource does not have an implicit dependency on the individual datastores within a cluster. To prevent errors, use the depends_on meta-argument to create an explicit dependency on the datastores in the cluster, or manage the datastore cluster and its datastores in a separate Terraform configuration.

    data "vsphere_datastore_cluster" "datastore_cluster" {
      name          = "datastore-cluster-01"
      datacenter_id = data.vsphere_datacenter.datacenter.id
    }
    
    resource "vsphere_virtual_machine" "vm" {
      name                 = "foo"
      resource_pool_id     = data.vsphere_compute_cluster.cluster.resource_pool_id
      datastore_cluster_id = data.vsphere_datastore_cluster.datastore_cluster.id
      num_cpus             = 1
      memory               = 1024
      guest_id             = "otherLinux64Guest"
      network_interface {
        network_id = data.vsphere_network.network.id
      }
      disk {
        label = "Hard Disk 1"
        size  = 20
      }
    }