-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
159 lines (142 loc) · 4.8 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
##############################################
# #
# Create storage pool for the project #
# #
##############################################
resource "libvirt_pool" "project_pool" {
name = var.project_name
type = "dir"
path = "${var.pool_prefix}/${var.project_name}"
}
##################################################
# #
# Create dedicated network for the project #
# #
##################################################
resource "libvirt_network" "project_network" {
name = "${var.project_name}.${var.network_tld}"
domain = "${var.project_name}.${var.network_tld}"
addresses = var.cidr_networks
autostart = true
dns {
enabled = true
local_only = true
}
}
#############################################################
# #
# Create base volumes shared by the Operating Systems #
# #
#############################################################
module "auto_base_volume" {
source = "./modules/auto_base_volume"
# From global_config.tf
templates_path = var.templates_path
ansible_playbooks = var.ansible_playbooks
os_image_catalog = var.os_image_catalog
# From variables.tf
project_name = var.project_name
project_machines = var.project_machines
project_pool_name = libvirt_pool.project_pool.name
}
####################################
# #
# Create volumes for the VMs #
# #
####################################
resource "libvirt_volume" "cow_volume" {
for_each = var.project_machines
name = "${each.key}.${libvirt_network.project_network.domain}.qcow2"
# Use the base volume created above
base_volume_id = module.auto_base_volume.volumes["${each.value.distro}"].id
base_volume_pool = libvirt_pool.project_pool.name
pool = libvirt_pool.project_pool.name
size = each.value.disk_size * 1024 * 1024 * 1024
}
##################################
# #
# Create cloudinit volumes #
# #
##################################
module "auto_cloudinit" {
source = "./modules/auto_cloudinit/"
# From global_config.tf
os_image_catalog = var.os_image_catalog
# From variables.tf
project_name = var.project_name
project_machines = var.project_machines
project_pool_name = libvirt_pool.project_pool.name
ssh_pubkey = var.ssh_pubkey_file
}
#############################
# #
# Create the machines #
# #
#############################
resource "libvirt_domain" "domain" {
for_each = var.project_machines
name = "${each.key}.${libvirt_network.project_network.domain}"
memory = each.value.memory
vcpu = each.value.vcpu
qemu_agent = each.value.qemu_agent
cloudinit = var.os_image_catalog["${each.value.distro}"].cloudinit_template != "" ? module.auto_cloudinit.cloudinit_disks["${each.key}"].id : null
cpu {
mode = "host-passthrough"
}
disk {
volume_id = libvirt_volume.cow_volume[each.key].id
}
network_interface {
network_id = libvirt_network.project_network.id
wait_for_lease = true
}
video {
type = var.os_image_catalog["${each.value.distro}"].video_type
}
xml {
# A serial port is neede by Debian for resizing the disk via cloud-init
xslt = file("xslt/add_spice_and_serial.xsl")
}
# Shared folders
dynamic "filesystem" {
for_each = each.value.filesystems
content {
source = filesystem.value["source"]
target = filesystem.key
readonly = filesystem.value["readonly"]
}
}
# Deployment playbook: name the machine and do other stuff
# See ansible/windows_deploy.yml
# It can be empty for machines deployed via cloud init
provisioner "local-exec" {
command = <<-EOC
ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook \
-u ${var.os_image_catalog["${each.value.distro}"].ansible_user} \
-i ${self.name}, \
-e newhostname=${self.name} ${var.ansible_playbooks}/${var.os_image_catalog[each.value.distro].provision_playbook} \
|| echo "No ansible playbook supplied :)"
EOC
}
# Remove the ssh fingerprint from known_hosts
provisioner "local-exec" {
when = destroy
command = <<-EOC
sleep $[ ($RANDOM % 3) + 1 ]s
ssh-keygen -R ${self.name}
EOC
}
timeouts {
create = "3m"
}
}