Start with Ansible#
In this lab we’ll execute our first Ansible playbook on the instance we just created with Terraform.
You should already have done the steps in the Start with Terraform Lab.
Step 1: Install Prerequisites#
Ansible relies on inventory files to know what hosts to manage. This is a bit awkward when you can create and destroy VMs so easily. Ansible has a plugin that automatically adds instances to your inventory. You can find instructions for installing the plugin on the Google Cloud Platform Guide. Using the Cloud Editor run this command to install necessary Python components:
$ pip3 install --upgrade pip $ pip3 install ansible requests google-auth
Step 2: Create an Inventory File#
Now put this in a file called
inventory.gcp.yaml the root of your
plugin: gcp_compute projects: - your-project-here auth_kind: serviceaccount service_account_file: /path/to/your/key/file keyed_groups: - prefix: gcp key: labels - prefix: tags key: tags
your-project-here to match the ID of your project. Also change
/path/to/your/key/file with the path to the key used by Terraform.
Step 3: Run the Playbook#
base directory run the command:
$ ansible-playbook -i ../inventory.gcp.yaml --private-key ~/.ssh/google_compute_engine playbook.yaml
Ansible will tell you if all of your plays were successfully applied.
A screenshot of the output of Ansible.
Don’t forget to run
terraform destroy when you’re done with your VM