#!/bin/bash cd prd && ls -d */ declare -a dirs i=1 for d in */ do dirs[i++]="${d%/}" done echo "There are ${#dirs[@]} dirs in the current path" for((i=1;i<=${#dirs[@]};i++)) do cd "${dirs[i]}" && rm -rf .terraform \ && echo "terraform {" > backend.tf \ && echo "backend \"consul\" {" >> backend.tf \ && echo "}" >> backend.tf \ && echo "}" >> backend.tf \ && terraform get -update=true \ && terraform init -backend=true \ -backend-config "address=consul-ip" \ -backend-config "path=prd/${dirs[i]}" \ && terraform apply -auto-approve \ && rm backend.tf \ && cd .. done