Google Cloud Platform Guide

Introduction

Ansible + Google have been working together on a set of auto-generatedAnsible modules designed to consistently and comprehensively cover the entiretyof the Google Cloud Platform.

Ansible contains modules for managing Google Cloud Platform resources,including creating instances, controlling network access, working withpersistent disks, managing load balancers, and a lot more.

These new modules can be found under a new consistent name scheme “gcp*”(Note: gcp_target_proxy and gcp_url_map are legacy modules, despite the “gcp*”name. Please use gcp_compute_target_proxy and gcp_compute_url_map instead).

Additionally, the gcp_compute inventory plugin can discover all GCE instancesand make them automatically available in your Ansible inventory.

You may see a collection of other GCP modules that do not conform to thisnaming convention. These are the original modules primarily developed by theAnsible community. You will find some overlapping functionality such as withthe “gce” module and the new “gcp_compute_instance” module. Either can beused, but you may experience issues trying to use them together.

While the community GCP modules are not going away, Google is investing effortinto the new “gcp_*” modules. Google is committed to ensuring the Ansiblecommunity has a great experience with GCP and therefore recommends that beginadopting these new modules if possible.

Introduction

The Google Cloud Platform (GCP) modules require both the requests and thegoogle-auth libraries to be installed.

  1. $ pip install requests google-auth

Credentials

It’s easy to create a GCP account with credentials for Ansible. You have multiple options toget your credentials - here are two of the most common options:

  • Service Accounts (Recommended): Use JSON service accounts with specific permissions.
  • Machine Accounts: Use the permissions associated with the GCP Instance you’re using Ansible on.

For the following examples, we’ll be using service account credentials.

To work with the GCP modules, you’ll first need to get some credentials in theJSON format:

  • by specifying them directly as module parameters
  • by setting environment variables

Providing Credentials as Module Parameters

For the GCE modules you can specify the credentials as arguments:

  • auth_kind: type of authentication being used (choices: machineaccount, serviceaccount, application)
  • service_account_email: email associated with the project
  • service_account_file: path to the JSON credentials file
  • project: id of the project
  • scopes: The specific scopes that you want the actions to use.

For example, to create a new IP address using the gcp_compute_address module,you can use the following configuration:

  1. - name: Create IP address
  2. hosts: localhost
  3. connection: local
  4. gather_facts: no
  5.  
  6. vars:
  7. service_account_file: /home/my_account.json
  8. project: my-project
  9. auth_kind: serviceaccount
  10. scopes:
  11. - www.googleapis.com/auth/compute
  12.  
  13. tasks:
  14.  
  15. - name: Allocate an IP Address
  16. gcp_compute_address:
  17. state: present
  18. name: 'test-address1'
  19. region: 'us-west1'
  20. project: "{{ project }}"
  21. auth_kind: "{{ auth_kind }}"
  22. service_account_file: "{{ service_account_file }}"
  23. scopes: "{{ scopes }}"

Providing Credentials as Environment Variables

Set the following environment variables before running Ansible in order to configure your credentials:

  1. GCP_AUTH_KIND
  2. GCP_SERVICE_ACCOUNT_EMAIL
  3. GCP_SERVICE_ACCOUNT_FILE
  4. GCP_SCOPES

GCE Dynamic Inventory

The best way to interact with your hosts is to use the gcp_compute inventory plugin, which dynamically queries GCE and tells Ansible what nodes can be managed.

To be able to use this GCE dynamic inventory plugin, you need to enable it first by specifying the following in the ansible.cfg file:

  1. [inventory]
  2. enable_plugins = gcp_compute

Then, create a file that ends in .gcp.yml in your root directory.

The gcp_compute inventory script takes in the same authentication information as any module.

Here’s an example of a valid inventory file:

  1. plugin: gcp_compute
  2. projects:
  3. - graphite-playground
  4. filters:
  5. auth_kind: serviceaccount
  6. service_account_file: /home/alexstephen/my_account.json

Executing ansible-inventory —list -i <filename>.gcp.yml will create a list of GCP instances that are ready to be configured using Ansible.

Create an instance

The full range of GCP modules provide the ability to create a wide variety ofGCP resources with the full support of the entire GCP API.

The following playbook creates a GCE Instance. This instance relies on a GCPnetwork and a Disk. By creating the Disk and Network separately, we can give asmuch detail as necessary about how we want the disk and network formatted. Byregistering a Disk/Network to a variable, we can simply insert the variableinto the instance task. The gcp_compute_instance module will figure out therest.

  1. - name: Create an instance
  2. hosts: localhost
  3. gather_facts: no
  4. connection: local
  5. vars:
  6. project: my-project
  7. auth_kind: serviceaccount
  8. service_account_file: /home/my_account.json
  9. zone: "us-central1-a"
  10. region: "us-central1"
  11.  
  12. tasks:
  13. - name: create a disk
  14. gcp_compute_disk:
  15. name: 'disk-instance'
  16. size_gb: 50
  17. source_image: 'projects/ubuntu-os-cloud/global/images/family/ubuntu-1604-lts'
  18. zone: "{{ zone }}"
  19. project: "{{ gcp_project }}"
  20. auth_kind: "{{ gcp_cred_kind }}"
  21. service_account_file: "{{ gcp_cred_file }}"
  22. scopes:
  23. - https://www.googleapis.com/auth/compute
  24. state: present
  25. register: disk
  26. - name: create a network
  27. gcp_compute_network:
  28. name: 'network-instance'
  29. project: "{{ gcp_project }}"
  30. auth_kind: "{{ gcp_cred_kind }}"
  31. service_account_file: "{{ gcp_cred_file }}"
  32. scopes:
  33. - https://www.googleapis.com/auth/compute
  34. state: present
  35. register: network
  36. - name: create a address
  37. gcp_compute_address:
  38. name: 'address-instance'
  39. region: "{{ region }}"
  40. project: "{{ gcp_project }}"
  41. auth_kind: "{{ gcp_cred_kind }}"
  42. service_account_file: "{{ gcp_cred_file }}"
  43. scopes:
  44. - https://www.googleapis.com/auth/compute
  45. state: present
  46. register: address
  47. - name: create a instance
  48. gcp_compute_instance:
  49. state: present
  50. name: test-vm
  51. machine_type: n1-standard-1
  52. disks:
  53. - auto_delete: true
  54. boot: true
  55. source: "{{ disk }}"
  56. network_interfaces:
  57. - network: "{{ network }}"
  58. access_configs:
  59. - name: 'External NAT'
  60. nat_ip: "{{ address }}"
  61. type: 'ONE_TO_ONE_NAT'
  62. zone: "{{ zone }}"
  63. project: "{{ gcp_project }}"
  64. auth_kind: "{{ gcp_cred_kind }}"
  65. service_account_file: "{{ gcp_cred_file }}"
  66. scopes:
  67. - https://www.googleapis.com/auth/compute
  68. register: instance
  69.  
  70. - name: Wait for SSH to come up
  71. wait_for: host={{ instance.address }} port=22 delay=10 timeout=60
  72.  
  73. - name: Add host to groupname
  74. add_host: hostname={{ instance.address }} groupname=new_instances
  75.  
  76.  
  77. - name: Manage new instances
  78. hosts: new_instances
  79. connection: ssh
  80. sudo: True
  81. roles:
  82. - base_configuration
  83. - production_server

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 machinesin the ‘new_instances’ group, if so desired. Any sort of arbitrary configuration is possible at this point.

For more information about Google Cloud, please visit the Google Cloud website.