Created
May 19, 2015 18:24
-
-
Save mattupstate/ac26c3c4b79c3779abc1 to your computer and use it in GitHub Desktop.
Ansible module to gather a Cloudformation stack outputs into variable scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
try: | |
import boto | |
from boto import cloudformation | |
HAS_BOTO = True | |
except ImportError: | |
HAS_BOTO = False | |
def boto_exception(err): | |
'''generic error message handler''' | |
if hasattr(err, 'error_message'): | |
error = err.error_message | |
elif hasattr(err, 'message'): | |
error = err.message | |
else: | |
error = '%s: %s' % (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 main(): | |
argument_spec = ec2_argument_spec() | |
argument_spec.update(dict( | |
stack_name=dict(required=True), | |
var=dict(required=True) | |
)) | |
module = AnsibleModule(argument_spec=argument_spec) | |
if not HAS_BOTO: | |
module.fail_json(msg='boto required for this module') | |
stack_name = module.params['stack_name'] | |
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module) | |
try: | |
connection = cloudformation.connect_to_region(region, **aws_connect_kwargs) | |
stack = connection.describe_stacks(stack_name_or_id=stack_name)[0] | |
except Exception as err: | |
module.fail_json(msg=boto_exception(err)) | |
stack_output_facts = { | |
module.params['var']: { | |
item.key: item.value | |
for item in stack.outputs | |
} | |
} | |
stack_outputs_result = dict(changed=False, ansible_facts=stack_output_facts) | |
module.exit_json(**stack_outputs_result) | |
# import module snippets | |
from ansible.module_utils.basic import * | |
from ansible.module_utils.ec2 import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment