This project is used to set up a simple infrastructure configuration with Google cloud Terraform provider and Ansible
As GCP service accounts cannot be used to create a GCP project without parents (single projects that are not attached to any organization), Terraform admin project will contain only a GCS bucket to store Terraform state.
-
Set up the environment
export TF_VAR_billing_account=YOUR_BILLING_ACCOUNT_ID export TF_ADMIN=${USER}-terraform-adminNoteTo see available billing accounts use gcloud beta billing accounts listcommand -
Authenticate with your personal account
gcloud auth application-default login
-
Set default compute zone (Frankfurt in this case)
gcloud config set compute/zone europe-west3
-
Create the Terraform Admin Project
gcloud projects create ${TF_ADMIN} --set-as-default gcloud beta billing projects link ${TF_ADMIN} --billing-account ${TF_VAR_billing_account} -
Set up remote state in Cloud Storage
gsutil mb -p ${TF_ADMIN} -l EU gs://${TF_ADMIN} gsutil versioning set on gs://${TF_ADMIN} -
Configure your environment for the Google Cloud Terraform provider
export GOOGLE_PROJECT=${TF_ADMIN}
-
Configure ssh keys: place
id_rsa.pubtoterraform/filesdirectory and specify username environment variableexport TF_VAR_ssh_username=<username>
-
Initialize the backend:
terraform init -
Preview the Terraform changes:
terraform plan -
Apply the Terraform changes:
terraform apply
Terraform creates inventory hosts file with gcp-website hosts group configured
-
Ping target VMs
export ANSIBLE_HOST_KEY_CHECKING=False ansible -i ansible/hosts -m ping gcp-website
-
Print kernel version
ansible -i ansible/hosts -m command -a 'uname -r' gcp-website
-
Execute playbook
ansible-playbook -i hosts apache.yaml