-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
118 lines (99 loc) · 3.68 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# terraform {
# required_providers {
# vsphere = "~> 1.15.0" // at this momment 1.16.1 is provided
# }
# }
// PROVIDER declaration and connection parameters.
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
# if you have a self-signed certificate
allow_unverified_ssl = true
}
/*
DATA declaration
This section gathers data from provider's API that Terraform
needs to provision the infrastructure. For details on what
the API returns see the provider's documentation.
https://www.terraform.io/docs/providers/index.html
*/
// Data source that gathers the info for the datacenter.
data "vsphere_datacenter" "dc" {
name = var.vsphere_datacenter
}
// Populates the datastore source from this datacenter with info.
data "vsphere_datastore" "datastore" {
name = var.vsphere_datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
// Populates with data the cluster object from this datacenter.
data "vsphere_compute_cluster" "cluster" {
name = var.vsphere_compute_cluster
datacenter_id = data.vsphere_datacenter.dc.id
}
// Get data about the network for the new VM in this datacenter.
data "vsphere_network" "network" {
name = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
}
// Get info for the template used to clone the VM.
data "vsphere_virtual_machine" "template" {
name = var.vsphere_vm_template
datacenter_id = data.vsphere_datacenter.dc.id
}
// Get info for the resource pool in which will be created the VM.
data "vsphere_resource_pool" "pool" {
name = var.res_pool_name
datacenter_id = data.vsphere_datacenter.dc.id
depends_on = [
vsphere_resource_pool.resource_pool
]
}
// RESOURCE declaration
// Creates the resource pool in which the VM will be created.
resource "vsphere_resource_pool" "resource_pool" {
name = var.res_pool_name
parent_resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
}
// Creates the VM.
resource "vsphere_virtual_machine" "vm" {
name = var.vsphere_vm_name
# resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
# resource_pool_id = "resgroup-530"
resource_pool_id = data.vsphere_resource_pool.pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.vm_num_cpus
memory = var.vm_memory
guest_id = data.vsphere_virtual_machine.template.guest_id
nested_hv_enabled = true // enable nested hw virtulization - default false
// see docs https://www.terraform.io/docs/providers/vsphere/r/virtual_machine.html#nested_hv_enabled
scsi_type = data.vsphere_virtual_machine.template.scsi_type
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
}
disk {
label = "disk0"
size = data.vsphere_virtual_machine.template.disks.0.size
eagerly_scrub = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
# customize {
# linux_options {
# host_name = var.vm_host_name
# domain = var.vm_domain_name
# }
# network_interface {
# ipv4_address = var.vm_ipv4_address
# ipv4_netmask = var.vm_ipv4_netmask
# }
# ipv4_gateway = var.vm_ipv4_gateway
# # this will to allow to specify multiple values for dns servers
# dns_server_list = var.vm_dns_servers
# dns_suffix_list = [var.vm_dns_search_domain]
# }
}
}