-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
71 lines (52 loc) · 1.84 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 {
google = {
source = "hashicorp/google"
version = "3.90.0"
}
}
}
provider "google" {
credentials = file("keys/${var.service_account_file}")
project = var.project
}
# #########################################################################################################################################
# Resources
# #########################################################################################################################################
resource "google_compute_instance" "vm_instance" {
for_each = var.vm_nodes
project = var.project
name = each.value.vm_name
machine_type = var.machine_type
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
size = 10
type = "pd-standard"
}
}
network_interface {
network = "default"
access_config {
}
}
metadata = {
ssh-keys = "${var.ssh_user}:${file(var.ssh_pub_key)}"
}
tags = ["externalssh"]
provisioner "remote-exec" {
inline = ["sudo apt update", "sudo apt -y install python3-pip"]
connection {
type = "ssh"
port = 22
user = var.ssh_user
host = self.network_interface[0].access_config[0].nat_ip
private_key = file(var.ssh_prv_key)
timeout = "5m"
}
}
provisioner "local-exec" {
command = "ansible-playbook --extra-vars='{\"ssh_user\": ${var.ssh_user}, \"project\": ${var.project}, \"gcp_service_email\": ${var.gcp_service_email}, \"vm_name\": ${each.value.vm_name}, \"vm_no\": ${each.value.vm_no}, \"seed_file\": ${var.seed_file}, \"script\": ${var.script}, \"gcp_bucket\": ${var.gcp_bucket}, \"service_account_file\": ${var.service_account_file}}' -i '${self.network_interface[0].access_config[0].nat_ip},' playbook.yaml"
}
}