This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
58 lines (50 loc) · 1.48 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
resource "openstack_networking_floatingip_v2" "vm_fip" {
count = var.vm_count
pool = var.pool
}
resource "openstack_compute_instance_v2" "vm" {
count = var.vm_count
name = format("${var.instance_prefix}-%02d", count.index+1)
image_name = var.image
flavor_name = var.flavor
key_pair = var.ssh_key_pair
security_groups = [
var.security_group]
network {
name = var.network_name
}
}
resource "openstack_compute_floatingip_associate_v2" "vm_fip_assign" {
count = var.vm_count
instance_id = openstack_compute_instance_v2.vm.*.id[count.index]
floating_ip = openstack_networking_floatingip_v2.vm_fip.*.address[count.index]
}
// workaround: we can't provision file and remote-exec before VMs have floating IP assigned
// so we create dummy step to execute script AFTER floating IPs are assigned
resource "null_resource" "vm_init" {
count = var.vm_count
depends_on = [
openstack_compute_floatingip_associate_v2.vm_fip_assign
]
connection {
type = "ssh"
user = var.ssh_user_name
private_key = file(var.ssh_key_path)
host = openstack_networking_floatingip_v2.vm_fip.*.address[count.index]
timeout = "1m"
agent = false
}
provisioner "file" {
source = var.vm_init_script
destination = "/tmp/wait-for.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/wait-for.sh",
"/tmp/wait-for.sh localhost:22 -t 60"
]
}
}
output "vm_ips" {
value = openstack_compute_floatingip_associate_v2.vm_fip_assign.*.floating_ip
}