Deprecated

Removed in 2.0.0

i

Reason:Updated modules released with increased functionality | Alternative:Use M(google.cloud.gcp_compute_instance) instead.

community.general.gce (1.3.14) — module

create or terminate GCE instances

Authors: Eric Johnson (@erjohnso) <erjohnso@google.com>, Tom Melendez (@supertom) <supertom@google.com>

Install collection

Install with ansible-galaxy collection install community.general:==1.3.14


Add to requirements.yml

  collections:
    - name: community.general
      version: 1.3.14

Description

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


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Basic provisioning example.  Create a single Debian 8 instance in the
# us-central1-a Zone of the n1-standard-1 machine type.
# Create multiple instances by specifying multiple names, separated by
# commas in the instance_names field
# (e.g. my-test-instance1,my-test-instance2)
  - community.general.gce:
      instance_names: my-test-instance1
      zone: us-central1-a
      machine_type: n1-standard-1
      image: debian-8
      state: present
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"
      disk_size: 32
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Create a single instance of an image from the "my-base-image" image family
# in the us-central1-a Zone of the n1-standard-1 machine type.
# This image family is in the "my-other-project" GCP project.
  - community.general.gce:
      instance_names: my-test-instance1
      zone: us-central1-a
      machine_type: n1-standard-1
      image_family: my-base-image
      external_projects:
        - my-other-project
      state: present
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"
      disk_size: 32
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Create a single Debian 8 instance in the us-central1-a Zone
# Use existing disks, custom network/subnetwork, set service account permissions
# add tags and metadata.
  - community.general.gce:
      instance_names: my-test-instance
      zone: us-central1-a
      machine_type: n1-standard-1
      state: present
      metadata: '{"db":"postgres", "group":"qa", "id":500}'
      tags:
        - http-server
        - my-other-tag
      disks:
        - name: disk-2
          mode: READ_WRITE
        - name: disk-3
          mode: READ_ONLY
      disk_auto_delete: false
      network: foobar-network
      subnetwork: foobar-subnetwork-1
      preemptible: true
      ip_forward: true
      service_account_permissions:
        - storage-full
        - taskqueue
        - bigquery
        - https://www.googleapis.com/auth/ndev.clouddns.readwrite
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Basic provisioning example.  Create a single Debian 8 instance in the
# us-central1-a Zone of the n1-standard-1 machine type.
# Create multiple instances by specifying multiple names, separated by
# commas in the instance_names field
# (e.g. my-test-instance1,my-test-instance2)
  - community.general.gce:
      instance_names: my-test-instance1
      zone: us-central1-a
      machine_type: n1-standard-1
      image: debian-8
      state: present
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"
      disk_size: 32

# Create a single instance of an image from the "my-base-image" image family
# in the us-central1-a Zone of the n1-standard-1 machine type.
# This image family is in the "my-other-project" GCP project.
  - community.general.gce:
      instance_names: my-test-instance1
      zone: us-central1-a
      machine_type: n1-standard-1
      image_family: my-base-image
      external_projects:
        - my-other-project
      state: present
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"
      disk_size: 32

# Create a single Debian 8 instance in the us-central1-a Zone
# Use existing disks, custom network/subnetwork, set service account permissions
# add tags and metadata.
  - community.general.gce:
      instance_names: my-test-instance
      zone: us-central1-a
      machine_type: n1-standard-1
      state: present
      metadata: '{"db":"postgres", "group":"qa", "id":500}'
      tags:
        - http-server
        - my-other-tag
      disks:
        - name: disk-2
          mode: READ_WRITE
        - name: disk-3
          mode: READ_ONLY
      disk_auto_delete: false
      network: foobar-network
      subnetwork: foobar-subnetwork-1
      preemptible: true
      ip_forward: true
      service_account_permissions:
        - storage-full
        - taskqueue
        - bigquery
        - https://www.googleapis.com/auth/ndev.clouddns.readwrite
      service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
      credentials_file: "/path/to/your-key.json"
      project_id: "your-project-name"

---
# Example Playbook
- name: Compute Engine Instance Examples
  hosts: localhost
  vars:
    service_account_email: "your-sa@your-project-name.iam.gserviceaccount.com"
    credentials_file: "/path/to/your-key.json"
    project_id: "your-project-name"
  tasks:
    - name: Create multiple instances
      # Basic provisioning example.  Create multiple Debian 8 instances in the
      # us-central1-a Zone of n1-standard-1 machine type.
      community.general.gce:
        instance_names: test1,test2,test3
        zone: us-central1-a
        machine_type: n1-standard-1
        image: debian-8
        state: present
        service_account_email: "{{ service_account_email }}"
        credentials_file: "{{ credentials_file }}"
        project_id: "{{ project_id }}"
        metadata : '{ "startup-script" : "apt-get update" }'
      register: gce

    - name: Save host data
      ansible.builtin.add_host:
        hostname: "{{ item.public_ip }}"
        groupname: gce_instances_ips
      with_items: "{{ gce.instance_data }}"

    - name: Wait for SSH for instances
      ansible.builtin.wait_for:
        delay: 1
        host: "{{ item.public_ip }}"
        port: 22
        state: started
        timeout: 30
      with_items: "{{ gce.instance_data }}"

    - name: Configure Hosts
      hosts: gce_instances_ips
      become: yes
      become_method: sudo
      roles:
        - my-role-one
        - my-role-two
      tags:
        - config

    - name: Delete test-instances
      # Basic termination of instance.
      community.general.gce:
        service_account_email: "{{ service_account_email }}"
        credentials_file: "{{ credentials_file }}"
        project_id: "{{ project_id }}"
        instance_names: "{{ gce.instance_names }}"
        zone: us-central1-a
        state: absent
      tags:
        - delete

Inputs

    
name:
    aliases:
    - base_name
    description:
    - either a name of a single instance or when used with 'num_instances', the base name
      of a cluster of nodes
    type: str

tags:
    description:
    - a comma-separated list of tags to associate with the instance
    type: list

zone:
    default: us-central1-a
    description:
    - the GCE zone to use. The list of available zones is at U(https://cloud.google.com/compute/docs/regions-zones/regions-zones#available).
    type: str

disks:
    description:
    - 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).
    type: list

image:
    default: debian-8
    description:
    - image string to use for the instance (default will follow latest stable debian image)
    type: str

state:
    choices:
    - active
    - present
    - absent
    - deleted
    - started
    - stopped
    - terminated
    default: present
    description:
    - desired state of the resource
    type: str

network:
    default: default
    description:
    - name of the network, 'default' will be used if not specified
    type: str

metadata:
    description:
    - a hash/dictionary of custom data for the instance; '{"key":"value", ...}'
    type: str

pem_file:
    description:
    - path to the pem file associated with the service account email This option is deprecated.
      Use 'credentials_file'.
    type: path

disk_size:
    default: 10
    description:
    - The size of the boot disk created for this instance (in GB)
    type: int

ip_forward:
    default: 'no'
    description:
    - set to C(yes) if the instance can forward ip packets (useful for gateways)
    type: bool

project_id:
    description:
    - your GCE project ID
    type: str

subnetwork:
    description:
    - name of the subnetwork in which the instance should be created
    type: str

external_ip:
    default: ephemeral
    description:
    - type of external ip, ephemeral by default; alternatively, a fixed gce ip or ip name
      can be given. Specify 'none' if no external ip is desired.
    type: str

preemptible:
    description:
    - if set to C(yes), instances will be preemptible and time-limited. (requires libcloud
      >= 0.20.0)
    type: bool

image_family:
    description:
    - image family from which to select the image.  The most recent non-deprecated image
      in the family will be used.
    type: str

machine_type:
    default: n1-standard-1
    description:
    - machine type to use for the instance, use 'n1-standard-1' by default
    type: str

num_instances:
    description:
    - can be used with 'name', specifies the number of nodes to provision using 'name'
      as a base name
    type: int

instance_names:
    description:
    - a comma-separated list of instance names to create or destroy
    type: str

credentials_file:
    description:
    - path to the JSON file associated with the service account email
    type: path

disk_auto_delete:
    default: 'yes'
    description:
    - if set boot disk will be removed after instance destruction
    type: bool

external_projects:
    description:
    - A list of other projects (accessible with the provisioning credentials) to be searched
      for the image.
    type: list

persistent_boot_disk:
    default: 'no'
    description:
    - if set, create the instance with a persistent boot disk
    type: bool

service_account_email:
    description:
    - service account email
    type: str

service_account_permissions:
    description:
    - service account permissions (see U(https://cloud.google.com/sdk/gcloud/reference/compute/instances/create),
      --scopes section for detailed information)
    - 'Available choices are: C(bigquery), C(cloud-platform), C(compute-ro), C(compute-rw),
      C(useraccounts-ro), C(useraccounts-rw), C(datastore), C(logging-write), C(monitoring),
      C(sql-admin), C(storage-full), C(storage-ro), C(storage-rw), C(taskqueue), C(userinfo-email).

      '
    type: list