-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb_init.sh
28 lines (21 loc) · 890 Bytes
/
mongodb_init.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
cd terraform/
# Step 1: Initialize Terraform
terraform init
# Step 2: Apply to create AWS resources (-auto-approve flag to save typing "yes")
terraform apply -auto-approve
# Extract the public IP addresses of MongoDB and the web app instances
# And create an Ansible inventory file
mongodb_ips=$(terraform output -json | jq -r '.mongo_public_ips.[]')
go_web_ip=$(terraform output -json | jq -r '.app_public_ip.value')
echo "[mongodb]" > ../ansible/inventory.ini
for ip in $mongodb_ips; do
echo "$ip ansible_ssh_user=ec2-user" >> ../ansible/inventory.ini
done
echo "[goweb]" >> ansible/inventory.ini
echo "$go_web_ip ansible_ssh_user=ec2-user" >> ../ansible/inventory.ini
# Step 3: Run Ansible playbook to configure MongoDB replica set
# and go web app instance
cd ../ansible/
ansible-playbook -i inventory.ini mongodb.yml
ansible-playbook -i inventory.ini go-app.yml