forked from ArmanTaheriGhaleTaki/terraform-libvirt-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
69 lines (55 loc) · 1.46 KB
/
main.tf
File metadata and controls
69 lines (55 loc) · 1.46 KB
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
provider "libvirt" {
uri = "qemu:///system"
}
data "template_file" "user_data" {
template = file("${path.module}/config/cloud_init.cfg")
}
data "template_file" "network_config" {
template = file("${path.module}/config/network_config.yml")
}
# Create a disk for each VM
resource "libvirt_volume" "ubuntu_qcow2" {
for_each = var.vms
name = "${each.key}-ubuntu-disk.qcow2"
source = var.ubuntu_18_img_url
format = "qcow2"
# size = each.value.disk_size
}
# Create a cloudinit disk for each VM
resource "libvirt_cloudinit_disk" "commoninit" {
for_each = var.vms
name = "${each.key}-commoninit.iso"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
}
# Create a VM (domain) for each VM configuration
resource "libvirt_domain" "domain_ubuntu" {
for_each = var.vms
name = each.value.vm_hostname
memory = each.value.memory
vcpu = each.value.vcpu
cloudinit = libvirt_cloudinit_disk.commoninit[each.key].id
network_interface {
network_name = "default"
wait_for_lease = true
hostname = each.value.vm_hostname
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = libvirt_volume.ubuntu_qcow2[each.key].id
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}