From f654bd45f8c45f305e7ce2f08899ede165835bf0 Mon Sep 17 00:00:00 2001 From: daniel-sc Date: Sat, 10 Oct 2015 16:22:05 +0200 Subject: [PATCH] Allowing specifying ip by address --- lib/ansible/modules/cloud/google/gce.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/google/gce.py b/lib/ansible/modules/cloud/google/gce.py index 08333852831..4016f172b28 100644 --- a/lib/ansible/modules/cloud/google/gce.py +++ b/lib/ansible/modules/cloud/google/gce.py @@ -150,7 +150,7 @@ options: external_ip: version_added: "1.9" description: - - type of external ip, ephemeral by default; alternatively, a list of fixed gce ip names can be given (if there is not enough specified ip, 'ephemeral' will be used) + - 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) required: false default: "ephemeral" aliases: [] @@ -258,12 +258,15 @@ EXAMPLES = ''' ''' +import socket + try: import libcloud from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver from libcloud.common.google import GoogleBaseError, QuotaExceededError, \ ResourceExistsError, ResourceInUseError, ResourceNotFoundError + from libcloud.compute.drivers.gce import GCEAddress _ = Provider.GCE HAS_LIBCLOUD = True except ImportError: @@ -350,7 +353,16 @@ def create_instances(module, gce, instance_names): instance_external_ip = None elif not isinstance(external_ip, basestring): try: - instance_external_ip = gce.ex_get_address(external_ip.pop(0) if len(external_ip) != 0 else 'ephemeral') + if len(external_ip) != 0: + instance_external_ip = external_ip.pop(0) + # check if instance_external_ip is an ip or a name + try: + socket.inet_aton(instance_external_ip) + instance_external_ip = GCEAddress(id='unknown', name='unknown', address=instance_external_ip, region='unknown', driver=gce) + except socket.error: + instance_external_ip = gce.ex_get_address(instance_external_ip) + else: + instance_external_ip = 'ephemeral' except GoogleBaseError, e: module.fail_json(msg='Unexpected error attempting to get a static ip %s, error: %s' % (external_ip, e.value)) else: