-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
247 lines (210 loc) · 7.99 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
provider "vsphere" {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_server
allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
name = var.vsphere_datacenter
}
data "vsphere_datastore" "datastore" {
name = var.vsphere_datastore
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_resource_pool" "resource_pool" {
name = var.vsphere_resource_pool
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = var.vsphere_network
datacenter_id = data.vsphere_datacenter.dc.id
}
resource "vsphere_virtual_machine" "rancherserver" {
name = "${var.prefix}-rancherserver"
resource_pool_id = data.vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.rancher_cpus
memory = var.rancher_memory
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = var.vsphere_cdrom_path
}
disk {
label = "disk0"
size = var.rancher_disk_size
eagerly_scrub = "false"
thin_provisioned = "true"
}
extra_config = {
"guestinfo.cloud-init.config.data" = base64encode(templatefile("files/cloud_config_server", {
admin_password = var.admin_password,
audit_level = var.audit_level,
rancher_version = var.rancher_version,
rancher_args = var.rancher_args,
ssh_keys = var.ssh_keys,
hostname = "${var.prefix}-rancherserver",
vsphere_server = var.vsphere_server,
vsphere_user = var.vsphere_user,
vsphere_password = var.vsphere_password,
k8s_version = var.k8s_version,
cluster_name = var.cluster_name,
vsphere_server = var.vsphere_server,
vsphere_user = var.vsphere_user,
vsphere_password = var.vsphere_password,
vsphere_datacenter = var.vsphere_datacenter,
vsphere_datastore = var.vsphere_datastore,
vsphere_resource_pool = var.vsphere_resource_pool
}))
"guestinfo.cloud-init.data.encoding" = "base64"
}
}
resource "vsphere_virtual_machine" "rancheragent-all" {
count = var.count_agent_all_nodes
name = "${var.prefix}-rancheragent-all-${count.index}"
resource_pool_id = data.vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.agent_cpus
memory = var.agent_memory
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = var.vsphere_cdrom_path
}
disk {
label = "disk0"
size = var.agent_disk_size
eagerly_scrub = "false"
thin_provisioned = "true"
}
extra_config = {
"guestinfo.cloud-init.config.data" = base64encode(templatefile("files/cloud_config_agent", {
ssh_keys = var.ssh_keys,
hostname = "${var.prefix}-rancheragent-all-${count.index}",
rancher_version = var.rancher_version,
cluster_name = var.cluster_name,
server_address = vsphere_virtual_machine.rancherserver.default_ip_address
admin_password = var.admin_password
}))
"guestinfo.cloud-init.data.encoding" = "base64"
}
}
resource "vsphere_virtual_machine" "rancheragent-etcd" {
count = var.count_agent_etcd_nodes
name = "${var.prefix}-rancheragent-etcd-${count.index}"
resource_pool_id = data.vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.agent_cpus
memory = var.agent_memory
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = var.vsphere_cdrom_path
}
disk {
label = "disk0"
size = var.agent_disk_size
eagerly_scrub = "false"
thin_provisioned = "true"
}
extra_config = {
"guestinfo.cloud-init.config.data" = base64encode(templatefile("files/cloud_config_agent", {
ssh_keys = var.ssh_keys,
hostname = "${var.prefix}-rancheragent-etcd-${count.index}",
rancher_version = var.rancher_version,
cluster_name = var.cluster_name,
server_address = vsphere_virtual_machine.rancherserver.default_ip_address
admin_password = var.admin_password
}))
"guestinfo.cloud-init.data.encoding" = "base64"
}
}
resource "vsphere_virtual_machine" "rancheragent-controlplane" {
count = var.count_agent_controlplane_nodes
name = "${var.prefix}-rancheragent-controlplane-${count.index}"
resource_pool_id = data.vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.agent_cpus
memory = var.agent_memory
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = var.vsphere_cdrom_path
}
disk {
label = "disk0"
size = var.agent_disk_size
eagerly_scrub = "false"
thin_provisioned = "true"
}
extra_config = {
"guestinfo.cloud-init.config.data" = base64encode(templatefile("files/cloud_config_agent", {
ssh_keys = var.ssh_keys,
hostname = "${var.prefix}-rancheragent-controlplane-${count.index}",
rancher_version = var.rancher_version,
cluster_name = var.cluster_name,
server_address = vsphere_virtual_machine.rancherserver.default_ip_address
admin_password = var.admin_password
}))
"guestinfo.cloud-init.data.encoding" = "base64"
}
}
resource "vsphere_virtual_machine" "rancheragent-worker" {
count = var.count_agent_worker_nodes
name = "${var.prefix}-rancheragent-worker-${count.index}"
resource_pool_id = data.vsphere_resource_pool.resource_pool.id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = var.agent_cpus
memory = var.agent_memory
guest_id = "other3xLinux64Guest"
network_interface {
network_id = data.vsphere_network.network.id
}
cdrom {
datastore_id = data.vsphere_datastore.datastore.id
path = var.vsphere_cdrom_path
}
disk {
label = "disk0"
size = var.agent_disk_size
eagerly_scrub = "false"
thin_provisioned = "true"
}
extra_config = {
"guestinfo.cloud-init.config.data" = base64encode(templatefile("files/cloud_config_agent", {
ssh_keys = var.ssh_keys,
hostname = "${var.prefix}-rancheragent-worker-${count.index}",
rancher_version = var.rancher_version,
cluster_name = var.cluster_name,
server_address = vsphere_virtual_machine.rancherserver.default_ip_address
admin_password = var.admin_password
}))
"guestinfo.cloud-init.data.encoding" = "base64"
}
}
resource "local_file" "ssh_config" {
content = templatefile("${path.module}/files/ssh_config_template", {
prefix = var.prefix
rancherserver = vsphere_virtual_machine.rancherserver.default_ip_address
rancheragent-all = [for node in vsphere_virtual_machine.rancheragent-all : node.default_ip_address],
rancheragent-etcd = [for node in vsphere_virtual_machine.rancheragent-etcd : node.default_ip_address],
rancheragent-controlplane = [for node in vsphere_virtual_machine.rancheragent-controlplane : node.default_ip_address],
rancheragent-worker = [for node in vsphere_virtual_machine.rancheragent-worker : node.default_ip_address],
})
filename = "${path.module}/ssh_config"
}
output "rancher-url" {
value = ["https://${vsphere_virtual_machine.rancherserver.default_ip_address}"]
}