From b5ae76107041caa5289d2ce0500ca34cd2d0829c Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Fri, 27 Sep 2013 13:21:55 +0200 Subject: [PATCH 1/3] Add support for tags parameter to cloudformation module Expose boto.cloudformation.create_stack() tags parameter. Supplied tags will be applied to stack and all it's resources on stack creation. Cannot be updated later (not supported by UpdateStack CloudFormation API). --- library/cloud/cloudformation | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index ae9f2e0f26..955278406b 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -60,6 +60,12 @@ options: required: true default: null aliases: [] + tags: + description: + - Dictionary of tags to associate with stack and it's resources during stack creation. Cannot be updated later. + required: false + default: null + aliases: [] requirements: [ "boto" ] author: James S. Martin @@ -79,6 +85,8 @@ tasks: DiskType: ephemeral InstanceType: m1.small ClusterSize: 3 + tags: + Stack: ansible-cloudformation ''' import json @@ -163,7 +171,8 @@ def main(): region=dict(aliases=['aws_region', 'ec2_region'], required=True, choices=AWS_REGIONS), state=dict(default='present', choices=['present', 'absent']), template=dict(default=None, required=True), - disable_rollback=dict(default=False) + disable_rollback=dict(default=False), + tags=dict(default=None) ) ) @@ -173,14 +182,15 @@ def main(): template_body = open(module.params['template'], 'r').read() disable_rollback = module.params['disable_rollback'] template_parameters = module.params['template_parameters'] + tags = module.params['tags'] if not r: if 'AWS_REGION' in os.environ: r = os.environ['AWS_REGION'] elif 'EC2_REGION' in os.environ: r = os.environ['EC2_REGION'] - - + + # convert the template parameters ansible passes into a tuple for boto template_parameters_tup = [(k, v) for k, v in template_parameters.items()] @@ -203,7 +213,8 @@ def main(): cfn.create_stack(stack_name, parameters=template_parameters_tup, template_body=template_body, disable_rollback=disable_rollback, - capabilities=['CAPABILITY_IAM']) + capabilities=['CAPABILITY_IAM'], + tags=tags) operation = 'CREATE' except Exception, err: error_msg = boto_exception(err) From d294669dec309a2a425e53221d0a314c617d4b3c Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Mon, 30 Sep 2013 09:25:20 +0200 Subject: [PATCH 2/3] Add Boto version check for tags parameter of cloudformation module Tags parameter requires at least version 2.6.0 of Boto module. When tags parameter is used with older version, error is raised. When tags parameter is unused, module works as before. --- library/cloud/cloudformation | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index 955278406b..164f306854 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -63,6 +63,7 @@ options: tags: description: - Dictionary of tags to associate with stack and it's resources during stack creation. Cannot be updated later. + Requires at least Boto version 2.6.0. required: false default: null aliases: [] @@ -93,6 +94,7 @@ import json import time try: + import boto import boto.cloudformation.connection except ImportError: print "failed=True msg='boto required for this module'" @@ -126,6 +128,17 @@ def boto_exception(err): return error +def boto_version_required(version_tuple): + parts = boto.Version.split('.') + boto_version = [] + try: + for part in parts: + boto_version.append(int(part)) + except: + boto_version.append(-1) + return tuple(boto_version) >= tuple(version_tuple) + + def stack_operation(cfn, stack_name, operation): '''gets the status of a stack while it is created/updated/deleted''' existed = [] @@ -190,6 +203,11 @@ def main(): elif 'EC2_REGION' in os.environ: r = os.environ['EC2_REGION'] + kwargs = dict() + if tags is not None: + if not boto_version_required((2,6,0)): + module.fail_json(msg='Module parameter "tags" requires at least Boto version 2.6.0') + kwargs['tags'] = tags # convert the template parameters ansible passes into a tuple for boto @@ -214,7 +232,7 @@ def main(): template_body=template_body, disable_rollback=disable_rollback, capabilities=['CAPABILITY_IAM'], - tags=tags) + **kwargs) operation = 'CREATE' except Exception, err: error_msg = boto_exception(err) From 5e4fff98f2da76cf4499c76809976c3d06e6dc10 Mon Sep 17 00:00:00 2001 From: Petr Svoboda Date: Mon, 30 Sep 2013 09:28:53 +0200 Subject: [PATCH 3/3] Add version_added to cloudformation tags parameter --- library/cloud/cloudformation | 1 + 1 file changed, 1 insertion(+) diff --git a/library/cloud/cloudformation b/library/cloud/cloudformation index 164f306854..8c80fa75c1 100644 --- a/library/cloud/cloudformation +++ b/library/cloud/cloudformation @@ -67,6 +67,7 @@ options: required: false default: null aliases: [] + version_added: "1.4" requirements: [ "boto" ] author: James S. Martin