2013-10-07 23:01:37 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# Copyright 2013 Google Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: gce
|
2013-11-19 00:55:49 +01:00
|
|
|
version_added: "1.4"
|
2013-10-07 23:01:37 +02:00
|
|
|
short_description: create or terminate GCE instances
|
|
|
|
description:
|
|
|
|
- Creates or terminates Google Compute Engine (GCE) instances. See
|
|
|
|
U(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.
|
|
|
|
options:
|
|
|
|
image:
|
|
|
|
description:
|
|
|
|
- image string to use for the instance
|
|
|
|
required: false
|
|
|
|
default: "debian-7"
|
|
|
|
instance_names:
|
|
|
|
description:
|
|
|
|
- a comma-separated list of instance names to create or destroy
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
machine_type:
|
|
|
|
description:
|
|
|
|
- machine type to use for the instance, use 'n1-standard-1' by default
|
|
|
|
required: false
|
|
|
|
default: "n1-standard-1"
|
|
|
|
metadata:
|
|
|
|
description:
|
2014-10-01 11:41:17 +02:00
|
|
|
- a hash/dictionary of custom data for the instance;
|
|
|
|
'{"key": "value", ...}'
|
2013-10-07 23:01:37 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2014-03-01 21:56:15 +01:00
|
|
|
service_account_email:
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.5.1"
|
2014-03-01 21:56:15 +01:00
|
|
|
description:
|
|
|
|
- service account email
|
|
|
|
required: false
|
|
|
|
default: null
|
2015-05-15 15:34:36 +02:00
|
|
|
service_account_permissions:
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "2.0"
|
2015-05-15 15:34:36 +02:00
|
|
|
description:
|
2014-10-01 11:41:17 +02:00
|
|
|
- service account permissions (see
|
|
|
|
U(https://cloud.google.com/sdk/gcloud/reference/compute/instances/create),
|
|
|
|
--scopes section for detailed information)
|
2015-05-15 15:34:36 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2014-10-01 11:41:17 +02:00
|
|
|
choices: [
|
|
|
|
"bigquery", "cloud-platform", "compute-ro", "compute-rw",
|
|
|
|
"computeaccounts-ro", "computeaccounts-rw", "datastore", "logging-write",
|
|
|
|
"monitoring", "sql", "sql-admin", "storage-full", "storage-ro",
|
|
|
|
"storage-rw", "taskqueue", "userinfo-email"
|
|
|
|
]
|
2014-03-01 21:56:15 +01:00
|
|
|
pem_file:
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.5.1"
|
2014-03-01 21:56:15 +01:00
|
|
|
description:
|
|
|
|
- path to the pem file associated with the service account email
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
project_id:
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.5.1"
|
2014-03-01 21:56:15 +01:00
|
|
|
description:
|
|
|
|
- your GCE project ID
|
|
|
|
required: false
|
|
|
|
default: null
|
2013-10-07 23:01:37 +02:00
|
|
|
name:
|
|
|
|
description:
|
2013-10-13 22:48:09 +02:00
|
|
|
- identifier when working with a single instance
|
2013-10-07 23:01:37 +02:00
|
|
|
required: false
|
|
|
|
network:
|
|
|
|
description:
|
|
|
|
- name of the network, 'default' will be used if not specified
|
|
|
|
required: false
|
|
|
|
default: "default"
|
|
|
|
persistent_boot_disk:
|
|
|
|
description:
|
|
|
|
- if set, create the instance with a persistent boot disk
|
|
|
|
required: false
|
|
|
|
default: "false"
|
2014-04-13 00:21:26 +02:00
|
|
|
disks:
|
|
|
|
description:
|
2014-10-01 11:41:17 +02:00
|
|
|
- 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).
|
2014-04-13 00:21:26 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2014-07-20 01:08:28 +02:00
|
|
|
version_added: "1.7"
|
2013-10-07 23:01:37 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- desired state of the resource
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: ["active", "present", "absent", "deleted"]
|
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- a comma-separated list of tags to associate with the instance
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
zone:
|
|
|
|
description:
|
|
|
|
- the GCE zone to use
|
|
|
|
required: true
|
|
|
|
default: "us-central1-a"
|
2015-02-12 23:24:03 +01:00
|
|
|
ip_forward:
|
|
|
|
version_added: "1.9"
|
|
|
|
description:
|
2014-10-01 11:41:17 +02:00
|
|
|
- set to true if the instance can forward ip packets (useful for
|
|
|
|
gateways)
|
2015-02-12 23:24:03 +01:00
|
|
|
required: false
|
|
|
|
default: "false"
|
2015-03-03 15:15:45 +01:00
|
|
|
external_ip:
|
|
|
|
version_added: "1.9"
|
|
|
|
description:
|
|
|
|
- type of external ip, ephemeral by default
|
|
|
|
required: false
|
|
|
|
default: "ephemeral"
|
2015-03-04 00:49:15 +01:00
|
|
|
disk_auto_delete:
|
|
|
|
version_added: "1.9"
|
|
|
|
description:
|
|
|
|
- if set boot disk will be removed after instance destruction
|
|
|
|
required: false
|
|
|
|
default: "true"
|
2013-10-07 23:01:37 +02:00
|
|
|
|
2015-05-11 21:15:53 +02:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- "apache-libcloud >= 0.13.3"
|
2014-03-01 21:56:15 +01:00
|
|
|
notes:
|
|
|
|
- Either I(name) or I(instance_names) is required.
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Eric Johnson (@erjohnso) <erjohnso@google.com>"
|
2013-10-07 23:01:37 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
|
|
|
- name: Create a sandbox instance
|
|
|
|
hosts: localhost
|
|
|
|
vars:
|
|
|
|
names: foo,bar
|
|
|
|
machine_type: n1-standard-1
|
|
|
|
image: debian-6
|
|
|
|
zone: us-central1-a
|
2014-03-01 21:56:15 +01:00
|
|
|
service_account_email: unique-email@developer.gserviceaccount.com
|
|
|
|
pem_file: /path/to/pem_file
|
|
|
|
project_id: project-id
|
2013-10-07 23:01:37 +02:00
|
|
|
tasks:
|
|
|
|
- name: Launch instances
|
2013-11-19 23:30:22 +01:00
|
|
|
local_action: gce instance_names={{names}} machine_type={{machine_type}}
|
2014-10-01 11:41:17 +02:00
|
|
|
image={{image}} zone={{zone}}
|
|
|
|
service_account_email={{ service_account_email }}
|
2014-03-01 21:56:15 +01:00
|
|
|
pem_file={{ pem_file }} project_id={{ project_id }}
|
2013-10-07 23:01:37 +02:00
|
|
|
register: gce
|
|
|
|
- name: Wait for SSH to come up
|
2013-11-19 23:30:22 +01:00
|
|
|
local_action: wait_for host={{item.public_ip}} port=22 delay=10
|
2013-10-07 23:01:37 +02:00
|
|
|
timeout=60 state=started
|
2013-11-19 23:30:22 +01:00
|
|
|
with_items: {{gce.instance_data}}
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
- name: Configure instance(s)
|
|
|
|
hosts: launched
|
|
|
|
sudo: 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'
|
2013-11-19 23:30:22 +01:00
|
|
|
instance_names: {{gce.instance_names}}
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
2014-10-01 11:41:17 +02:00
|
|
|
import libcloud
|
2013-10-07 23:01:37 +02:00
|
|
|
from libcloud.compute.types import Provider
|
|
|
|
from libcloud.compute.providers import get_driver
|
|
|
|
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
|
2014-10-01 11:41:17 +02:00
|
|
|
ResourceExistsError, ResourceInUseError, ResourceNotFoundError
|
2013-10-07 23:01:37 +02:00
|
|
|
_ = Provider.GCE
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_LIBCLOUD = True
|
2013-10-07 23:01:37 +02:00
|
|
|
except ImportError:
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_LIBCLOUD = False
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
from ast import literal_eval
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_PYTHON26 = True
|
2013-10-07 23:01:37 +02:00
|
|
|
except ImportError:
|
2015-05-11 21:15:53 +02:00
|
|
|
HAS_PYTHON26 = False
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_instance_info(inst):
|
|
|
|
"""Retrieves instance information from an instance object and returns it
|
|
|
|
as a dictionary.
|
|
|
|
|
|
|
|
"""
|
|
|
|
metadata = {}
|
2013-10-19 18:57:47 +02:00
|
|
|
if 'metadata' in inst.extra and 'items' in inst.extra['metadata']:
|
2013-10-07 23:01:37 +02:00
|
|
|
for md in inst.extra['metadata']['items']:
|
|
|
|
metadata[md['key']] = md['value']
|
|
|
|
|
|
|
|
try:
|
|
|
|
netname = inst.extra['networkInterfaces'][0]['network'].split('/')[-1]
|
|
|
|
except:
|
|
|
|
netname = None
|
2014-04-13 00:21:26 +02:00
|
|
|
if 'disks' in inst.extra:
|
|
|
|
disk_names = [disk_info['source'].split('/')[-1]
|
|
|
|
for disk_info
|
|
|
|
in sorted(inst.extra['disks'],
|
|
|
|
key=lambda disk_info: disk_info['index'])]
|
|
|
|
else:
|
|
|
|
disk_names = []
|
2015-03-03 15:15:45 +01:00
|
|
|
|
|
|
|
if len(inst.public_ips) == 0:
|
|
|
|
public_ip = None
|
|
|
|
else:
|
|
|
|
public_ip = inst.public_ips[0]
|
|
|
|
|
2013-10-07 23:01:37 +02:00
|
|
|
return({
|
2014-10-01 11:41:17 +02:00
|
|
|
'image': inst.image is not None and inst.image.split('/')[-1] or None,
|
2014-04-13 00:21:26 +02:00
|
|
|
'disks': disk_names,
|
2013-10-07 23:01:37 +02:00
|
|
|
'machine_type': inst.size,
|
|
|
|
'metadata': metadata,
|
|
|
|
'name': inst.name,
|
|
|
|
'network': netname,
|
2013-11-19 23:30:22 +01:00
|
|
|
'private_ip': inst.private_ips[0],
|
2015-03-03 15:15:45 +01:00
|
|
|
'public_ip': public_ip,
|
2013-10-19 18:57:47 +02:00
|
|
|
'status': ('status' in inst.extra) and inst.extra['status'] or None,
|
|
|
|
'tags': ('tags' in inst.extra) and inst.extra['tags'] or [],
|
|
|
|
'zone': ('zone' in inst.extra) and inst.extra['zone'].name or None,
|
2014-10-01 11:41:17 +02:00
|
|
|
})
|
|
|
|
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
def create_instances(module, gce, instance_names):
|
|
|
|
"""Creates new instances. Attributes other than instance_names are picked
|
|
|
|
up from 'module'
|
|
|
|
|
2013-10-13 22:41:55 +02:00
|
|
|
module : AnsibleModule object
|
2013-10-07 23:01:37 +02:00
|
|
|
gce: authenticated GCE libcloud driver
|
|
|
|
instance_names: python list of instance names to create
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of dictionaries with instance information
|
|
|
|
about the instances that were launched.
|
|
|
|
|
|
|
|
"""
|
|
|
|
image = module.params.get('image')
|
|
|
|
machine_type = module.params.get('machine_type')
|
|
|
|
metadata = module.params.get('metadata')
|
|
|
|
network = module.params.get('network')
|
|
|
|
persistent_boot_disk = module.params.get('persistent_boot_disk')
|
2014-04-13 00:21:26 +02:00
|
|
|
disks = module.params.get('disks')
|
2013-10-07 23:01:37 +02:00
|
|
|
state = module.params.get('state')
|
|
|
|
tags = module.params.get('tags')
|
|
|
|
zone = module.params.get('zone')
|
2015-02-12 23:24:03 +01:00
|
|
|
ip_forward = module.params.get('ip_forward')
|
2015-03-03 15:15:45 +01:00
|
|
|
external_ip = module.params.get('external_ip')
|
2015-03-04 00:49:15 +01:00
|
|
|
disk_auto_delete = module.params.get('disk_auto_delete')
|
2015-05-15 15:28:28 +02:00
|
|
|
service_account_permissions = module.params.get('service_account_permissions')
|
|
|
|
service_account_email = module.params.get('service_account_email')
|
2015-03-03 15:15:45 +01:00
|
|
|
|
|
|
|
if external_ip == "none":
|
|
|
|
external_ip = None
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
new_instances = []
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
lc_image = gce.ex_get_image(image)
|
2014-04-13 00:21:26 +02:00
|
|
|
lc_disks = []
|
|
|
|
disk_modes = []
|
|
|
|
for i, disk in enumerate(disks or []):
|
|
|
|
if isinstance(disk, dict):
|
|
|
|
lc_disks.append(gce.ex_get_volume(disk['name']))
|
|
|
|
disk_modes.append(disk['mode'])
|
|
|
|
else:
|
|
|
|
lc_disks.append(gce.ex_get_volume(disk))
|
|
|
|
# boot disk is implicitly READ_WRITE
|
|
|
|
disk_modes.append('READ_ONLY' if i > 0 else 'READ_WRITE')
|
2013-10-07 23:01:37 +02:00
|
|
|
lc_network = gce.ex_get_network(network)
|
|
|
|
lc_machine_type = gce.ex_get_size(machine_type)
|
|
|
|
lc_zone = gce.ex_get_zone(zone)
|
|
|
|
|
|
|
|
# Try to convert the user's metadata value into the format expected
|
|
|
|
# by GCE. First try to ensure user has proper quoting of a
|
|
|
|
# dictionary-like syntax using 'literal_eval', then convert the python
|
|
|
|
# dict into a python list of 'key' / 'value' dicts. Should end up
|
|
|
|
# with:
|
|
|
|
# [ {'key': key1, 'value': value1}, {'key': key2, 'value': value2}, ...]
|
|
|
|
if metadata:
|
2014-10-01 11:41:17 +02:00
|
|
|
if isinstance(metadata, dict):
|
|
|
|
md = metadata
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
md = literal_eval(str(metadata))
|
|
|
|
if not isinstance(md, dict):
|
|
|
|
raise ValueError('metadata must be a dict')
|
|
|
|
except ValueError as e:
|
|
|
|
module.fail_json(msg='bad metadata: %s' % str(e))
|
|
|
|
except SyntaxError as e:
|
|
|
|
module.fail_json(msg='bad metadata syntax')
|
|
|
|
|
|
|
|
if hasattr(libcloud, '__version__') and libcloud.__version__ < '0.15':
|
2013-10-07 23:01:37 +02:00
|
|
|
items = []
|
2014-10-01 11:41:17 +02:00
|
|
|
for k, v in md.items():
|
|
|
|
items.append({"key": k, "value": v})
|
2013-10-07 23:01:37 +02:00
|
|
|
metadata = {'items': items}
|
2014-10-01 11:41:17 +02:00
|
|
|
else:
|
|
|
|
metadata = md
|
2013-10-07 23:01:37 +02:00
|
|
|
|
2015-05-15 15:28:28 +02:00
|
|
|
ex_sa_perms = []
|
2015-05-15 16:00:24 +02:00
|
|
|
bad_perms = []
|
2015-05-15 15:28:28 +02:00
|
|
|
if service_account_permissions:
|
2015-05-15 16:00:24 +02:00
|
|
|
for perm in service_account_permissions:
|
2014-10-01 11:41:17 +02:00
|
|
|
if perm not in gce.SA_SCOPES_MAP.keys():
|
2015-05-15 16:00:24 +02:00
|
|
|
bad_perms.append(perm)
|
|
|
|
if len(bad_perms) > 0:
|
|
|
|
module.fail_json(msg='bad permissions: %s' % str(bad_perms))
|
2015-05-15 15:28:28 +02:00
|
|
|
if service_account_email:
|
|
|
|
ex_sa_perms.append({'email': service_account_email})
|
|
|
|
else:
|
|
|
|
ex_sa_perms.append({'email': "default"})
|
|
|
|
ex_sa_perms[0]['scopes'] = service_account_permissions
|
|
|
|
|
2013-10-07 23:01:37 +02:00
|
|
|
# These variables all have default values but check just in case
|
|
|
|
if not lc_image or not lc_network or not lc_machine_type or not lc_zone:
|
|
|
|
module.fail_json(msg='Missing required create instance variable',
|
2014-10-01 11:41:17 +02:00
|
|
|
changed=False)
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
for name in instance_names:
|
|
|
|
pd = None
|
2014-04-13 00:21:26 +02:00
|
|
|
if lc_disks:
|
|
|
|
pd = lc_disks[0]
|
|
|
|
elif persistent_boot_disk:
|
2013-10-07 23:01:37 +02:00
|
|
|
try:
|
|
|
|
pd = gce.create_volume(None, "%s" % name, image=lc_image)
|
|
|
|
except ResourceExistsError:
|
|
|
|
pd = gce.ex_get_volume("%s" % name, lc_zone)
|
|
|
|
inst = None
|
|
|
|
try:
|
2014-10-01 11:41:17 +02:00
|
|
|
inst = gce.create_node(
|
|
|
|
name, lc_machine_type, lc_image, location=lc_zone,
|
|
|
|
ex_network=network, ex_tags=tags, ex_metadata=metadata,
|
|
|
|
ex_boot_disk=pd, ex_can_ip_forward=ip_forward,
|
|
|
|
external_ip=external_ip, ex_disk_auto_delete=disk_auto_delete,
|
|
|
|
ex_service_accounts=ex_sa_perms
|
|
|
|
)
|
2013-10-07 23:01:37 +02:00
|
|
|
changed = True
|
|
|
|
except ResourceExistsError:
|
|
|
|
inst = gce.ex_get_node(name, lc_zone)
|
2014-10-01 11:41:17 +02:00
|
|
|
except GoogleBaseError as e:
|
|
|
|
module.fail_json(msg='Unexpected error attempting to create ' +
|
|
|
|
'instance %s, error: %s' % (name, e.value))
|
2013-10-07 23:01:37 +02:00
|
|
|
|
2014-04-13 00:21:26 +02:00
|
|
|
for i, lc_disk in enumerate(lc_disks):
|
|
|
|
# Check whether the disk is already attached
|
|
|
|
if (len(inst.extra['disks']) > i):
|
|
|
|
attached_disk = inst.extra['disks'][i]
|
|
|
|
if attached_disk['source'] != lc_disk.extra['selfLink']:
|
|
|
|
module.fail_json(
|
|
|
|
msg=("Disk at index %d does not match: requested=%s found=%s" % (
|
|
|
|
i, lc_disk.extra['selfLink'], attached_disk['source'])))
|
|
|
|
elif attached_disk['mode'] != disk_modes[i]:
|
|
|
|
module.fail_json(
|
|
|
|
msg=("Disk at index %d is in the wrong mode: requested=%s found=%s" % (
|
|
|
|
i, disk_modes[i], attached_disk['mode'])))
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
gce.attach_volume(inst, lc_disk, ex_mode=disk_modes[i])
|
|
|
|
# Work around libcloud bug: attached volumes don't get added
|
|
|
|
# to the instance metadata. get_instance_info() only cares about
|
|
|
|
# source and index.
|
|
|
|
if len(inst.extra['disks']) != i+1:
|
|
|
|
inst.extra['disks'].append(
|
|
|
|
{'source': lc_disk.extra['selfLink'], 'index': i})
|
|
|
|
|
2013-10-07 23:01:37 +02:00
|
|
|
if inst:
|
|
|
|
new_instances.append(inst)
|
|
|
|
|
|
|
|
instance_names = []
|
|
|
|
instance_json_data = []
|
|
|
|
for inst in new_instances:
|
|
|
|
d = get_instance_info(inst)
|
|
|
|
instance_names.append(d['name'])
|
|
|
|
instance_json_data.append(d)
|
|
|
|
|
|
|
|
return (changed, instance_json_data, instance_names)
|
|
|
|
|
|
|
|
|
|
|
|
def terminate_instances(module, gce, instance_names, zone_name):
|
|
|
|
"""Terminates a list of instances.
|
|
|
|
|
|
|
|
module: Ansible module object
|
|
|
|
gce: authenticated GCE connection object
|
|
|
|
instance_names: a list of instance names to terminate
|
|
|
|
zone_name: the zone where the instances reside prior to termination
|
|
|
|
|
|
|
|
Returns a dictionary of instance names that were terminated.
|
|
|
|
|
|
|
|
"""
|
|
|
|
changed = False
|
|
|
|
terminated_instance_names = []
|
|
|
|
for name in instance_names:
|
|
|
|
inst = None
|
|
|
|
try:
|
|
|
|
inst = gce.ex_get_node(name, zone_name)
|
|
|
|
except ResourceNotFoundError:
|
|
|
|
pass
|
2014-10-01 11:41:17 +02:00
|
|
|
except Exception as e:
|
2013-10-07 23:01:37 +02:00
|
|
|
module.fail_json(msg=unexpected_error_msg(e), changed=False)
|
|
|
|
if inst:
|
|
|
|
gce.destroy_node(inst)
|
|
|
|
terminated_instance_names.append(inst.name)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
return (changed, terminated_instance_names)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
2014-10-01 11:41:17 +02:00
|
|
|
argument_spec=dict(
|
|
|
|
image=dict(default='debian-7'),
|
|
|
|
instance_names=dict(),
|
|
|
|
machine_type=dict(default='n1-standard-1'),
|
|
|
|
metadata=dict(),
|
|
|
|
name=dict(),
|
|
|
|
network=dict(default='default'),
|
|
|
|
persistent_boot_disk=dict(type='bool', default=False),
|
|
|
|
disks=dict(type='list'),
|
|
|
|
state=dict(choices=['active', 'present', 'absent', 'deleted'],
|
|
|
|
default='present'),
|
|
|
|
tags=dict(type='list'),
|
|
|
|
zone=dict(default='us-central1-a'),
|
|
|
|
service_account_email=dict(),
|
|
|
|
service_account_permissions=dict(type='list'),
|
|
|
|
pem_file=dict(),
|
|
|
|
project_id=dict(),
|
|
|
|
ip_forward=dict(type='bool', default=False),
|
|
|
|
external_ip=dict(choices=['ephemeral', 'none'],
|
|
|
|
default='ephemeral'),
|
|
|
|
disk_auto_delete=dict(type='bool', default=True),
|
2013-10-07 23:01:37 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-05-11 21:15:53 +02:00
|
|
|
if not HAS_PYTHON26:
|
|
|
|
module.fail_json(msg="GCE module requires python's 'ast' module, python v2.6+")
|
|
|
|
if not HAS_LIBCLOUD:
|
|
|
|
module.fail_json(msg='libcloud with GCE support (0.13.3+) required for this module')
|
|
|
|
|
2014-03-01 21:56:15 +01:00
|
|
|
gce = gce_connect(module)
|
|
|
|
|
2013-10-07 23:01:37 +02:00
|
|
|
image = module.params.get('image')
|
|
|
|
instance_names = module.params.get('instance_names')
|
|
|
|
machine_type = module.params.get('machine_type')
|
|
|
|
metadata = module.params.get('metadata')
|
|
|
|
name = module.params.get('name')
|
|
|
|
network = module.params.get('network')
|
|
|
|
persistent_boot_disk = module.params.get('persistent_boot_disk')
|
|
|
|
state = module.params.get('state')
|
|
|
|
tags = module.params.get('tags')
|
|
|
|
zone = module.params.get('zone')
|
2015-02-12 23:24:03 +01:00
|
|
|
ip_forward = module.params.get('ip_forward')
|
2013-10-07 23:01:37 +02:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
inames = []
|
|
|
|
if isinstance(instance_names, list):
|
|
|
|
inames = instance_names
|
|
|
|
elif isinstance(instance_names, str):
|
|
|
|
inames = instance_names.split(',')
|
|
|
|
if name:
|
|
|
|
inames.append(name)
|
|
|
|
if not inames:
|
|
|
|
module.fail_json(msg='Must specify a "name" or "instance_names"',
|
2014-10-01 11:41:17 +02:00
|
|
|
changed=False)
|
2013-10-07 23:01:37 +02:00
|
|
|
if not zone:
|
|
|
|
module.fail_json(msg='Must specify a "zone"', changed=False)
|
|
|
|
|
|
|
|
json_output = {'zone': zone}
|
|
|
|
if state in ['absent', 'deleted']:
|
|
|
|
json_output['state'] = 'absent'
|
2014-10-01 11:41:17 +02:00
|
|
|
(changed, terminated_instance_names) = terminate_instances(
|
|
|
|
module, gce, inames, zone)
|
2013-10-07 23:01:37 +02:00
|
|
|
|
|
|
|
# based on what user specified, return the same variable, although
|
|
|
|
# value could be different if an instance could not be destroyed
|
|
|
|
if instance_names:
|
|
|
|
json_output['instance_names'] = terminated_instance_names
|
|
|
|
elif name:
|
|
|
|
json_output['name'] = name
|
|
|
|
|
|
|
|
elif state in ['active', 'present']:
|
|
|
|
json_output['state'] = 'present'
|
2014-10-01 11:41:17 +02:00
|
|
|
(changed, instance_data, instance_name_list) = create_instances(
|
|
|
|
module, gce, inames)
|
2013-10-07 23:01:37 +02:00
|
|
|
json_output['instance_data'] = instance_data
|
|
|
|
if instance_names:
|
|
|
|
json_output['instance_names'] = instance_name_list
|
|
|
|
elif name:
|
|
|
|
json_output['name'] = name
|
|
|
|
|
|
|
|
json_output['changed'] = changed
|
2015-05-11 21:15:53 +02:00
|
|
|
module.exit_json(**json_output)
|
2013-10-07 23:01:37 +02:00
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2014-03-01 21:56:15 +01:00
|
|
|
from ansible.module_utils.gce import *
|
2015-05-11 21:15:53 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|