Terraform Provisioner
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
Write a Terraform script which create a ubuntu ec2-instance and copy a ansible playbook, install ansible and run it.
Step 1 – Create ec2-instance with key and group
Step 2 – COpy playbook using file prov*
Step 3 – Install Ansible using remote prov*
Step 4 – RUn Ansinle playbook command using remote prv*
resource "aws_instance" "first-ec2" {
ami = "ami-03d5c68bab01f3496" # us-west-2
instance_type = "t2.micro"
key_name = "avinash-last"
tags = {
Name = "AvinashMallik"
}
connection {
type = "ssh"
user = "ubuntu"
private_key = file("avinash-last.pem")
#host = aws_instance.web.public_ip
host = self.public_ip
}
provisioner "local-exec" {
command = "touch devopsschool-local"
}
provisioner "file" {
source = "abc.yml"
destination = "/tmp/"
}
provisioner "remote-exec" {
inline = [
"sudo apt-get update",
"sudo apt install software-properties-common",
"sudo add-apt-repository --yes --update ppa:ansible/ansible",
"sudo apt install ansible -y",
"ansible-playbook abc.yml --check",
]
}
}