diff --git a/Jenkins-file b/Jenkins-file new file mode 100644 index 0000000..1f1fb0e --- /dev/null +++ b/Jenkins-file @@ -0,0 +1,87 @@ +pipeline { + agent any + tools { + terraform 'terraform-Rag' + } + parameters { + //string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') + //string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') + //choice choices: ['AWS', 'AZURE', 'OracleCloud'], description: '''Select the cloud platform + //''', name: 'Cloud' + choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region + ''', name: 'Region' + choice choices: ['Ragesh', 'Moiz'], description: '''Server owner + ''', name: 'Owner' + choice choices: ['DEV-GROUP', 'IT-GROUP'], description: '''Server Group + ''', name: 'Group' + string defaultValue: '', description: 'Project', name: 'Project', trim: false + choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. + For more information + https://aws.amazon.com/ec2/instance-types/ + ''', name: 'Configuration' + string defaultValue: '', description: 'Input server Name', name: 'ServerName', trim: false + + booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') + } + + environment { + AWS_ACCESS_KEY_ID = credentials('AWS-ACCESS-KEY-ID') + AWS_SECRET_ACCESS_KEY = credentials('AWS-SECRET-KEY') + TF_IN_AUTOMATION = '1' + } + + stages { + stage('Git Checkout'){ + steps{ + git credentialsId: 'GITHUB-RAGEH', url: 'https://github.com/Rageshmookoth/iac-demo' + } + } + stage('Plan') { + steps { + //script { + // currentBuild.displayName = params.version + //} + sh 'terraform init -input=false' + //sh 'terraform workspace select ${environment}' + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}' -var 'Group=${params.Group}' -var 'Owner=${params.Owner}' -var 'Project=${params.Project}'" + sh 'terraform show -no-color tfplan > tfplan.txt' + } + } + + stage('Approval') { + when { + not { + equals expected: true, actual: params.autoApprove + } + } + + steps { + script { + def plan = readFile 'tfplan.txt' + input message: "Do you want to apply the plan?", + parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] + } + } + } + + stage('Apply') { + steps { + sh "terraform apply -input=false tfplan" + } + } + stage('Remove State file') { + steps { + sh "rm -fr terraform.tfstate" + } + } + + + } + post { + success { + mail to: 'rageshmoorkoth@gmail.com', + subject: "Server build completed", + body: "Server name ${params.ServerName}" + } +} +} \ No newline at end of file diff --git a/inventory.template b/inventory.template new file mode 100644 index 0000000..f03f2b9 --- /dev/null +++ b/inventory.template @@ -0,0 +1 @@ +${web_public_ip} \ No newline at end of file diff --git a/jenkins-configuration b/jenkins-configuration new file mode 100644 index 0000000..1abebea --- /dev/null +++ b/jenkins-configuration @@ -0,0 +1,91 @@ +pipeline { + agent any + tools { + terraform 'terraform-Rag' + } + parameters { + //string(name: 'environment', defaultValue: 'default', description: 'Workspace/environment file to use for deployment') + //string(name: 'version', defaultValue: '', description: 'Version variable to pass to Terraform') + //choice choices: ['Oracle Linux', 'Microsoft Windows'], description: '''Select the Operating System + //''', name: 'OS' + choice choices: ['us-west-1', 'ap-south-1', 'eu-central-1'], description: '''Select the region + ''', name: 'Region' + choice choices: ['Ragesh', 'Moiz','Sourabh'], description: '''Server owner + ''', name: 'Owner' + choice choices: ['t2.micro', 't2.small', 't2.medium'], description: '''Select the appropriate instance type. + For more information + https://aws.amazon.com/ec2/instance-types/ + ''', name: 'Configuration' + string defaultValue: '', description: 'Input server Name', name: 'ServerName', trim: false + + booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') + } + + environment { + AWS_ACCESS_KEY_ID = credentials('AWS-ACCESS-KEY-ID') + AWS_SECRET_ACCESS_KEY = credentials('AWS-SECRET-KEY') + TF_IN_AUTOMATION = '1' + } + stages { + stage('Git Checkout'){ + steps{ + git credentialsId: 'GITHUB-RAGEH', url: 'https://github.com/Rageshmookoth/iac-demo' + } + } + stage('OS Image select'){ + + steps{ + + } + + } + + stage('Plan') { + steps { + //script { + // currentBuild.displayName = params.version + //} + sh 'terraform init -input=false' + //sh 'terraform workspace select ${environment}' + sh "terraform plan -input=false -out tfplan -var 'region=${params.Region}' -var 'instance_type=${params.Configuration}' -var 'instance_name=${params.ServerName}'" + sh 'terraform show -no-color tfplan > tfplan.txt' + } + } + + stage('Approval') { + when { + not { + equals expected: true, actual: params.autoApprove + } + } + + steps { + script { + def plan = readFile 'tfplan.txt' + input message: "Do you want to apply the plan?", + parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] + } + } + } + + stage('Apply') { + steps { + sh "terraform apply -input=false tfplan" + } + } + stage('Remove State file') { + steps { + sh "rm -fr terraform.tfstate" + } + } + + + } + post { + success { + mail to: 'rageshmoorkoth@gmail.com', + subject: "Server build completed", + body: "Server name ${params.ServerName}" + } +} +} \ No newline at end of file diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..c328e8c --- /dev/null +++ b/output.tf @@ -0,0 +1,11 @@ +output "ip" { + value = "${aws_instance.Ragesh.public_ip}" +} +resource "local_file" "ansible_inventory_hosts" { + content = templatefile("inventory.template", + { + web_public_ip = aws_instance.Ragesh.public_ip, + } + ) + filename = "inventory" +} diff --git a/playbook.yml b/playbook.yml new file mode 100644 index 0000000..ffe14d2 --- /dev/null +++ b/playbook.yml @@ -0,0 +1,13 @@ +--- +- hosts: all + become: yes + become_user: root + become_method: sudo + tasks: + - name: Install httpd + yum: + name: httpd + state: latest + - name: Restart httpd + service: name=httpd state=restarted + become: yes diff --git a/provider.tf b/provider.tf index 88f0a18..165800f 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,3 @@ provider "aws" { - region = "ap-south-1" -} \ No newline at end of file +region = var.region +} diff --git a/resource.tf b/resource.tf new file mode 100644 index 0000000..b014aeb --- /dev/null +++ b/resource.tf @@ -0,0 +1,42 @@ +resource "aws_instance" "Ragesh" { +ami = var.ami +instance_type = var.instance_type +key_name = "POC-STD-KEY-PAIR" +tags = { + Name = var.instance_name + Owner = var.Owner + Group = var.Group + Project = var.Project + } +} +#resource "null_resource" "step1" { +#provisioner "local-exec" { +# command = "echo ${aws_instance.Ragesh.public_ip}" +# } +#} +resource "null_resource" "wait" { +depends_on = [ + aws_instance.Ragesh, +# null_resource.step1, +# null_resource.wait, + ] + provisioner "local-exec" { + command = "sleep 180" + } +} +resource "null_resource" "Ansible-play" { +depends_on = [ + null_resource.wait, + ] + # using ansible, declarative approach of configuration management + provisioner "local-exec" { + command ="ansible-playbook -i inventory playbook.yml --private-key=${var.private_key} --user ${var.ansible_user}" + } +} + + + + + +### https://a4ank.medium.com/aws-terraform-ansible-end-to-end-automation-9072f2ecf624 +####https://medium.com/on-the-cloud/one-click-environment-creation-with-terraform-ansible-in-under-10-6e8d9284f60 \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..5131ea8 --- /dev/null +++ b/variables.tf @@ -0,0 +1,52 @@ +#------------------------------------------------------------------------------ +# Misc +#------------------------------------------------------------------------------ +variable "instance_name" { +description = "Name prefix for resources on AWS" +type = string +default = "test-instance" +} + +#------------------------------------------------------------------------------ +# AWS CREDENTIALS AND REGION +#------------------------------------------------------------------------------ +variable "instance_type" { +description = "AWS EC2 instance type" +default = "t2.micro" +} +variable "region" { +description = "AWS Region the infrastructure is hosted in" +default = "us-east-1" +} +variable "ami" { +description = "AMI Image" +default = "ami-045e6fa7127ab1ac4" +} +variable "cloudprovider" { +description = "Select the Cloud Provider" +default = "AWS" +} +variable "size" { +description = "Provide size" +default = "100" +} +variable "Owner" { +description = "Owner name" +default = "Ragesh" +} +variable "private_key" { + default = "POC-STD-KEY-PAIR.pem" +} +variable "ansible_user" { + default = "ec2-user" +} +variable "Group" { +description = "Group Tag" +type = string +default = "No-Group" +} +variable "Project" { +description = "Project name Tag" +type = string +default = "No-Project" +} \ No newline at end of file diff --git a/vpc.tf b/vpc.tf deleted file mode 100644 index 175c603..0000000 --- a/vpc.tf +++ /dev/null @@ -1,9 +0,0 @@ -resource "aws_vpc" "main" { - cidr_block = "10.0.0.0/16" - instance_tenancy = "default" - - tags = { - Name = "demo-vpc" - Purpose = "Jenkins Demo" - } -}