Google Cloud Platform Guide

    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.

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

    • 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

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

    • auth_kind: type of authentication being used (choices: machineaccount, serviceaccount, application)
    • : email associated with the project
    • service_account_file: path to the JSON credentials file
    • project: id of the project
    • : 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 }}"

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

    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.

    1. 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:

    Executing will create a list of GCP instances that are ready to be configured using Ansible.

    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. service_account_file: "{{ gcp_cred_file }}"
    42. scopes:
    43. - https://www.googleapis.com/auth/compute
    44. state: present
    45. register: address
    46. - name: create a instance
    47. gcp_compute_instance:
    48. state: present
    49. name: test-vm
    50. machine_type: n1-standard-1
    51. disks:
    52. - auto_delete: true
    53. boot: true
    54. source: "{{ disk }}"
    55. network_interfaces:
    56. - network: "{{ network }}"
    57. access_configs:
    58. - name: 'External NAT'
    59. nat_ip: "{{ address }}"
    60. type: 'ONE_TO_ONE_NAT'
    61. zone: "{{ zone }}"
    62. project: "{{ gcp_project }}"
    63. auth_kind: "{{ gcp_cred_kind }}"
    64. service_account_file: "{{ gcp_cred_file }}"
    65. scopes:
    66. - https://www.googleapis.com/auth/compute
    67. register: instance
    68.  
    69. - name: Wait for SSH to come up
    70. wait_for: host={{ instance.address }} port=22 delay=10 timeout=60
    71.  
    72. - name: Add host to groupname
    73. add_host: hostname={{ instance.address }} groupname=new_instances
    74.  
    75.  
    76. - name: Manage new instances
    77. hosts: new_instances
    78. connection: ssh
    79. sudo: True
    80. roles:
    81. - base_configuration

    For more information about Google Cloud, please visit the .