From 6d1fd1afcc59beaba263dbe607e546b416ddb98c Mon Sep 17 00:00:00 2001 From: Lev Popov Date: Tue, 3 Mar 2015 15:15:45 +0100 Subject: [PATCH] Allow to spawn instances without external ip in google cloud module --- lib/ansible/modules/cloud/google/gce.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gce.py b/lib/ansible/modules/cloud/google/gce.py index 4105baa30f4..e207a4a8ec1 100644 --- a/lib/ansible/modules/cloud/google/gce.py +++ b/lib/ansible/modules/cloud/google/gce.py @@ -122,6 +122,13 @@ options: required: false default: "false" aliases: [] + external_ip: + version_added: "1.9" + description: + - type of external ip, ephemeral by default + required: false + default: "ephemeral" + aliases: [] requirements: [ "libcloud" ] notes: @@ -230,6 +237,12 @@ def get_instance_info(inst): key=lambda disk_info: disk_info['index'])] else: disk_names = [] + + if len(inst.public_ips) == 0: + public_ip = None + else: + public_ip = inst.public_ips[0] + return({ 'image': not inst.image is None and inst.image.split('/')[-1] or None, 'disks': disk_names, @@ -238,7 +251,7 @@ def get_instance_info(inst): 'name': inst.name, 'network': netname, 'private_ip': inst.private_ips[0], - 'public_ip': inst.public_ips[0], + 'public_ip': public_ip, '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, @@ -267,6 +280,10 @@ def create_instances(module, gce, instance_names): tags = module.params.get('tags') zone = module.params.get('zone') ip_forward = module.params.get('ip_forward') + external_ip = module.params.get('external_ip') + + if external_ip == "none": + external_ip = None new_instances = [] changed = False @@ -327,7 +344,8 @@ def create_instances(module, gce, instance_names): try: 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) + ex_metadata=metadata, ex_boot_disk=pd, ex_can_ip_forward=ip_forward, + external_ip=external_ip) changed = True except ResourceExistsError: inst = gce.ex_get_node(name, lc_zone) @@ -418,6 +436,8 @@ def main(): pem_file = dict(), project_id = dict(), ip_forward = dict(type='bool', default=False), + external_ip = dict(choices=['ephemeral', 'none'], + default='ephemeral'), ) )