-
Notifications
You must be signed in to change notification settings - Fork 70
/
main.tf
209 lines (169 loc) · 6.83 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
##### Terraform Initialization
terraform {
required_version = ">= 0.13"
required_providers {
vsphere = {
source = "hashicorp/vsphere"
version = "1.24.3"
}
}
}
##### Provider
provider "vsphere" {
user = var.provider_vsphere_user
password = var.provider_vsphere_password
vsphere_server = var.provider_vsphere_host
# if you have a self-signed cert
allow_unverified_ssl = true
}
##### Data sources
data "vsphere_datacenter" "target_dc" {
name = var.deploy_vsphere_datacenter
}
data "vsphere_datastore" "target_datastore" {
name = var.deploy_vsphere_datastore
datacenter_id = data.vsphere_datacenter.target_dc.id
}
data "vsphere_compute_cluster" "target_cluster" {
name = var.deploy_vsphere_cluster
datacenter_id = data.vsphere_datacenter.target_dc.id
}
data "vsphere_network" "target_network" {
name = var.deploy_vsphere_network
datacenter_id = data.vsphere_datacenter.target_dc.id
}
data "vsphere_virtual_machine" "source_template" {
name = var.guest_template
datacenter_id = data.vsphere_datacenter.target_dc.id
}
##### Resources
# Clones a single Linux VM from a template
resource "vsphere_virtual_machine" "kubernetes_master" {
count = length(var.master_ips)
name = "${var.guest_name_prefix}-master0${count.index + 1}"
resource_pool_id = data.vsphere_compute_cluster.target_cluster.resource_pool_id
datastore_id = data.vsphere_datastore.target_datastore.id
folder = var.deploy_vsphere_folder
firmware = var.guest_firmware
num_cpus = var.guest_vcpu
memory = var.guest_memory
guest_id = data.vsphere_virtual_machine.source_template.guest_id
scsi_type = data.vsphere_virtual_machine.source_template.scsi_type
network_interface {
network_id = data.vsphere_network.target_network.id
adapter_type = data.vsphere_virtual_machine.source_template.network_interface_types[0]
}
disk {
label = "disk0"
size = data.vsphere_virtual_machine.source_template.disks[0].size
eagerly_scrub = data.vsphere_virtual_machine.source_template.disks[0].eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.source_template.disks[0].thin_provisioned
}
clone {
template_uuid = data.vsphere_virtual_machine.source_template.id
customize {
linux_options {
host_name = "${var.guest_name_prefix}-master0${count.index + 1}"
domain = var.guest_domain
}
network_interface {
ipv4_address = lookup(var.master_ips, count.index)
ipv4_netmask = var.guest_ipv4_netmask
}
ipv4_gateway = var.guest_ipv4_gateway
dns_server_list = [var.guest_dns_servers]
dns_suffix_list = [var.guest_dns_suffix]
}
}
boot_delay = 10000
# Remove existing SSH known hosts as remote identification (host key) changes between deployments.
provisioner "local-exec" {
command = "ssh-keygen -R ${self.guest_ip_addresses[0]}"
}
# Ansible requires Python to be installed on the remote machines (as well as the local machine).
provisioner "remote-exec" {
inline = ["sudo apt-get update && sudo apt-get -qq install python -y"]
connection {
type = "ssh"
user = var.guest_ssh_user
password = var.guest_ssh_password
host = self.guest_ip_addresses[0]
}
}
# Disabling SSH authenticity checking StrictHostKeyChecking=no, to avoid beeing asked to add RSA key fingerprint of a host when you access it for the first time.
provisioner "local-exec" {
command = "sshpass -p ${var.guest_ssh_password} ssh-copy-id -i ${var.guest_ssh_key_public} -o StrictHostKeyChecking=no ${var.guest_ssh_user}@${self.guest_ip_addresses[0]}"
}
# Prepare operating system for kubernetes using Ansible
#provisioner "local-exec" {
# command = "ansible-playbook -i '${self.guest_ip_addresses[0]},' --private-key ${var.guest_ssh_key_private} ../ansible/k8s-preparation.yml"
#}
lifecycle {
ignore_changes = [annotation]
}
}
# Clones multiple Linux VMs from a template
resource "vsphere_virtual_machine" "kubernetes_workers" {
count = length(var.worker_ips)
name = "${var.guest_name_prefix}-worker0${count.index + 1}"
resource_pool_id = data.vsphere_compute_cluster.target_cluster.resource_pool_id
datastore_id = data.vsphere_datastore.target_datastore.id
folder = var.deploy_vsphere_folder
firmware = var.guest_firmware
num_cpus = var.guest_vcpu
memory = var.guest_memory
guest_id = data.vsphere_virtual_machine.source_template.guest_id
scsi_type = data.vsphere_virtual_machine.source_template.scsi_type
network_interface {
network_id = data.vsphere_network.target_network.id
adapter_type = data.vsphere_virtual_machine.source_template.network_interface_types[0]
}
disk {
label = "disk0"
size = data.vsphere_virtual_machine.source_template.disks[0].size
eagerly_scrub = data.vsphere_virtual_machine.source_template.disks[0].eagerly_scrub
thin_provisioned = data.vsphere_virtual_machine.source_template.disks[0].thin_provisioned
}
clone {
template_uuid = data.vsphere_virtual_machine.source_template.id
customize {
linux_options {
host_name = "${var.guest_name_prefix}-worker0${count.index + 1}"
domain = var.guest_domain
}
network_interface {
ipv4_address = lookup(var.worker_ips, count.index)
ipv4_netmask = var.guest_ipv4_netmask
}
ipv4_gateway = var.guest_ipv4_gateway
dns_server_list = [var.guest_dns_servers]
dns_suffix_list = [var.guest_dns_suffix]
}
}
boot_delay = 10000
# Remove existing SSH known hosts as remote identification (host key) changes between deployments.
provisioner "local-exec" {
command = "ssh-keygen -R ${self.guest_ip_addresses[0]}"
}
# Ansible requires Python to be installed on the remote machines (as well as the local machine).
provisioner "remote-exec" {
inline = ["sudo apt-get update && sudo apt-get -qq install python -y"]
connection {
type = "ssh"
user = var.guest_ssh_user
password = var.guest_ssh_password
host = self.guest_ip_addresses[0]
}
}
# Disabling SSH authenticity checking StrictHostKeyChecking=no, to avoid beeing asked to add RSA key fingerprint of a host when you access it for the first time.
provisioner "local-exec" {
command = "sshpass -p ${var.guest_ssh_password} ssh-copy-id -i ${var.guest_ssh_key_public} -o StrictHostKeyChecking=no ${var.guest_ssh_user}@${self.guest_ip_addresses[0]}"
}
# Prepare operating system for kubernetes using Ansible
#provisioner "local-exec" {
# command = "ansible-playbook -i '${self.guest_ip_addresses[0]},' --private-key ${var.guest_ssh_key_private} ../ansible/k8s-preparation.yml"
#}
lifecycle {
ignore_changes = [annotation]
}
}