-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
71 lines (58 loc) · 1.7 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
terraform {
required_providers {
virtualbox = {
source = "shekeriev/virtualbox"
version = "0.0.4"
}
}
}
provider "virtualbox" {
delay = 60
mintimeout = 5
}
resource "virtualbox_vm" "lab-vm" {
count = length(var.hostname)
name = var.hostname[count.index]
image = var.vm_template
cpus = var.cpu
memory = var.memoryMB
# user_data will be deprecated soon, so disabled for this
# user_data = file("${path.module}/user_data")
network_adapter {
type = var.interfaceType
device = "IntelPro1000MTDesktop"
host_interface = var.hostInterface
# On Windows use this instead
# host_interface = "VirtualBox Host-Only Ethernet Adapter"
}
provisioner "remote-exec" {
inline = ["sudo apt update", "sudo apt install python3 -y", "sudo hostnamectl set-hostname ${self.name}"]
connection {
host = self.network_adapter.0.ipv4_address
type = "ssh"
user = "student"
password = "student"
# private_key = file(var.pvt_key)
}
}
}
# generate inventory file for Ansible
resource "local_file" "hosts_cfg" {
depends_on = [
virtualbox_vm.lab-vm
]
filename = "./ansible/inventory"
directory_permission = 0644
file_permission = 0755
content = templatefile("${path.module}/ansible/hosts.tpl",
{
vm_addresses = virtualbox_vm.lab-vm.*.network_adapter.0.ipv4_address
}
)
provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -i './ansible/inventory' --become-user root ./ansible/playbook.yml"
}
}
output "IPAddress" {
value = zipmap(virtualbox_vm.lab-vm.*.name, virtualbox_vm.lab-vm.*.network_adapter.0.ipv4_address)
}