-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy path2-example.tf
85 lines (70 loc) · 1.86 KB
/
2-example.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
locals {
project_id = "devopsbyexample-325402"
network = "default"
image = "ubuntu-2004-focal-v20211212"
ssh_user = "ansible"
private_key_path = "~/.ssh/ansbile_ed25519"
web_servers = {
nginx-000-staging = {
machine_type = "e2-micro"
zone = "us-central1-a"
}
nginx-001-staging = {
machine_type = "e2-micro"
zone = "us-central1-b"
}
}
}
provider "google" {
project = local.project_id
region = "us-central1"
}
resource "google_service_account" "nginx" {
account_id = "nginx-demo"
}
resource "google_compute_firewall" "web" {
name = "web-access"
network = local.network
allow {
protocol = "tcp"
ports = ["80"]
}
source_ranges = ["0.0.0.0/0"]
target_service_accounts = [google_service_account.nginx.email]
}
resource "google_compute_instance" "nginx" {
for_each = local.web_servers
name = each.key
machine_type = each.value.machine_type
zone = each.value.zone
boot_disk {
initialize_params {
image = local.image
}
}
network_interface {
network = local.network
access_config {}
}
service_account {
email = google_service_account.nginx.email
scopes = ["cloud-platform"]
}
provisioner "remote-exec" {
inline = ["echo 'Wait until SSH is ready'"]
connection {
type = "ssh"
user = local.ssh_user
private_key = file(local.private_key_path)
host = self.network_interface.0.access_config.0.nat_ip
}
}
provisioner "local-exec" {
command = "ansible-playbook -i ${self.network_interface.0.access_config.0.nat_ip}, --private-key ${local.private_key_path} nginx.yaml"
}
}
output "nginx_ips" {
value = {
for k, v in google_compute_instance.nginx : k => "http://${v.network_interface.0.access_config.0.nat_ip}"
}
}