-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
126 lines (108 loc) · 3.33 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.6"
}
}
}
provider "libvirt" {
uri = var.libvirt_uri
}
data "template_file" "user_data" {
count = var.number_of_instances
template = file("${path.module}/templates/cloud_init.cfg")
vars = {
ssh_public_key = file(var.ssh_public_key)
hostname = "${var.instance_name}-${terraform.workspace}-${count.index}"
}
}
data "template_file" "network_config" {
count = var.number_of_instances
template = file("${path.module}/templates/network_config.cfg")
vars = {
ip_address = cidrhost(var.network_address, count.index + 2)
netmask = cidrnetmask(var.network_address)
gateway = cidrhost(var.network_address, 1)
}
}
# for more info about paramater check this out
# https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/cloudinit.html.markdown
# Use CloudInit to add our ssh-key to the instance
# you can add also meta_data field
resource "libvirt_cloudinit_disk" "commoninit" {
count = var.number_of_instances
name = "${var.instance_name}-${terraform.workspace}-${count.index}.iso"
pool = var.libvirt_cloudinit_disk_pool
user_data = data.template_file.user_data[count.index].rendered
network_config = data.template_file.network_config[count.index].rendered
}
resource "libvirt_volume" "instance_volume" {
count = var.number_of_instances
name = "${var.instance_name}-${terraform.workspace}-${count.index}.qcow2"
pool = var.libvirt_volume_pool
source = var.libvirt_volume_source
format = "qcow2"
}
# Create the machine
resource "libvirt_domain" "instance_domain" {
count = var.number_of_instances
name = "${var.instance_name}-${terraform.workspace}-${count.index}"
memory = var.instance_memory
vcpu = var.instance_vcpu
cloudinit = libvirt_cloudinit_disk.commoninit.*.id[count.index]
network_interface {
network_id = libvirt_network.network.id
wait_for_lease = true
}
# IMPORTANT: this is a known bug on cloud images, since they expect a console
# we need to pass it
# https://bugs.launchpad.net/cloud-images/+bug/1573095
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
disk {
volume_id = libvirt_volume.instance_volume.*.id[count.index]
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
data "libvirt_network_dns_host_template" "hosts" {
count = var.number_of_instances
hostname = data.template_file.user_data[count.index].vars.hostname
ip = libvirt_domain.instance_domain[count.index].network_interface.0.addresses[0]
}
resource "libvirt_network" "network" {
name = "${var.instance_name}-${terraform.workspace}-network"
mode = var.network_mode
domain = var.domain
dhcp { enabled = true }
addresses = [ var.network_address ]
autostart = true
dns {
enabled = true
local_only = false
}
}
output "disk_id" {
value = libvirt_volume.instance_volume.*.id
}
output "network_id" {
value = libvirt_network.network.id
}
output "hosts" {
value = data.libvirt_network_dns_host_template.hosts.*.rendered
}
output "ipv4" {
value = libvirt_domain.instance_domain.*.network_interface.0.addresses
}