-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
119 lines (97 loc) · 2.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
provider "libvirt" {
uri = "qemu:///system"
}
resource "libvirt_volume" "base" {
name = "${var.vm_hostname}-base"
pool = "default"
source = var.disk_img_url
format = "qcow2"
}
resource "libvirt_volume" "root" {
name = "${var.vm_hostname}-root"
pool = "default"
base_volume_id = libvirt_volume.base.id
size = 50 * 1024 * 1024 * 1024 #50GB
}
data "template_file" "user_data" {
template = file("${path.module}/config/cloud_init.yml")
vars = {
ssh_username = var.ssh_username
ssh_public_key = file(var.ssh_public_key)
}
}
data "template_file" "network_config" {
template = file("${path.module}/config/network_config.yml")
}
resource "libvirt_cloudinit_disk" "commoninit" {
name = "${var.vm_hostname}-init.iso"
user_data = data.template_file.user_data.rendered
network_config = data.template_file.network_config.rendered
pool = "default"
}
resource "libvirt_domain" "domain" {
name = var.vm_hostname
memory = "8192"
vcpu = 4
autostart = true
cloudinit = libvirt_cloudinit_disk.commoninit.id
network_interface {
network_name = "default"
wait_for_lease = true
hostname = var.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.root.id
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
provisioner "remote-exec" {
inline = [
"echo 'ssh connection ready'"
]
connection {
type = "ssh"
user = var.ssh_username
host = libvirt_domain.domain.network_interface[0].addresses[0]
private_key = file(var.ssh_private_key)
timeout = "5m"
}
}
provisioner "file" {
source = "./config/ansible"
destination = "/home/${var.ssh_username}"
connection {
type = "ssh"
user = var.ssh_username
host = libvirt_domain.domain.network_interface[0].addresses[0]
private_key = file(var.ssh_private_key)
timeout = "1m"
}
}
provisioner "remote-exec" {
inline = [
"cloud-init status --wait",
"ansible-playbook ~/ansible/all.yml"
]
connection {
type = "ssh"
user = var.ssh_username
host = libvirt_domain.domain.network_interface[0].addresses[0]
private_key = file(var.ssh_private_key)
timeout = "30m"
}
}
}