Skip to content
Open
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
9 changes: 8 additions & 1 deletion Terraform_project/Ansible_Master.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ sudo -u admns bash -c "ssh-keygen -f ~admns/.ssh/id_rsa -N ''"
cd /home/admns/.ssh



sshpass -p "$account_password" ssh-copy-id -o StrictHostKeyChecking=no ${eip1} && sshpass -p "$account_password" ssh-copy-id -o StrictHostKeyChecking=no ${eip2}
# eipslist1="${join(var.special, google_compute_instance.node.*.network_interface.0.access_config.0.nat_ip)}"
# read -a eips <<<"$eipslist1"
# IFS='-' #setting - as delimiter
# for eip in "${eips[@]}"; #accessing each element of array
# do
# echo $eip
# sshpass -p "$account_password" ssh-copy-id -o StrictHostKeyChecking=no ${eip}
# done
# Login to Admns and try ansible all -m ping
94 changes: 42 additions & 52 deletions Terraform_project/main.tf
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
data "template_file" "script_file" {
template = file("Ansible_Master.sh")
vars = {
eip1 = google_compute_instance.node1.network_interface.0.access_config.0.nat_ip
eip2 = google_compute_instance.node2.network_interface.0.access_config.0.nat_ip
}
}
# data "template_file" "script_file" {
# template = file("Ansible_Master.sh")
# vars = {
# eip1 = google_compute_instance.node1.network_interface.0.access_config.0.nat_ip
# eip2 = google_compute_instance.node2.network_interface.0.access_config.0.nat_ip
# }
# }


resource "google_compute_instance" "master" {
name = "ansible-master"
name = "ansible-master"
machine_type = var.master_instance
tags = [
"ansible",
"master"]
"master"]
boot_disk {
initialize_params {
image = var.image
}
}


network_interface {
network = "default"
access_config {
}
}
metadata_startup_script = data.template_file.script_file.rendered



# metadata_startup_script = data.template_file.script_file.rendered
depends_on = [
google_compute_instance.node1,
google_compute_instance.node2,
google_compute_instance.node
]

# Add ssh key
metadata = {
ssh-keys = "${var.gce_ssh_user}:${file(var.gce_ssh_pub_key_file)}"
}
//metadata_startup_script = file("${path.module}/Ansible_Master.sh")
}

resource "google_compute_instance" "node1" {
name = "ansible-node1"
resource "google_compute_instance" "node" {
count = var.number_of_nodes
name = "ansible-node${count.index}"
machine_type = var.node_instance
tags = [
"ansible",
"node-1"]
"node${count.index}",
"node"]
boot_disk {
initialize_params {
image = var.image
Expand All @@ -48,44 +58,24 @@ resource "google_compute_instance" "node1" {
}
}
metadata_startup_script = file("${path.module}/Ansible_node.sh")
}

resource "google_compute_instance" "node2" {
name = "ansible-node2"
machine_type = var.node_instance
tags = [
"ansible",
"node-2"]
boot_disk {
initialize_params {
image = var.image
}
}
network_interface {
network = "default"
access_config {
}
metadata = {
ssh-keys = "${var.gce_ssh_user}:${file(var.gce_ssh_pub_key_file)}"
}
metadata_startup_script = file("${path.module}/Ansible_node.sh")
}

resource "google_compute_firewall" "default" {
name = "firewall-ssh"
network = google_compute_network.ssh_network.name

/*resource "google_compute_instance" "node" {
count = var.number_of_nodes
name = "ansible-node${count.index}"
machine_type = var.node_instance
tags = [
"ansible",
"node${count.index}"]
boot_disk {
initialize_params {
image = var.image
}
allow {
protocol = "tcp"
ports = ["22"]
}
network_interface {
network = "default"
access_config {
}
}
metadata_startup_script = file("${path.module}/Ansible_node.sh")
}*/
source_tags = ["node", "master"]
source_ranges = ["0.0.0.0/0"]

}

resource "google_compute_network" "ssh_network" {
name = "ssh-network"
}
17 changes: 9 additions & 8 deletions Terraform_project/output.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
output "Ansible_master" {
value = join(" ", google_compute_instance.master.*.network_interface.0.access_config.0.nat_ip)
value = join(" ", google_compute_instance.master.*.network_interface.0.access_config.0.nat_ip)
description = "Master, ext ip"
}

output "Ansible_node1" {
value = join(" ", google_compute_instance.node1.*.network_interface.0.access_config.0.nat_ip)
description = "Node 1, ext ip"
# Output will be printed for a batch of node instances
output "Ansible_node" {
value = join(", ", google_compute_instance.node.*.network_interface.0.access_config.0.nat_ip)
description = "Nodes , ext ip"
}

output "Ansible_node2" {
value = join(" ", google_compute_instance.node2.*.network_interface.0.access_config.0.nat_ip)
description = "Node 2, ext ip"
}
# output "Ansible_node2" {
# value = join(" ", google_compute_instance.node[1].*.network_interface.0.access_config.0.nat_ip)
# description = "Node 2, ext ip"
# }
31 changes: 21 additions & 10 deletions Terraform_project/variables.tf
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
/*variable "instance_type" {
type=string
}Ask for user input (see if you want to play with it)

# variable "instance_type" {
# type = string
# }
# Ask for user input (see if you want to play with it)

variable "number_of_nodes" {
type = number
type = number
default = 2
}
*/

variable "master_instance" {
type=string
type = string
default = "e2-medium"
}

variable "node_instance" {
type=string
type = string
default = "e2-micro"
}

variable "image" {
type=string
type = string
default = "ubuntu-os-pro-cloud/ubuntu-pro-2004-lts"
}

# Location of the public key file
variable "gce_ssh_pub_key_file" {
type = string
default = "../test-ssh.pub"
}


# Username for the intsance user to add ssh key.
variable "gce_ssh_user" {
type = string
default = "ubuntu"
}
/* You can use this as variable in Provider or resource terraform file.
variable "Zone" {
type = string
Expand Down