google.cloud.gcp_compute inventory – Google Cloud Compute Engine inventory source

Note

This inventory plugin is part of the google.cloud collection (version 1.4.1).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install google.cloud. You need further requirements to be able to use this inventory plugin, see Requirements for details.

To use it in a playbook, specify: google.cloud.gcp_compute.

Note

The google.cloud collection will be removed from Ansible 12 due to violations of the Ansible inclusion requirements. The collection has unresolved sanity test failures. See the discussion thread for more information.

Synopsis

  • Get inventory hosts from Google Cloud Platform GCE.

  • Uses a YAML configuration file that ends with gcp_compute.(yml|yaml) or gcp.(yml|yaml).

Requirements

The below requirements are needed on the local controller node that executes this inventory.

  • requests >= 2.18.4

  • google-auth >= 1.3.0

Parameters

Parameter

Comments

access_token

string

An OAuth2 access token if credential type is accesstoken.

Configuration:

auth_kind

string / required

The type of credential used.

Choices:

  • "application"

  • "serviceaccount"

  • "machineaccount"

  • "accesstoken"

Configuration:

cache

boolean

Toggle to enable/disable the caching of the inventory’s source data, requires a cache plugin setup to work.

Choices:

  • false ← (default)

  • true

Configuration:

cache_connection

string

Cache connection data or path, read cache plugin documentation for specifics.

Configuration:

cache_plugin

string

Cache plugin to use for the inventory’s source data.

Default: "memory"

Configuration:

cache_prefix

string

Prefix to use for cache plugin files/tables.

Default: "ansible_inventory_"

Configuration:

cache_timeout

integer

Cache duration in seconds.

Default: 3600

Configuration:

compose

dictionary

Create vars from jinja2 expressions.

Default: {}

filters

list / elements=string

A list of filter value pairs. Available filters are listed here https://cloud.google.com/compute/docs/reference/rest/v1/instances/aggregatedList. Each additional filter in the list will be added as an AND condition (filter1 and filter2)

folders

list / elements=string

A folder that contains many projects

groups

dictionary

Add hosts to group based on Jinja2 conditionals.

Default: {}

hostnames

list / elements=string

A list of options that describe the ordering for which hostnames should be assigned. Currently supported hostnames are ‘public_ip’, ‘private_ip’, ‘name’ or ‘labels.vm_name’.

Default: ["public_ip", "private_ip", "name"]

keyed_groups

list / elements=dictionary

Add hosts to group based on the values of a variable.

Default: []

default_value

string

added in ansible-core 2.12

The default value when the host variable’s value is an empty string.

This option is mutually exclusive with keyed_groups[].trailing_separator.

key

string

The key from input dictionary used to generate groups.

parent_group

string

parent group for keyed group.

prefix

string

A keyed group name will start with this prefix.

Default: ""

separator

string

separator used to build the keyed group name.

Default: "_"

trailing_separator

boolean

added in ansible-core 2.12

Set this option to false to omit the keyed_groups[].separator after the host variable when the value is an empty string.

This option is mutually exclusive with keyed_groups[].default_value.

Choices:

  • false

  • true ← (default)

leading_separator

boolean

added in ansible-core 2.11

Use in conjunction with keyed_groups.

By default, a keyed group that does not have a prefix or a separator provided will have a name that starts with an underscore.

This is because the default prefix is "" and the default separator is "_".

Set this option to false to omit the leading underscore (or other separator) if no prefix is given.

If the group name is derived from a mapping the separator is still used to concatenate the items.

To not use a separator in the group name at all, set the separator for the keyed group to an empty string instead.

Choices:

  • false

  • true ← (default)

name_suffix

string

Custom domain suffix. If set, this string will be appended to all hosts.

Default: ""

plugin

string / required

token that ensures this is a source file for the ‘gcp_compute’ plugin.

Choices:

  • "google.cloud.gcp_compute"

  • "gcp_compute"

projects

list / elements=string

A list of projects in which to describe GCE instances.

retrieve_image_info

boolean

Populate the image host fact for the instances returned with the GCP image name

By default this plugin does not attempt to resolve the boot image of an instance to the image name cataloged in GCP because of the performance overhead of the task.

Unless this option is enabled, the image host variable will be null

Choices:

  • false ← (default)

  • true

scopes

list / elements=string

list of authentication scopes

Default: ["https://www.googleapis.com/auth/compute"]

Configuration:

service_account_contents

string

A string representing the contents of a Service Account JSON file. This should not be passed in as a dictionary, but a string that has the exact contents of a service account json file (valid JSON).

Configuration:

service_account_email

string

An optional service account email address if machineaccount is selected and the user does not wish to use the default email.

Configuration:

service_account_file

path

The path of a Service Account JSON file if serviceaccount is selected as type.

Configuration:

strict

boolean

If yes make invalid entries a fatal error, otherwise skip and continue.

Since it is possible to use facts in the expressions they might not always be available and we ignore those errors by default.

Choices:

  • false ← (default)

  • true

use_contrib_script_compatible_sanitization

boolean

By default this plugin is using a general group name sanitization to create safe and usable group names for use in Ansible. This option allows you to override that, in efforts to allow migration from the old inventory script.

For this to work you should also turn off the TRANSFORM_INVALID_GROUP_CHARS setting, otherwise the core engine will just use the standard sanitization on top.

This is not the default as such names break certain functionality as not all characters are valid Python identifiers which group names end up being used as.

Choices:

  • false ← (default)

  • true

use_extra_vars

boolean

added in ansible-core 2.11

Merge extra vars into the available variables for composition (highest precedence).

Choices:

  • false ← (default)

  • true

Configuration:

vars_prefix

string

prefix to apply to host variables, does not include facts nor params

Default: ""

zones

list / elements=string

A list of regions in which to describe GCE instances. If none provided, it defaults to all zones available to a given project.

Examples

plugin: google.cloud.gcp_compute
zones: # populate inventory with instances in these regions
  - us-east1-a
projects:
  - gcp-prod-gke-100
  - gcp-cicd-101
filters:
  - status = RUNNING
  - scheduling.automaticRestart = true AND status = RUNNING
service_account_file: /tmp/service_account.json
auth_kind: serviceaccount
scopes:
 - 'https://www.googleapis.com/auth/cloud-platform'
 - 'https://www.googleapis.com/auth/compute.readonly'
keyed_groups:
  # Create groups from GCE labels
  - prefix: gcp
    key: labels
name_suffix: .example.com
hostnames:
  # List host by name instead of the default public ip
  - name
compose:
  # Set an inventory parameter to use the Public IP address to connect to the host
  # For Private ip use "networkInterfaces[0].networkIP"
  ansible_host: networkInterfaces[0].accessConfigs[0].natIP

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.