Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

# .tfvars files
*.tfvars

# gcloud configs
account.json
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions terraform_gcp/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
provider "google" {
credentials = "${file("account.json")}"
project = "${var.project_name}"
region = "us-central1"
}

resource "google_compute_instance" "tfansible" {
name = "terraform-ansible"
machine_type = "n1-standard-1"
zone = "us-central1-a"

tags = ["web"]

boot_disk {
initialize_params {
image = "rhel-cloud/rhel-7"
}
}

// Local SSD disk
scratch_disk {}

network_interface {
network = "default"

access_config {
// Ephemeral IP
}
}

metadata {
Name = "Terraform and Ansible Demo"
ssh-keys = "${var.ssh_user}:${file("${var.public_key_path}")}"
}

metadata_startup_script = "echo hi > /test.txt"

service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}

provisioner "remote-exec" {
inline = ["echo 'Hello World'"]

connection {
type = "ssh"
host = "${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip}"
user = "${var.ssh_user}"
private_key = "${file("${var.private_key_path}")}"
}
}

provisioner "local-exec" {
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"
}
}

resource "google_compute_firewall" "default" {
name = "web-firewall"
network = "default"

allow {
protocol = "icmp"
}

allow {
protocol = "tcp"
ports = ["80"]
}

source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
}
3 changes: 3 additions & 0 deletions terraform_gcp/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "Webapp URL" {
value = "http://${google_compute_instance.tfansible.network_interface.0.access_config.0.assigned_nat_ip}"
}
19 changes: 19 additions & 0 deletions terraform_gcp/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
variable "public_key_path" {
description = "Path to the public SSH key you want to bake into the instance."
default = "~/.ssh/id_dsa.pub"
}

variable "private_key_path" {
description = "Path to the private SSH key, used to access the instance."
default = "~/.ssh/id_dsa"
}

variable "project_name" {
description = "Name of your GCP project. Example: ansible-terraform-218216"
default = "ansible-terraform-218216"
}

variable "ssh_user" {
description = "SSH user name to connect to your instance."
default = "scarolan"
}