-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
205 lines (167 loc) · 4.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
resource "libvirt_volume" "cloudinit_image" {
count = var.cloudinit_image != "" ? 1 : 0
name = "${var.name}_cloudinit_image"
pool = var.libvirt_pool
source = var.cloudinit_image
format = "qcow2"
}
resource "libvirt_volume" "disk" {
name = "${var.name}_disk"
pool = var.libvirt_pool
base_volume_id = try(libvirt_volume.cloudinit_image[0].id, "")
size = var.disk_size
}
data "template_file" "cloudinit_user_data" {
template = <<-EOT
#cloud-config
%{if var.cloudinit_use_user_data == true~}
# From diademiemi/terraform-libvirt-vm
hostname: ${var.name}
%{if var.domain != null && var.domain != ""~}
fqdn: ${var.name}.${var.domain}
%{else~}
fqdn: ${var.name}
%{endif~}
prefer_fqdn_over_hostname: true
ssh_pwauth: ${var.password_auth}
ssh_deletekeys: False
%{if length(var.ssh_keys) > 0}
ssh_authorized_keys:
%{for key in var.ssh_keys}
- "${key}"
%{endfor}
%{endif}
disable_root: ${var.disable_root}
%{if var.root_password != ""~}
chpasswd:
list: |
root:${var.root_password}
expire: False
%{endif~}
%{if var.allow_root_ssh_pwauth != null && var.allow_root_ssh_pwauth == true}
bootcmd:
- 'echo "PermitRootLogin yes" >> /etc/ssh/sshd_config.d/99-allow-root-ssh-pwauth.conf'
%{endif}
%{endif~}
# Custom cloud-init
${coalesce(var.cloudinit_custom_user_data, "")}
EOT
}
data "template_file" "cloudinit_network_data" {
template = <<-EOT
%{if var.cloudinit_use_network_data == true~}
version: 2
ethernets:
%{for interface in var.network_interfaces~}
${interface.name~}:
%{if interface.dhcp == null~}
dhcp4: true
%{endif~}
%{if interface.dhcp != null~}
dhcp4: ${interface.dhcp}
%{endif~}
%{if interface.dhcp != true~}
%{if interface.ip != null~}
addresses: ["${interface.ip}"]
%{endif~}
%{if interface.gateway != null~}
gateway4: ${interface.gateway}
%{endif~}
%{if interface.nameservers != null~}
%{if length(interface.nameservers) > 0~}
nameservers:
addresses:
%{for nameserver in interface.nameservers~}
- ${nameserver}
%{endfor~}
%{endif~}
%{endif~}
%{if interface.additional_routes != null~}
%{if length(interface.additional_routes) > 0~}
routes:
%{for route in interface.additional_routes~}
- to: ${route.network}
via: ${route.gateway}
%{endfor~}
%{endif~}
%{endif~}
%{endif~}
%{endfor~}
%{endif~}
# Custom cloud-init
${coalesce(var.cloudinit_custom_network_data, "")}
EOT
}
resource "libvirt_cloudinit_disk" "init_disk" {
name = "${var.name}_cloudinit"
pool = var.libvirt_pool
user_data = data.template_file.cloudinit_user_data.rendered
network_config = data.template_file.cloudinit_network_data.rendered
}
resource "libvirt_domain" "domain" {
name = var.domain != null && var.domain != "" ? "${var.name}.${var.domain}" : var.name
memory = var.memory
vcpu = var.vcpu
cpu {
mode = "host-passthrough"
}
autostart = var.autostart
cloudinit = libvirt_cloudinit_disk.init_disk.id // Attach cloud-init disk
disk {
volume_id = libvirt_volume.disk.id
}
dynamic "disk" {
for_each = var.disk_passthroughs
content {
block_device = disk.value
}
}
dynamic "disk" {
for_each = var.iso_urls
content {
url = disk.value
}
}
dynamic "disk" {
for_each = var.iso_paths
content {
file = disk.value
}
}
boot_device {
dev = ["hd", "cdrom", "network"]
}
dynamic "network_interface" {
for_each = var.network_interfaces
content {
macvtap = network_interface.value.macvtap
network_name = network_interface.value.network_name
network_id = network_interface.value.network_id
hostname = network_interface.value.hostname
wait_for_lease = network_interface.value.wait_for_lease
mac = network_interface.value.mac // For some providers, this is required
}
}
xml {
xslt = file("${path.module}/nicmodel.xsl")
}
console {
type = "pty"
target_port = "0"
target_type = "serial"
}
graphics {
type = "spice"
listen_type = var.spice_enabled ? "address" : "none"
}
}
resource "ansible_host" "default" {
name = coalesce(var.ansible_name, var.name)
groups = var.ansible_groups
variables = {
ansible_host = coalesce(var.ansible_host, try(split("/", var.network_interfaces[0].ip).0, var.domain != null && var.domain != "" ? "${var.name}.${var.domain}" : var.name))
ansible_user = coalesce(var.ansible_user, "root")
ansible_ssh_pass = coalesce(var.ansible_ssh_pass, var.root_password, "root")
ansible_ssh_private_key_file = try(var.ansible_ssh_private_key_file, "")
}
}