diff options
4 files changed, 54 insertions, 20 deletions
diff --git a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/terraform.tfvars b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/terraform.tfvars new file mode 100644 index 0000000..66811b1 --- /dev/null +++ b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/terraform.tfvars @@ -0,0 +1,4 @@ +password = "" # password for Ansible SSH +host = "" # IP of my Ansible +access = "" # access_key for AWS +secret = "" # secret_key for AWS
\ No newline at end of file diff --git a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/var.tf b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/var.tf index 6f1e65d..948c4b1 100644 --- a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/var.tf +++ b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/var.tf @@ -5,6 +5,7 @@ variable "password" { } variable "host"{ description = "The IP of my Ansible" + type = string } variable "access" { description = "my access_key for AWS" diff --git a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/versions.tf b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/versions.tf new file mode 100644 index 0000000..d0753ff --- /dev/null +++ b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/versions.tf @@ -0,0 +1,8 @@ + terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +}
\ No newline at end of file diff --git a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/vyos.tf b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/vyos.tf index 8940ab5..20793f4 100644 --- a/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/vyos.tf +++ b/TerraformCloud/AWS_terraform_ansible_single_vyos_instance-main/Terraform/vyos.tf @@ -1,11 +1,15 @@ -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "~> 5.0" - } - } -} +############################################################################## +# Build an VyOS VM from the Marketplace +# To finde nessesery AMI image_ in AWS +# +# In the script vyos.tf we'll use default values (you can chang it as you need) +# AWS Region = "us-east-1" +# AMI = "standard AMI of VyOS from AWS Marketplace" +# Size of VM = "t2.micro" +# AWS Region = "us-east-1" +# After deploying the AWS instance and getting an IP address, the IP address is copied into the file +#"ip.txt" and copied to the Ansible node for provisioning. +############################################################################## provider "aws" { access_key = var.access @@ -19,7 +23,7 @@ variable "region" { } variable "ami" { - default = "ami-**************" # ami image please enter your details + default = "ami-**************3b3" # ami image please enter your details description = "Amazon Machine Image ID for VyOS" } @@ -32,20 +36,31 @@ variable "type" { resource "aws_instance" "myVyOSec2" { ami = var.ami - key_name = "mykeyname" # Please enter your details - security_groups = ["my_sg"] # Please enter your details + key_name = "awsterraform" # Please enter your details from 1.2 of Preparation steps for deploying VyOS on AWS + security_groups = ["awsterraformsg"] # Please enter your details from 1.3 of Preparation steps for deploying VyOS on AWS instance_type = var.type tags = { name = "VyOS System" } } +############################################################################## +# specific variable (to getting type "terraform plan"): +# aws_instance.myVyOSec2.public_ip - the information about public IP address +# of our instance, needs for provisioning and ssh connection from Ansible +############################################################################## + output "my_IP"{ value = aws_instance.myVyOSec2.public_ip } - -#IP of aws instance copied to a file ip.txt in local system Terraform +############################################################################## +# +# IP of aws instance copied to a file ip.txt in local system Terraform +# ip.txt looks like: +# cat ./ip.txt +# ххх.ххх.ххх.ххх +############################################################################## resource "local_file" "ip" { content = aws_instance.myVyOSec2.public_ip @@ -54,6 +69,10 @@ resource "local_file" "ip" { #connecting to the Ansible control node using SSH connection +############################################################################## +# Steps "SSHconnection1" and "SSHconnection2" need to get file ip.txt from the terraform node and start remotely the playbook of Ansible. +############################################################################## + resource "null_resource" "SSHconnection1" { depends_on = [aws_instance.myVyOSec2] connection { @@ -62,7 +81,9 @@ connection { password = var.password host = var.host } + #copying the ip.txt file to the Ansible control node from local system + provisioner "file" { source = "ip.txt" destination = "/root/aws/ip.txt" # The folder of your Ansible project @@ -72,16 +93,16 @@ connection { resource "null_resource" "SSHconnection2" { depends_on = [aws_instance.myVyOSec2] connection { - type = "ssh" - user = "root" - password = var.password - host = var.host + type = "ssh" + user = "root" + password = var.password + host = var.host } #command to run Ansible playbook on remote Linux OS provisioner "remote-exec" { inline = [ - "cd /root/aws/", - "ansible-playbook instance.yml" + "cd /root/aws/", + "ansible-playbook instance.yml" # more detailed in "File contents of Ansible for AWS" ] } -} +}
\ No newline at end of file |