You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.6 KiB
59 lines
1.6 KiB
#!/bin/bash |
|
|
|
set -e |
|
|
|
# Remove backend.tf temporarily to avoid remote backend init |
|
if [ -f backend.tf ]; then |
|
echo "Temporarily renaming backend.tf to backend.tf.bak..." |
|
mv backend.tf backend.tf.bak |
|
fi |
|
|
|
echo "[1/7] Initializing Terraform locally..." |
|
terraform init |
|
|
|
echo "[2/7] Applying infrastructure with local state..." |
|
terraform apply -auto-approve |
|
|
|
# Now the infra should be created, including storage account + blob container |
|
|
|
# Write backend.tf after apply |
|
echo "[3/7] Writing backend.tf for remote state..." |
|
cat > backend.tf <<EOF |
|
terraform { |
|
backend "azurerm" { |
|
resource_group_name = "muscogee-rg" |
|
storage_account_name = "povertypoint" |
|
container_name = "tfstate" |
|
key = "terraform.tfstate" |
|
} |
|
} |
|
EOF |
|
|
|
echo "[4/7] Re-initializing Terraform with remote backend..." |
|
terraform init -force-copy |
|
|
|
# Now that state is copied, we can safely remove local state files |
|
rm -rf .terraform terraform.tfstate* |
|
# Optional: Remove backup if it existed |
|
if [ -f backend.tf.bak ]; then |
|
rm backend.tf.bak |
|
fi |
|
|
|
echo "[5/7] Fetching public IPs for Ansible inventory..." |
|
IPS=$(az vm list-ip-addresses --resource-group muscogee-rg --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" -o tsv) |
|
|
|
echo "[6/7] Creating Ansible inventory..." |
|
mkdir -p ansible |
|
> ansible/inventory.ini |
|
echo "[all]" >> ansible/inventory.ini |
|
i=1 |
|
for ip in $IPS; do |
|
echo "vm$i ansible_host=$ip ansible_user=lenape" >> ansible/inventory.ini |
|
((i++)) |
|
done |
|
|
|
echo "[7/7] Running Ansible playbook to install NGINX..." |
|
ANSIBLE_CONFIG=ansible/ansible.cfg ansible-playbook ansible/playbook.yml |
|
|
|
echo "[✓]Teleflex Azure Infrastructure Fully Deployed!" |
|
|
|
|