From 05c8630b48b1f417168159cce4892b55aa0abb0c Mon Sep 17 00:00:00 2001 From: Gilles Gagniard Date: Thu, 8 Sep 2016 21:12:20 +0200 Subject: [PATCH] GCE : Fix image family handling with libcloud > 0.20.1 (#2289) * fix image family handling with libcloud > 0.20.1 * add missing import --- .../modules/extras/cloud/google/gce_img.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/extras/cloud/google/gce_img.py b/lib/ansible/modules/extras/cloud/google/gce_img.py index bf3d9da5356..32c3d238ca3 100644 --- a/lib/ansible/modules/extras/cloud/google/gce_img.py +++ b/lib/ansible/modules/extras/cloud/google/gce_img.py @@ -38,6 +38,12 @@ options: - an optional description required: false default: null + family: + description: + - an optional family name + required: false + default: null + version_added: "2.2" source: description: - the source disk or the Google Cloud Storage URI to create the image from @@ -108,6 +114,7 @@ EXAMPLES = ''' import sys try: + import libcloud from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver from libcloud.common.google import GoogleBaseError @@ -128,6 +135,7 @@ def create_image(gce, name, module): zone = module.params.get('zone') desc = module.params.get('description') timeout = module.params.get('timeout') + family = module.params.get('family') if not source: module.fail_json(msg='Must supply a source', changed=False) @@ -147,10 +155,14 @@ def create_image(gce, name, module): except GoogleBaseError, e: module.fail_json(msg=str(e), changed=False) + gce_extra_args = {} + if family is not None: + gce_extra_args['family'] = family + old_timeout = gce.connection.timeout try: gce.connection.timeout = timeout - gce.ex_create_image(name, volume, desc, False) + gce.ex_create_image(name, volume, desc, use_existing=False, **gce_extra_args) return True except ResourceExistsError: return False @@ -175,6 +187,7 @@ def main(): module = AnsibleModule( argument_spec=dict( name=dict(required=True), + family=dict(), description=dict(), source=dict(), state=dict(default='present', choices=['present', 'absent']), @@ -193,8 +206,13 @@ def main(): name = module.params.get('name') state = module.params.get('state') + family = module.params.get('family') changed = False + if family is not None and hasattr(libcloud, '__version__') and libcloud.__version__ <= '0.20.1': + module.fail_json(msg="Apache Libcloud 1.0.0+ is required to use 'family' option", + changed=False) + # user wants to create an image. if state == 'present': changed = create_image(gce, name, module)