Start with Ansible

In this lab we’ll execute our first Ansible playbook on the instance we just created with Terraform.

Important

You should already have done the steps in the 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 cis-91 repository:

plugin: gcp_compute
projects:
  - your-project-here
auth_kind: serviceaccount
service_account_file: ${HOME}/cis-91/secrets/cis-91.key
keyed_groups:
  - prefix: gcp
    key: labels

Note

Change your-project-here to match the ID of your project. Also change the key name if necessary.

Step 3: Run the Playbook

Inside the 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.

Turn In

A screenshot of the output of Ansible.

Important

Don’t forget to run terraform destroy when you’re done with your VM