Provisioning using Terraform and ansible
Step1: Creating a terraform instance and installing terraform on that .
created bash file to install.
sudo su
apt-get update -y
#Now we will install terraform create the terra_install bash file and run it
vi terra_install.sh
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update -y
sudo apt-get install terraform -y
Step 2. writing Script to install ansible master and playbook running.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "us-east-1"
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
}
resource "aws_instance" "ansible" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = "pratice"
subnet_id = "subnet-02f1ba0ff720df4f0"
tags = {
Name = "ansible-instance"
}
user_data = <<-EOF
#!/bin/bash
apt-get update
apt-get install -y software-properties-common
apt-add-repository --yes --update ppa:ansible/ansible
apt-get install -y ansible
EOF
connection {
type = "ssh"
user = "ubuntu"
private_key = file("pratice.pem")
host = self.public_ip
}
provisioner "local-exec" {
command = "/usr/bin/ansible-playbook -i ${self.public_ip}, playbook.yml"
}
}
Step 3.to apply use the command
terraform apply
Step4. To destroy use the below command
terraform destroy