-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
110 lines (94 loc) · 2.42 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
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.6.14"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
data "template_file" "user_data" {
template = file("${path.module}/cloud_init.cfg")
vars = {
VM_USER = var.VM_USER
}
}
data "template_file" "network_config" {
template = file("${path.module}/network_config.cfg")
}
resource "libvirt_pool" "vm" {
name = "${var.VM_HOSTNAME}_pool"
type = "dir"
path = "/images_kvm/terraform-provider-libvirt-pool-ubuntu"
}
resource "libvirt_volume" "base_ubuntu_qcow2" {
name = "${var.VM_HOSTNAME}-base_volume.${var.VM_IMG_FORMAT}"
pool = libvirt_pool.vm.name
source = var.VM_IMG_URL
format = var.VM_IMG_FORMAT
}
resource "libvirt_volume" "vm" {
count = var.VM_COUNT
name = "${var.VM_HOSTNAME}-${count.index}_volume.${var.VM_IMG_FORMAT}"
pool = libvirt_pool.vm.name
format = var.VM_IMG_FORMAT
size = var.VM_VOLUME_SIZE
base_volume_id = libvirt_volume.base_ubuntu_qcow2.id
}
resource "libvirt_network" "vm_network" {
name = "${var.VM_HOSTNAME}_network"
#autostart = true
mode = "nat"
#bridge = "br0"
domain = "${var.VM_HOSTNAME}.local"
addresses = ["${var.VM_CIDR_RANGE}"]
dhcp {
enabled = true
}
dns {
enabled = true
}
}
resource "libvirt_cloudinit_disk" "cloudinit" {
name = "${var.VM_HOSTNAME}_cloudinit.iso"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
pool = libvirt_pool.vm.name
}
resource "libvirt_domain" "vm" {
count = var.VM_COUNT
name = "${var.VM_HOSTNAME}-${count.index}"
memory = var.MEMORY_SIZE
vcpu = var.VCPU_SIZE
cloudinit = "${libvirt_cloudinit_disk.cloudinit.id}"
qemu_agent = true
autostart = true
network_interface {
hostname = var.VM_HOSTNAME
network_name = "${libvirt_network.vm_network.name}"
network_id = "${libvirt_network.vm_network.id}"
}
console {
type = "pty"
target_type = "serial"
target_port = "0"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = "${libvirt_volume.vm[count.index].id}"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
output "IPs" {
value = "${libvirt_domain.vm.*.network_interface.0.addresses}"
}