Terraform and Ansible Practice Outputs
Terraform and Ansible Practice Outputs
terraform {
required_version = ">=0.12"
backend "s3" {
bucket = "OneClick-DevOps"
key = "terraform/${var.env_prefix}-state.tfstate"
region = "us-east-1"
}
}
provider "aws" {
region = "us-east-1"
}
connection {
type = "ssh"
host = self.public_ip
user = "ec2-user"
private_key = file(var.private_key_location)
}
provisioner "file" {
source = "./entry-script.sh"
destination = "/home/ec2-user/entry-script-on-ec2.sh"
}
provisioner "remote-exec" {
script = file("/home/ec2-user/entry-script-on-ec2.sh")
}
provisioner "local-exec" {
command = "echo ${self.public_ip} > output.txt"
}
}
output "public-ips-1" {
value = aws_instance.TFC-instances[*].public_ip
}
output "public-ips-2" {
value = {
for instance in aws_instance.TFC-instances:
instance.tags.name => instance.public_ip
}
}
===entry-script.sh===
!#/bin/bash
sudo yum upgrade -y && sudo yum install -y docker
sudo usermod -aG docker ec2-user
sudo systemctl start docker
docker run -p 8080 nginx
handlers:
- name: Reload Apache
service:
name: httpd
state: reloaded
command:
- hosts: localhost
vars:
tools:
- ansible
- puppet
- chef
tasks:
- name: Display using arrays
debug:
msg: "{{ tools[0] }} is the agent less model tool for
configuration management."
command: ansible-playbook -i hosts.ini playbook.yaml
- hosts: web
become: true
remote_user: root
vars:
packages:
- httpd
- docker
- mysql
tasks:
- name: Install packages by parsing through the array list
yum:
name: {{ item }}
state: installed
loop: {{ packages }}
- name: Removing through the list of items
yum:
name:
- httpd
- docker
- mysql
state: removed