<repo_name>ibbocus/Terraform_AWS<file_sep>/playbooks/install_ansible.sh
#!/bin/bash

sudo apt update -y
sudo apt install software-properties-common -y
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
sudo apt install python -y
sudo apt install python-pip -y
sudo pip install --upgrade pip -y
sudo pip install boto


cd /etc/ansible
echo "[web]
192.168.3.11 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=<PASSWORD>" >> hosts
echo  "[db]
172.16.17.32 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=<PASSWORD>" >> hosts
echo  "[aws]
192.168.33.12 ansible_connection=ssh ansible_ssh_user=vagrant ansible_ssh_pass=<PASSWORD>" >> hosts

<file_sep>/terraform/install_terraform.sh
#!/bin/bash

sudo wget -q -O - https://tjend.github.io/repo_terraform/repo_terraform.key | sudo apt-key add -
sudo echo 'deb [arch=amd64] https://tjend.github.io/repo_terraform stable main' >> /etc/apt/sources.list.d/terraform.list
sudo apt-get update
sudo apt-get install terraform

cd terraform/
terraform init
<file_sep>/run_vagrant.sh
#!/bin/bash


vagrant up

<file_sep>/terraform/run_terraform.sh
#!/bin/bash


cd terraform

terraform plan
terraform apply -auto-approve

echo "export App_ip=$(terraform output app_ip)" >> ./.bashrc
echo "export DB_ip=$(terraform output db_ip)" >> ./.bashrc
echo "export Bastion_ip=$(terraform output bastion_ip)" >>./.bashrc

source ~/.bashrc
<file_sep>/Vagrantfile
# Install required plugins
required_plugins = ["vagrant-hostsupdater", "vagrant-berkshelf"]
required_plugins.each do |plugin|
  unless Vagrant.has_plugin?(plugin)
    # User vagrant plugin manager to install plugin, which will automatically refresh plugin list afterwards
    puts "Installing vagrant plugin #{plugin}"
    Vagrant::Plugin::Manager.instance.install_plugin plugin
    puts "Installed vagrant plugin #{plugin}"
  end
end



def set_env vars
  command = <<~HEREDOC
      echo "Setting Environment Variables"
      source ~/.bashrc
  HEREDOC

  vars.each do |key, value|
    command += <<~HEREDOC
      if [ -z "$#{key}" ]; then
          echo "export #{key}=#{value}" >> ~/.bashrc
      fi
    HEREDOC
  end

  return command
end

Vagrant.configure("2") do |config|
  config.vm.define "aws" do |aws|
    aws.vm.box = "ubuntu/xenial64"
    aws.vm.network "private_network", ip: "192.168.33.12"
    aws.hostsupdater.aliases = ["development.local"]
    aws.vm.synced_folder "app/", "/home/vagrant/app"
    aws.vm.synced_folder "playbooks/", "/home/vagrant/playbooks"
    aws.vm.synced_folder "terraform/", "/home/vagrant/terraform"
    aws.vm.synced_folder "key/", "/home/vagrant/key/"
    aws.vm.provision "shell", path: "terraform/run_terraform.sh", privileged: false
    aws.vm.provision "shell", inline: set_env({ AWS_ACCESS_KEY: $AWS_ACCESS_KEY_ID }), privileged: false
    aws.vm.provision "shell", inline: set_env({ AWS_SECRET_KEY: $AWS_SECRET_KEY }), privileged: false

  end

end
