-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
68 lines (56 loc) · 1.62 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
resource "null_resource" "download_image" {
provisioner "local-exec" {
command = "test -f ${path.module}/${var.cloud_image_name} || wget -O ${path.module}/${var.cloud_image_name} ${var.cloud_image_url}"
}
}
resource "libvirt_volume" "ubuntu_img" {
name = var.cloud_image_name
pool = "default"
source = "${path.module}/${var.cloud_image_name}"
format = "qcow2"
depends_on = [null_resource.download_image]
}
resource "libvirt_volume" "vm_disk" {
name = "${var.vm_name}.qcow2"
pool = "default"
base_volume_id = libvirt_volume.ubuntu_img.id
format = "qcow2"
}
resource "libvirt_cloudinit_disk" "cloudinit" {
name = "${var.vm_name}-cloudinit.iso"
pool = "default"
user_data = data.template_file.user_data.rendered
}
data "template_file" "user_data" {
template = file("${path.module}/cloud-init/user_data.tpl")
vars = {
username = var.username
password = var.password
hostname = var.vm_name
interface1_name = "ens3"
interface2_name = "ens4"
interface1_config = indent(6, yamlencode(var.vm_interface1_config))
interface2_config = indent(6, yamlencode(var.vm_interface2_config))
}
}
resource "libvirt_domain" "vm" {
name = var.vm_name
memory = var.memory
vcpu = var.vcpu
autostart = true
network_interface {
bridge = var.bridge1_name
}
network_interface {
bridge = var.bridge2_name
}
disk {
volume_id = libvirt_volume.vm_disk.id
}
cloudinit = libvirt_cloudinit_disk.cloudinit.id
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
}