Skip to content

Commit c20362a

Browse files
committed
Moved demo code to GCP.
1 parent 2b49e15 commit c20362a

File tree

7 files changed

+98
-0
lines changed

7 files changed

+98
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77

88
# .tfvars files
99
*.tfvars
10+
11+
# gcloud configs
12+
account.json
File renamed without changes.
File renamed without changes.
File renamed without changes.

terraform_gcp/main.tf

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
provider "google" {
2+
credentials = "${file("account.json")}"
3+
project = "${var.project_name}"
4+
region = "us-central1"
5+
}
6+
7+
resource "google_compute_instance" "tfansible" {
8+
name = "terraform-ansible"
9+
machine_type = "n1-standard-1"
10+
zone = "us-central1-a"
11+
12+
tags = ["web"]
13+
14+
boot_disk {
15+
initialize_params {
16+
image = "rhel-cloud/rhel-7"
17+
}
18+
}
19+
20+
// Local SSD disk
21+
scratch_disk {}
22+
23+
network_interface {
24+
network = "default"
25+
26+
access_config {
27+
// Ephemeral IP
28+
}
29+
}
30+
31+
metadata {
32+
Name = "Terraform and Ansible Demo"
33+
ssh-keys = "${var.ssh_user}:${file("${var.public_key_path}")}"
34+
}
35+
36+
metadata_startup_script = "echo hi > /test.txt"
37+
38+
service_account {
39+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
40+
}
41+
42+
provisioner "remote-exec" {
43+
inline = ["echo 'Hello World'"]
44+
45+
connection {
46+
type = "ssh"
47+
host = "${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip}"
48+
user = "${var.ssh_user}"
49+
private_key = "${file("${var.private_key_path}")}"
50+
}
51+
}
52+
53+
provisioner "local-exec" {
54+
command = "ansible-playbook -i '${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip},' --private-key ${var.private_key_path} ../ansible/httpd.yml"
55+
}
56+
}
57+
58+
resource "google_compute_firewall" "default" {
59+
name = "web-firewall"
60+
network = "default"
61+
62+
allow {
63+
protocol = "icmp"
64+
}
65+
66+
allow {
67+
protocol = "tcp"
68+
ports = ["80"]
69+
}
70+
71+
source_ranges = ["0.0.0.0/0"]
72+
target_tags = ["web"]
73+
}

terraform_gcp/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "Webapp URL" {
2+
value = "http://${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip}"
3+
}

terraform_gcp/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
variable "public_key_path" {
2+
description = "Path to the public SSH key you want to bake into the instance."
3+
default = "~/.ssh/id_dsa.pub"
4+
}
5+
6+
variable "private_key_path" {
7+
description = "Path to the private SSH key, used to access the instance."
8+
default = "~/.ssh/id_dsa"
9+
}
10+
11+
variable "project_name" {
12+
description = "Name of your GCP project. Example: ansible-terraform-218216"
13+
default = "ansible-terraform-218216"
14+
}
15+
16+
variable "ssh_user" {
17+
description = "SSH user name to connect to your instance."
18+
default = "scarolan"
19+
}

0 commit comments

Comments
 (0)