Documentation

gce - create or terminate GCE instances

New in version 1.4.

Synopsis

Creates or terminates Google Compute Engine (GCE) instances. See https://cloud.google.com/products/compute-engine for an overview. Full install/configuration instructions for the gce* modules can be found in the comments of ansible/test/gce_tests.py.

Requirements (on host that executes module)

  • python >= 2.6
  • apache-libcloud >= 0.13.3, >= 0.17.0 if using JSON credentials, >= 0.20.0 if using preemptible option

Options

parameter required default choices comments
credentials_file
(added in 2.1.0)
no
    path to the JSON file associated with the service account email
    disk_auto_delete
    (added in 1.9)
    no true
      if set boot disk will be removed after instance destruction
      disks
      (added in 1.7)
      no
        a list of persistent disks to attach to the instance; a string value gives the name of the disk; alternatively, a dictionary value can define 'name' and 'mode' ('READ_ONLY' or 'READ_WRITE'). The first entry will be the boot disk (which must be READ_WRITE).
        external_ip
        (added in 1.9)
        no ephemeral
          type of external ip, ephemeral by default; alternatively, a list of fixed gce ips or ip names can be given (if there is not enough specified ip, 'ephemeral' will be used)
          image
          no debian-7
            image string to use for the instance
            instance_names
            no
              a comma-separated list of instance names to create or destroy
              ip_forward
              (added in 1.9)
              no false
                set to true if the instance can forward ip packets (useful for gateways)
                machine_type
                no n1-standard-1
                  machine type to use for the instance, use 'n1-standard-1' by default
                  metadata
                  no
                    a hash/dictionary of custom data for the instance; '{"key":"value", ...}'
                    name
                    no
                      identifier when working with a single instance
                      network
                      no default
                        name of the network, 'default' will be used if not specified
                        pem_file
                        (added in 1.5.1)
                        no
                          path to the pem file associated with the service account email This option is deprecated. Use 'credentials_file'.
                          persistent_boot_disk
                          no false
                            if set, create the instance with a persistent boot disk
                            preemptible
                            (added in 2.1)
                            no false
                              if set to true, instances will be preemptible and time-limited. (requires libcloud >= 0.20.0)
                              project_id
                              (added in 1.5.1)
                              no
                                your GCE project ID
                                service_account_email
                                (added in 1.5.1)
                                no
                                  service account email
                                  service_account_permissions
                                  (added in 2.0)
                                  no
                                  • bigquery
                                  • cloud-platform
                                  • compute-ro
                                  • compute-rw
                                  • useraccounts-ro
                                  • useraccounts-rw
                                  • datastore
                                  • logging-write
                                  • monitoring
                                  • sql
                                  • sql-admin
                                  • storage-full
                                  • storage-ro
                                  • storage-rw
                                  • taskqueue
                                  • userinfo-email
                                  service account permissions (see https://cloud.google.com/sdk/gcloud/reference/compute/instances/create, --scopes section for detailed information)
                                  state
                                  no present
                                  • active
                                  • present
                                  • absent
                                  • deleted
                                  desired state of the resource
                                  tags
                                  no
                                    a comma-separated list of tags to associate with the instance
                                    zone
                                    yes us-central1-a
                                      the GCE zone to use

                                      Examples

                                      # Basic provisioning example.  Create a single Debian 7 instance in the
                                      # us-central1-a Zone of n1-standard-1 machine type.
                                      - local_action:
                                          module: gce
                                          name: test-instance
                                          zone: us-central1-a
                                          machine_type: n1-standard-1
                                          image: debian-7
                                      
                                      # Example using defaults and with metadata to create a single 'foo' instance
                                      - local_action:
                                          module: gce
                                          name: foo
                                          metadata: '{"db":"postgres", "group":"qa", "id":500}'
                                      
                                      
                                      # Launch instances from a control node, runs some tasks on the new instances,
                                      # and then terminate them
                                      # This example uses JSON credentials with the credentials_file parameter
                                      # rather than the deprecated pem_file option with PEM formatted credentials.
                                      
                                      - name: Create a sandbox instance
                                        hosts: localhost
                                        vars:
                                          names: foo,bar
                                          machine_type: n1-standard-1
                                          image: debian-6
                                          zone: us-central1-a
                                          service_account_email: unique-email@developer.gserviceaccount.com
                                          credentials_file: /path/to/json_file
                                          project_id: project-id
                                        tasks:
                                          - name: Launch instances
                                            local_action: gce instance_names={{names}} machine_type={{machine_type}}
                                                          image={{image}} zone={{zone}}
                                                          service_account_email={{ service_account_email }}
                                                          credentials_file={{ credentials_file }}
                                                          project_id={{ project_id }}
                                            register: gce
                                          - name: Wait for SSH to come up
                                            local_action: wait_for host={{item.public_ip}} port=22 delay=10
                                                          timeout=60 state=started
                                            with_items: {{gce.instance_data}}
                                      
                                      - name: Configure instance(s)
                                        hosts: launched
                                        become: True
                                        roles:
                                          - my_awesome_role
                                          - my_awesome_tasks
                                      
                                      - name: Terminate instances
                                        hosts: localhost
                                        connection: local
                                        tasks:
                                          - name: Terminate instances that were previously launched
                                            local_action:
                                              module: gce
                                              state: 'absent'
                                              instance_names: {{gce.instance_names}}
                                      
                                      # The deprecated PEM file credentials can be used as follows
                                      - name: Create a sandbox instance with PEM credentials
                                        hosts: localhost
                                        vars:
                                          names: foo,bar
                                          machine_type: n1-standard-1
                                          image: debian-6
                                          zone: us-central1-a
                                          service_account_email: unique-email@developer.gserviceaccount.com
                                          pem_file: /path/to/pem_file
                                          project_id: project-id
                                        tasks:
                                          - name: Launch instances
                                            local_action: gce instance_names={{names}} machine_type={{machine_type}}
                                                          image={{image}} zone={{zone}}
                                                          service_account_email={{ service_account_email }}
                                                          pem_file={{ pem_file }}
                                                          project_id={{ project_id }}
                                            register: gce
                                          - name: Wait for SSH to come up
                                            local_action: wait_for host={{item.public_ip}} port=22 delay=10
                                                          timeout=60 state=started
                                            with_items: {{gce.instance_data}}
                                      

                                      Notes

                                      Note

                                      Either name or instance_names is required.

                                      This is a Core Module

                                      For more information on what this means please read Core Modules

                                      For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Helping Testing PRs and Developing Modules.