-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
57 lines (51 loc) · 2.03 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
locals {
vm_img = "/var/lib/libvirt/images/vm.img"
vm_size = "120G"
}
# configure vms
data "template_file" "vm_master" {
count = var.master_count
template = file("vm.tpl")
vars = {
name = "${var.master_nodes[count.index]["name"]}"
memory_gb = "${var.master_nodes[count.index]["memory_gb"]}"
vcpu = "${var.master_nodes[count.index]["vcpu"]}"
vm_img = "${local.vm_img}"
provisioning_bridge = "${var.master_nodes[count.index]["provisioning_bridge"]}"
baremetal_bridge = "${var.master_nodes[count.index]["baremetal_bridge"]}"
baremetal_mac_address = "${var.master_nodes[count.index]["baremetal_mac_address"]}"
provisioning_mac_address = "${var.master_nodes[count.index]["provisioning_mac_address"]}"
}
}
resource "local_file" "vm_master" {
count = var.master_count
content = "${data.template_file.vm_master[count.index]["rendered"]}"
filename = "/tmp/master_vm_${count.index}.xml"
}
resource "null_resource" "vm_master" {
count = var.master_count
provisioner "local-exec" {
command = <<EOT
rm -f ${local.vm_img} || true
qemu-img create -f qcow2 ${local.vm_img} ${local.vm_size}
chown qemu:qemu ${local.vm_img}
virsh define /tmp/master_vm_${count.index}.xml
vbmc add ${var.master_nodes[count.index]["name"]} --username ${var.master_nodes[count.index]["ipmi_user"]} --password ${var.master_nodes[count.index]["ipmi_password"]} --port ${var.master_nodes[count.index]["ipmi_port"]}
vbmc start ${var.master_nodes[count.index]["name"]}
EOT
}
depends_on = [local_file.vm_master]
}
# destroy vm
resource "null_resource" "vm_master_destroy" {
count = var.master_count
provisioner "local-exec" {
when = "destroy"
command = <<EOT
virsh destroy ${var.master_nodes[count.index]["name"]} || true
virsh undefine ${var.master_nodes[count.index]["name"]} || true
vbmc stop ${var.master_nodes[count.index]["name"]}
vbmc delete ${var.master_nodes[count.index]["name"]}
EOT
}
}