-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.tf
100 lines (80 loc) · 2.89 KB
/
compute.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
# Database serving VM provisioning
resource "openstack_compute_keypair_v2" "bastion_key" {
name = "bastion_key"
}
resource "local_file" "bastion-key" {
content = openstack_compute_keypair_v2.bastion_key.private_key
filename = "./bastion_key"
}
resource "openstack_compute_instance_v2" "bastion_vm" {
name = "bastion-vm"
image_name = var.vm_image
flavor_name = var.vm_flavor
security_groups = ["default"]
key_pair = var.ssh_public_key_name
network {
name = openstack_networking_network_v2.webapp_network.name
}
}
resource "null_resource" "bastion_config" {
depends_on = [
openstack_lb_member_v2.ssh_member,
openstack_compute_instance_v2.bastion_vm,
openstack_networking_floatingip_associate_v2.lb_fip,
openstack_networking_router_interface_v2.webapp_router_interface
]
provisioner "file" {
source = "./bastion_key"
destination = "/home/${var.fed_id}/.ssh/id_rsa"
connection {
type = "ssh"
user = "${var.fed_id}"
private_key = file("/home/${var.fed_id}/.ssh/id_rsa")
host = var.lb_float_ip
port = 2222
timeout = "60s"
}
}
provisioner "remote-exec" {
inline = [ "sudo chmod 600 /home/${var.fed_id}/.ssh/id_rsa" ]
connection {
type = "ssh"
user = "${var.fed_id}"
private_key = file("/home/${var.fed_id}/.ssh/id_rsa")
host = var.lb_float_ip
port = 2222
timeout = "60s"
}
}
provisioner "local-exec" {
command = "ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook -e 'ansible_port=2222 lb_fip=${var.lb_float_ip} fed_id=${var.fed_id}' -i ${var.lb_float_ip}, play.yaml"
}
}
resource "openstack_compute_instance_v2" "db_vm" {
name = "db-vm"
image_name = var.vm_image
flavor_name = var.vm_flavor
security_groups = ["default", openstack_networking_secgroup_v2.db_secgroup.name]
key_pair = openstack_compute_keypair_v2.bastion_key.name
network {
name = openstack_networking_network_v2.webapp_network.name
}
metadata = {
group = "db_vms"
}
}
# Web serving VM provisioning
resource "openstack_compute_instance_v2" "web_vm" {
count = var.no_web_vms
name = "web-vm-${count.index}"
image_name = var.vm_image
flavor_name = var.vm_flavor
security_groups = ["default", openstack_networking_secgroup_v2.http_secgroup.name]
key_pair = openstack_compute_keypair_v2.bastion_key.name
network {
name = openstack_networking_network_v2.webapp_network.name
}
metadata = {
group = "web_vms"
}
}