2014-03-14 20:01:07 +01:00
Google Cloud Platform Guide
===========================
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
.. gce_intro:
2014-03-05 16:53:38 +01:00
Introduction
------------
2014-03-14 20:01:07 +01:00
.. note :: This section of the documentation is under construction. We are in the process of adding more examples about all of the GCE modules and how they work together. Upgrades via github pull requests are welcomed!
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
Ansible contains modules for managing Google Compute Engine resources, including creating instances, controlling network access, working with persistent disks, and managing
load balancers. Additionally, there is an inventory plugin that can automatically suck down all of your GCE instances into Ansible dynamic inventory, and create groups by tag and other properties.
2016-05-12 17:57:26 +02:00
The GCE modules all require the apache-libcloud module which you can install from pip:
2014-03-05 16:53:38 +01:00
.. code-block :: bash
$ pip install apache-libcloud
2014-03-14 20:01:07 +01:00
.. note :: If you're using Ansible on Mac OS X, libcloud also needs to access a CA cert chain. You'll need to download one (you can get one for `here <http://curl.haxx.se/docs/caextract.html> `_ .)
2014-03-05 16:53:38 +01:00
Credentials
-----------
2016-05-12 17:57:26 +02:00
To work with the GCE modules, you'll first need to get some credentials in the
JSON format:
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
1. `Create a Service Account <https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount> `_
2. `Download JSON credentials <https://support.google.com/cloud/answer/6158849?hl=en&ref_topic=6262490#serviceaccounts> `_
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
There are three different ways to provide credentials to Ansible so that it can talk with Google Cloud for provisioning and configuration actions:
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
.. note :: If you would like to use JSON credentials you must have libcloud >= 0.17.0
2014-03-05 16:53:38 +01:00
* by providing to the modules directly
* by populating a `` secrets.py `` file
2016-05-12 17:57:26 +02:00
* by setting environment variables
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
Calling Modules By Passing Credentials
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
For the GCE modules you can specify the credentials as arguments:
2014-03-05 16:53:38 +01:00
* `` service_account_email `` : email associated with the project
2016-05-12 17:57:26 +02:00
* `` credentials_file `` : path to the JSON credentials file
2014-03-05 16:53:38 +01:00
* `` project_id `` : id of the project
For example, to create a new instance using the cloud module, you can use the following configuration:
.. code-block :: yaml
2014-03-14 20:01:07 +01:00
2014-03-05 16:53:38 +01:00
- name: Create instance(s)
hosts: localhost
2016-05-12 17:57:26 +02:00
connection: local
2014-03-05 16:53:38 +01:00
gather_facts: no
2014-03-14 20:01:07 +01:00
2014-03-05 16:53:38 +01:00
vars:
service_account_email: unique-id@developer.gserviceaccount.com
2016-05-12 17:57:26 +02:00
credentials_file: /path/to/project.json
2014-03-05 16:53:38 +01:00
project_id: project-id
machine_type: n1-standard-1
image: debian-7
2014-03-14 20:01:07 +01:00
2014-03-05 16:53:38 +01:00
tasks:
2014-03-14 20:01:07 +01:00
2014-03-05 16:53:38 +01:00
- name: Launch instances
2016-05-12 17:57:26 +02:00
gce:
instance_names: dev
2014-03-14 20:01:07 +01:00
machine_type: "{{ machine_type }}"
image: "{{ image }}"
service_account_email: "{{ service_account_email }}"
2016-05-12 17:57:26 +02:00
credentials_file: "{{ credentials_file }}"
2014-03-14 20:01:07 +01:00
project_id: "{{ project_id }}"
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
When running Ansible inside a GCE VM you can use the service account credentials from the local metadata server by
setting both `` service_account_email `` and `` credentials_file `` to a blank string.
Configuring Modules with secrets.py
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2014-03-05 16:53:38 +01:00
Create a file `` secrets.py `` looking like following, and put it in some folder which is in your `` $PYTHONPATH `` :
.. code-block :: python
2016-05-12 17:57:26 +02:00
GCE_PARAMS = ('i...@project.googleusercontent.com', '/path/to/project.json')
2015-02-12 10:15:43 +01:00
GCE_KEYWORD_PARAMS = {'project': 'project_id'}
2014-03-05 16:53:38 +01:00
2015-07-10 18:42:59 +02:00
Ensure to enter the email address from the created services account and not the one from your main account.
2015-06-12 16:21:23 +02:00
2014-03-14 20:01:07 +01:00
Now the modules can be used as above, but the account information can be omitted.
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
If you are running Ansible from inside a GCE VM with an authorized service account you can set the email address and
credentials path as follows so that get automatically picked up:
.. code-block :: python
GCE_PARAMS = ('', '')
2017-06-28 22:04:24 +02:00
GCE_KEYWORD_PARAMS = {'project': 'project_id', 'datacenter': ''}
2016-05-12 17:57:26 +02:00
Configuring Modules with Environment Variables
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
Set the following environment variables before running Ansible in order to configure your credentials:
.. code-block :: bash
GCE_EMAIL
GCE_PROJECT
GCE_CREDENTIALS_FILE_PATH
2014-03-14 20:01:07 +01:00
GCE Dynamic Inventory
---------------------
2014-03-05 16:53:38 +01:00
The best way to interact with your hosts is to use the gce inventory plugin, which dynamically queries GCE and tells Ansible what nodes can be managed.
2015-07-10 18:59:52 +02:00
Note that when using the inventory script `` gce.py `` , you also need to populate the `` gce.ini `` file that you can find in the contrib/inventory directory of the ansible checkout.
2014-03-14 20:01:07 +01:00
2015-07-10 18:59:52 +02:00
To use the GCE dynamic inventory script, copy `` gce.py `` from `` contrib/inventory `` into your inventory directory and make it executable. You can specify credentials for `` gce.py `` using the `` GCE_INI_PATH `` environment variable -- the default is to look for gce.ini in the same directory as the inventory script.
2014-03-14 20:01:07 +01:00
Let's see if inventory is working:
2014-03-26 04:18:22 +01:00
.. code-block :: bash
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
$ ./gce.py --list
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
You should see output describing the hosts you have, if any, running in Google Compute Engine.
Now let's see if we can use the inventory script to talk to Google.
2014-03-05 16:53:38 +01:00
.. code-block :: bash
$ GCE_INI_PATH=~/.gce.ini ansible all -i gce.py -m setup
hostname | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"x.x.x.x"
],
2015-07-10 18:59:52 +02:00
As with all dynamic inventory scripts in Ansible, you can configure the inventory path in ansible.cfg. The recommended way to use the inventory is to create an `` inventory `` directory, and place both the `` gce.py `` script and a file containing `` localhost `` in it. This can allow for cloud inventory to be used alongside local inventory (such as a physical datacenter) or machines running in different providers.
2014-03-05 16:53:38 +01:00
Executing `` ansible `` or `` ansible-playbook `` and specifying the `` inventory `` directory instead of an individual file will cause ansible to evaluate each file in that directory for inventory.
2014-03-14 20:01:07 +01:00
Let's once again use our inventory script to see if it can talk to Google Cloud:
2014-03-05 16:53:38 +01:00
.. code-block :: bash
$ ansible all -i inventory/ -m setup
hostname | success >> {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"x.x.x.x"
],
2014-03-14 20:01:07 +01:00
The output should be similar to the previous command. If you're wanting less output and just want to check for SSH connectivity, use "-m" ping instead.
2014-03-05 16:53:38 +01:00
Use Cases
---------
2014-03-14 20:01:07 +01:00
For the following use case, let's use this small shell script as a wrapper.
2014-03-05 16:53:38 +01:00
.. code-block :: bash
2014-11-17 22:23:18 +01:00
#!/usr/bin/env bash
2014-03-05 16:53:38 +01:00
PLAYBOOK="$1"
2014-09-29 05:18:39 +02:00
if [[ -z $PLAYBOOK ]]; then
echo "You need to pass a playbook as argument to this script."
2014-03-05 16:53:38 +01:00
exit 1
fi
2017-09-05 16:29:11 +02:00
export SSL_CERT_FILE=$(pwd)/cacert.pem
2014-03-05 16:53:38 +01:00
export ANSIBLE_HOST_KEY_CHECKING=False
2014-09-29 05:18:39 +02:00
if [[ ! -f "$SSL_CERT_FILE" ]]; then
2014-03-05 16:53:38 +01:00
curl -O http://curl.haxx.se/ca/cacert.pem
fi
ansible-playbook -v -i inventory/ "$PLAYBOOK"
Create an instance
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
The GCE module provides the ability to provision instances within Google Compute Engine. The provisioning task is typically performed from your Ansible control server against Google Cloud's API.
A playbook would looks like this:
.. code-block :: yaml
- name: Create instance(s)
hosts: localhost
gather_facts: no
2014-03-14 20:01:07 +01:00
connection: local
2014-03-05 16:53:38 +01:00
vars:
machine_type: n1-standard-1 # default
image: debian-7
service_account_email: unique-id@developer.gserviceaccount.com
2016-05-12 17:57:26 +02:00
credentials_file: /path/to/project.json
2014-03-05 16:53:38 +01:00
project_id: project-id
2014-03-14 20:01:07 +01:00
2014-03-05 16:53:38 +01:00
tasks:
- name: Launch instances
2014-03-14 20:01:07 +01:00
gce:
2014-09-29 05:18:39 +02:00
instance_names: dev
machine_type: "{{ machine_type }}"
image: "{{ image }}"
service_account_email: "{{ service_account_email }}"
2016-05-12 17:57:26 +02:00
credentials_file: "{{ credentials_file }}"
2014-03-14 20:01:07 +01:00
project_id: "{{ project_id }}"
tags: webserver
register: gce
- name: Wait for SSH to come up
wait_for: host={{ item.public_ip }} port=22 delay=10 timeout=60
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
loop: "{{ gce.instance_data }}"
2014-03-14 20:01:07 +01:00
2014-09-29 05:18:39 +02:00
- name: Add host to groupname
add_host: hostname={{ item.public_ip }} groupname=new_instances
move from with_<lookup>: to loop:
- old functionality is still available direct lookup use, the following are equivalent
with_nested: [[1,2,3], ['a','b','c']]
loop: "{{lookup('nested', [1,2,3], ['a','b','c'])}}"
- avoid squashing with 'loop:'
- fixed test to use new intenal attributes
- removed most of 'lookup docs' as these now reside in the plugins
2017-09-17 05:32:34 +02:00
loop: "{{ gce.instance_data }}"
2014-03-14 20:01:07 +01:00
- name: Manage new instances
hosts: new_instances
connection: ssh
2014-09-29 05:18:39 +02:00
sudo: True
2014-03-14 20:01:07 +01:00
roles:
- base_configuration
- production_server
2014-09-29 05:18:39 +02:00
2014-03-14 20:01:07 +01:00
Note that use of the "add_host" module above creates a temporary, in-memory group. This means that a play in the same playbook can then manage machines
in the 'new_instances' group, if so desired. Any sort of arbitrary configuration is possible at this point.
Configuring instances in a group
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
All of the created instances in GCE are grouped by tag. Since this is a cloud, it's probably best to ignore hostnames and just focus on group management.
Normally we'd also use roles here, but the following example is a simple one. Here we will also use the "gce_net" module to open up access to port 80 on
these nodes.
The variables in the 'vars' section could also be kept in a 'vars_files' file or something encrypted with Ansible-vault, if you so choose. This is just
a basic example of what is possible::
- name: Setup web servers
hosts: tag_webserver
gather_facts: no
vars:
machine_type: n1-standard-1 # default
image: debian-7
service_account_email: unique-id@developer.gserviceaccount.com
2016-05-12 17:57:26 +02:00
credentials_file: /path/to/project.json
2014-03-14 20:01:07 +01:00
project_id: project-id
roles:
- name: Install lighttpd
apt: pkg=lighttpd state=installed
sudo: True
- name: Allow HTTP
local_action: gce_net
args:
fwname: "all-http"
name: "default"
2016-05-12 17:57:26 +02:00
allowed: "tcp:80"
state: "present"
service_account_email: "{{ service_account_email }}"
credentials_file: "{{ credentials_file }}"
2014-03-14 20:01:07 +01:00
project_id: "{{ project_id }}"
2014-03-05 16:53:38 +01:00
2014-03-14 20:01:07 +01:00
By pointing your browser to the IP of the server, you should see a page welcoming you.
2014-03-05 16:53:38 +01:00
2016-05-12 17:57:26 +02:00
Upgrades to this documentation are welcome, hit the github link at the top right of this page if you would like to make additions!