This is the supporting documentation for Using Ansible with Google video.
The goal of this repository is to provide the extra detail necessary for you to completely replicate the recorded demo. The video's main goal is to show a quick, fully working demo without bogging you down with all of the required details so you can easily see the "Good Stuff".
So for interested viewers wanting to replicate the demo on their own, this repository contains all those necessary details.
-
You will need to create a Google Cloud Platform Project as a first step. Make sure you are logged in to your Google Account (gmail, Google+, etc) and point your browser to https://console.cloud.google.com/projectselector/compute/instances. You should see a page asking you to create your first Project.
-
When creating a Project, you will see a pop-up dialog box. You can specify custom names but the Project ID is globally unique across all Google Cloud Platform customers.
-
It's OK to create a Project first, but you will need to set up billing before you can create any virtual machines with Compute Engine. Find the menu icon at the top left, then look for the Billing link in the navigation bar.
-
In order for
ansible
to create Compute Engine instances, you'll need a Service Account. It's recommended that you create a new Service Account (don't use the default), called 'demo-ansible', for this demo. Make sure to create a new JSON formatted private key file for this Service Account. Also, note the Email address of this Service Account (should bedemo-ansible@YOUR_PROJECT_ID.iam.gserviceaccount.com
) since this will be required in the Ansible configuration files. -
Next you will want to install the Cloud SDK and make sure you've successfully authenticated and set your default project as instructed.
-
You will also need to make sure and set up SSH keys that will allow you to access your Compute Engine instances. You can either manually generate the keys and paste the public key into the metadata server or you can use
gcloud compute ssh
to access an existing Compute Engine instance and it will handle generating the keys and uploading them to the metadata server. For this demo, it is assumed you have opted to usegcloud compute ssh
and your private key is located at$HOME/.ssh/google_compute_engine
.
-
Install Dependencies. On Debian-7, you may run the following to install them.
sudo apt-get install -y build-essential git python-dev python-pip
-
Install Ansible with the running from source instructions.
-
Install GCP module dependencies
pip install googleauth requests
-
For the purposes of the demo, you can set a couple of environment variables to simplify your commands and SSH interactions.
export ANSIBLE_HOST_KEY_CHECKING=False
-
Check out this repository so that you can use pre-canned configuration and demo files.
cd $HOME git clone https://github.com/GoogleCloudPlatform/compute-video-demo-ansible
-
Edit the
gce_vars/auth
file and specify your Project ID in theproject_id
variable, Service Account email address in theservice_account_email
variable, and the location of your JSON key (downloaded earlier) in thecredentials_file
variable.--- # Google Compute Engine required authentication global variables # (Replace 'YOUR_PROJECT_ID' with the Project ID used in creating your GCP project.) project: YOUR_PROJECT_ID service_account_file: /path/to/your/json_key_file
You've now completed all of the necessary setup to replicate the demo as
shown on the video. Now, you'll use ansible-playbook
to create and bootstrap
the instances, install Apache, and set up a Compute Engine load-balancer.
Use the ansible-playbook
command to create the instances based on the
attributes in the configuration files. For the four instances, this
should take roughly 2 minutes to create the new Compute Engine
instances
ansible-playbook -i ansible_hosts site.yml
-
The output from this command will display the public IP address associated with your new load-balancer. You can also look in the Developers Console under Networking > Load balancing and find the Forwarding Rules under the "Advanced" menu.
-
Ok, let's test it out! Put the public IP address of your load-balancer into your browser and take a look at the result. Within a few seconds you should start to see a flicker of pages that will randomly bounce across each of your instances.
For the demo, a javascript function is set to fire when the page loads that pauses for a half-second, and then reloads itself. Since we installed a modified Apache configuration file to disable client-side caching and we enabled Apache's
mod_headers
, each "reload" results in a new HTTP request to the page. This is just a fancy hands-free way of asking you to do a "hard refresh" of the load-balancer IP address in order to see the cycling between instances.
That's it for the demo. There is a lot of other functionality for
Compute Engine in Ansible. Please take a look at the gce*
modules for a full set
of modules and instructions.
When you're done with the demo, make sure to tear down all of your instances and clean-up. You will get charged for this usage and you will accumulate additional charges if you do not remove these resources.
Fortunately, you can use the clean-up.yml
playbook for destroying these
demo Compute Engine resources. The following command can be used to destroy
all of the resources created for this demo.
ansible-playbook clean-up.yml
-
Make sure your GCP Project Name is set. To set it, go to API Manager > Credentials > OAuth consent screen.
-
If Ansible keeps saying that requests or googleauth aren't installed, run
which python
to see which copy of the Python interpreter you installed the dependencies to. In ansible_hosts, appendansible_python_interpreter=PATH_FROM_WHICH_PYTHON
to thelocalhost
line
Have a patch that will benefit this project? Awesome! Follow these steps to have it accepted.
- Please sign our Contributor License Agreement.
- Fork this Git repository and make your changes.
- Run the unit tests. (gcimagebundle only)
- Create a Pull Request
- Incorporate review feedback to your changes.
- Accepted!
All files in this repository are under the Apache License, Version 2.0 unless noted otherwise.