-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
108 lines (93 loc) · 2.51 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
# Render template for provisioning script
data "template_file" "provisioning_script" {
template = file("${path.module}/${var.provisioning_script_path}")
vars = {
user = var.user
timezone = var.timezone
}
}
# Create the VM
resource "proxmox_vm_qemu" "proxmox-vm" {
# amount of instances
count = length(var.hostnames)
# notes in pve UI
desc = <<-EOT
------------------------------------------------------
OS Type: ${var.os_description}
Created at: ${formatdate("EEE DD/MM/YYYY hh'h'mm AA (ZZZ)", timestamp())}
Created by: Terraform
------------------------------------------------------
EOT
# vm details
vmid = var.vmids[count.index]
name = var.hostnames[count.index]
target_node = var.target_node
# template to clone
clone = var.proxmox_template
full_clone = true
os_type = "cloud-init"
# resources
memory = var.memory
balloon = var.balloon
sockets = var.sockets
cores = var.cores
cpu = "kvm64"
numa = true
# extra vm settings
boot = "c"
bootdisk = "virtio0"
scsihw = "virtio-scsi-pci"
onboot = true
agent = 1
hotplug = "network,disk,cpu,memory"
# network interface
network {
model = "virtio"
bridge = var.bridge
}
# storage
disk {
type = "virtio"
storage = var.storage_name
size = var.disksize
}
# cloud init
# cicustom does not quit work yet, might be usable in next versions
# cicustom = "user=local:snippets/cloud_init_ubuntu_focal.yml"
ipconfig0 = "ip=${var.ipv4s[count.index]}/${var.subnet_short},gw=${var.gateway}"
nameserver = var.nameserver
searchdomain = var.searchdomain
ciuser = var.user
cipassword = var.password
sshkeys = file(var.ssh_key_public)
# Check if VM is ready:
# Terraform will not continue unless the connection is successful
connection {
type = "ssh"
agent = false
timeout = "3m"
host = var.ipv4s[count.index]
user = var.user
private_key = file(var.ssh_key_private)
}
# provision vm with basic config
provisioner "file" {
destination = "/tmp/provision.sh"
content = data.template_file.provisioning_script.rendered
}
provisioner "remote-exec" {
# on_failure = continue
inline = [
"sudo hostnamectl set-hostname ${var.hostnames[count.index]}",
"sudo chmod +x /tmp/provision.sh",
"sudo /tmp/provision.sh"
]
}
# Ignore network changes since TF generates a new MAC address
# on every apply causing the vm to upate on every apply.
lifecycle {
ignore_changes = [
network
]
}
}