2013-03-19 16:07:36 +00:00
|
|
|
#!/usr/bin/python
|
2013-02-22 20:52:23 +00:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: cloudformation
|
|
|
|
short_description: create a AWS CloudFormation stack
|
|
|
|
description:
|
|
|
|
- Launches an AWS CloudFormation stack and waits for it complete.
|
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
stack_name:
|
|
|
|
description:
|
|
|
|
- name of the cloudformation stack
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
disable_rollback:
|
|
|
|
description:
|
|
|
|
- If a stacks fails to form, rollback will remove the stack
|
|
|
|
required: false
|
2013-03-12 12:18:12 +00:00
|
|
|
default: "no"
|
|
|
|
choices: [ "yes", "no" ]
|
2013-02-22 20:52:23 +00:00
|
|
|
aliases: []
|
|
|
|
template_parameters:
|
|
|
|
description:
|
|
|
|
- a list of hashes of all the template variables for the stack
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
region:
|
|
|
|
description:
|
2013-08-13 13:30:56 +00:00
|
|
|
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
|
2013-02-22 20:52:23 +00:00
|
|
|
required: true
|
|
|
|
default: null
|
2013-08-13 13:30:56 +00:00
|
|
|
aliases: ['aws_region', 'ec2_region']
|
2013-02-22 20:52:23 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- If state is "present", stack will be created. If state is "present" and if stack exists and template has changed, it will be updated.
|
2013-04-01 19:15:40 +00:00
|
|
|
If state is absent, stack will be removed.
|
2013-02-22 20:52:23 +00:00
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
template:
|
|
|
|
description:
|
|
|
|
- the path of the cloudformation template
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-06-28 22:25:17 +00:00
|
|
|
|
2013-04-26 02:22:48 +00:00
|
|
|
requirements: [ "boto" ]
|
|
|
|
author: James S. Martin
|
|
|
|
'''
|
2013-02-22 20:52:23 +00:00
|
|
|
|
2013-04-26 02:22:48 +00:00
|
|
|
EXAMPLES = '''
|
2013-06-14 09:53:43 +00:00
|
|
|
# Basic task example
|
|
|
|
tasks:
|
|
|
|
- name: launch ansible cloudformation example
|
|
|
|
action: cloudformation >
|
|
|
|
stack_name="ansible-cloudformation" state=present
|
|
|
|
region=us-east-1 disable_rollback=yes
|
|
|
|
template=files/cloudformation-example.json
|
|
|
|
args:
|
|
|
|
template_parameters:
|
|
|
|
KeyName: jmartin
|
|
|
|
DiskType: ephemeral
|
|
|
|
InstanceType: m1.small
|
|
|
|
ClusterSize: 3
|
2013-02-22 20:52:23 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
import json
|
2013-06-28 22:25:17 +00:00
|
|
|
import time
|
2013-02-22 20:52:23 +00:00
|
|
|
|
2013-08-13 13:30:56 +00:00
|
|
|
try:
|
|
|
|
import boto.cloudformation.connection
|
|
|
|
except ImportError:
|
|
|
|
print "failed=True msg='boto required for this module'"
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
AWS_REGIONS = ['ap-northeast-1',
|
|
|
|
'ap-southeast-1',
|
|
|
|
'ap-southeast-2',
|
|
|
|
'eu-west-1',
|
|
|
|
'sa-east-1',
|
|
|
|
'us-east-1',
|
|
|
|
'us-west-1',
|
|
|
|
'us-west-2']
|
|
|
|
|
2013-02-22 20:52:23 +00:00
|
|
|
class Region:
|
|
|
|
def __init__(self, region):
|
2013-06-28 22:25:17 +00:00
|
|
|
'''connects boto to the region specified in the cloudformation template'''
|
2013-02-22 20:52:23 +00:00
|
|
|
self.name = region
|
|
|
|
self.endpoint = 'cloudformation.%s.amazonaws.com' % region
|
|
|
|
|
|
|
|
|
|
|
|
def boto_exception(err):
|
2013-06-28 22:25:17 +00:00
|
|
|
'''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)
|
2013-02-22 20:52:23 +00:00
|
|
|
|
2013-06-28 22:25:17 +00:00
|
|
|
return error
|
2013-02-22 20:52:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def stack_operation(cfn, stack_name, operation):
|
2013-06-28 22:25:17 +00:00
|
|
|
'''gets the status of a stack while it is created/updated/deleted'''
|
2013-02-22 20:52:23 +00:00
|
|
|
existed = []
|
|
|
|
result = {}
|
|
|
|
operation_complete = False
|
|
|
|
while operation_complete == False:
|
|
|
|
try:
|
|
|
|
stack = cfn.describe_stacks(stack_name)[0]
|
|
|
|
existed.append('yes')
|
|
|
|
except:
|
|
|
|
if 'yes' in existed:
|
2013-06-28 22:25:17 +00:00
|
|
|
result = dict(changed=True,
|
|
|
|
output='Stack Deleted',
|
|
|
|
events=map(str, list(stack.describe_events())))
|
2013-02-22 20:52:23 +00:00
|
|
|
else:
|
2013-06-30 22:50:40 +00:00
|
|
|
result = dict(changed= True, output='Stack Not Found')
|
2013-02-22 20:52:23 +00:00
|
|
|
break
|
|
|
|
if '%s_COMPLETE' % operation == stack.stack_status:
|
2013-06-28 22:25:17 +00:00
|
|
|
result = dict(changed=True,
|
|
|
|
events = map(str, list(stack.describe_events())),
|
2013-06-30 22:50:40 +00:00
|
|
|
output = 'Stack %s complete' % operation)
|
2013-06-28 22:25:17 +00:00
|
|
|
break
|
2013-09-30 13:15:19 +00:00
|
|
|
if 'ROLLBACK_COMPLETE' == stack.stack_status or '%s_ROLLBACK_COMPLETE' % operation == stack.stack_status:
|
2013-06-28 22:25:17 +00:00
|
|
|
result = dict(changed=True,
|
|
|
|
events = map(str, list(stack.describe_events())),
|
2013-06-30 22:50:40 +00:00
|
|
|
output = 'Problem with %s. Rollback complete' % operation)
|
2013-02-22 20:52:23 +00:00
|
|
|
break
|
|
|
|
elif '%s_FAILED' % operation == stack.stack_status:
|
2013-06-28 22:25:17 +00:00
|
|
|
result = dict(changed=False,
|
|
|
|
events = map(str, list(stack.describe_events())),
|
2013-06-30 22:50:40 +00:00
|
|
|
output = 'Stack %s failed' % operation)
|
2013-02-22 20:52:23 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(5)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
stack_name=dict(required=True),
|
2013-08-27 21:09:30 +00:00
|
|
|
template_parameters=dict(required=True),
|
2013-08-13 13:30:56 +00:00
|
|
|
region=dict(aliases=['aws_region', 'ec2_region'], required=True, choices=AWS_REGIONS),
|
2013-02-22 20:52:23 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
template=dict(default=None, required=True),
|
2013-06-28 22:25:17 +00:00
|
|
|
disable_rollback=dict(default=False)
|
2013-02-22 20:52:23 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
stack_name = module.params['stack_name']
|
2013-08-13 13:30:56 +00:00
|
|
|
r = module.params['region']
|
2013-02-22 20:52:23 +00:00
|
|
|
template_body = open(module.params['template'], 'r').read()
|
|
|
|
disable_rollback = module.params['disable_rollback']
|
|
|
|
template_parameters = module.params['template_parameters']
|
|
|
|
|
2013-08-13 13:30:56 +00:00
|
|
|
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']
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-28 22:25:17 +00:00
|
|
|
# convert the template parameters ansible passes into a tuple for boto
|
2013-02-22 20:52:23 +00:00
|
|
|
template_parameters_tup = [(k, v) for k, v in template_parameters.items()]
|
|
|
|
stack_outputs = {}
|
|
|
|
|
|
|
|
try:
|
2013-08-13 13:30:56 +00:00
|
|
|
region = Region(r)
|
2013-02-22 20:52:23 +00:00
|
|
|
cfn = boto.cloudformation.connection.CloudFormationConnection(
|
|
|
|
region=region)
|
|
|
|
except boto.exception.NoAuthHandlerFound, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
update = False
|
|
|
|
result = {}
|
|
|
|
operation = None
|
|
|
|
|
2013-06-28 22:25:17 +00:00
|
|
|
# if state is present we are going to ensure that the stack is either
|
|
|
|
# created or updated
|
2013-02-22 20:52:23 +00:00
|
|
|
if state == 'present':
|
|
|
|
try:
|
|
|
|
cfn.create_stack(stack_name, parameters=template_parameters_tup,
|
|
|
|
template_body=template_body,
|
|
|
|
disable_rollback=disable_rollback,
|
|
|
|
capabilities=['CAPABILITY_IAM'])
|
|
|
|
operation = 'CREATE'
|
|
|
|
except Exception, err:
|
|
|
|
error_msg = boto_exception(err)
|
2013-06-28 22:25:17 +00:00
|
|
|
if 'AlreadyExistsException' in error_msg:
|
2013-02-22 20:52:23 +00:00
|
|
|
update = True
|
|
|
|
else:
|
2013-06-28 22:25:17 +00:00
|
|
|
module.fail_json(msg=error_msg)
|
2013-02-22 20:52:23 +00:00
|
|
|
if not update:
|
|
|
|
result = stack_operation(cfn, stack_name, operation)
|
2013-06-28 22:25:17 +00:00
|
|
|
|
|
|
|
# if the state is present and the stack already exists, we try to update it
|
|
|
|
# AWS will tell us if the stack template and parameters are the same and
|
|
|
|
# don't need to be updated.
|
2013-02-22 20:52:23 +00:00
|
|
|
if update:
|
|
|
|
try:
|
|
|
|
cfn.update_stack(stack_name, parameters=template_parameters_tup,
|
|
|
|
template_body=template_body,
|
|
|
|
disable_rollback=disable_rollback,
|
|
|
|
capabilities=['CAPABILITY_IAM'])
|
|
|
|
operation = 'UPDATE'
|
|
|
|
except Exception, err:
|
|
|
|
error_msg = boto_exception(err)
|
2013-06-28 22:25:17 +00:00
|
|
|
if 'No updates are to be performed.' in error_msg:
|
|
|
|
result = dict(changed=False, output='Stack is already up-to-date.')
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=error_msg)
|
|
|
|
|
2013-02-22 20:52:23 +00:00
|
|
|
if operation == 'UPDATE':
|
|
|
|
result = stack_operation(cfn, stack_name, operation)
|
|
|
|
|
2013-06-28 22:25:17 +00:00
|
|
|
# check the status of the stack while we are creating/updating it.
|
|
|
|
# and get the outputs of the stack
|
|
|
|
|
2013-02-22 20:52:23 +00:00
|
|
|
if state == 'present' or update:
|
|
|
|
stack = cfn.describe_stacks(stack_name)[0]
|
|
|
|
for output in stack.outputs:
|
2013-06-28 22:25:17 +00:00
|
|
|
stack_outputs[output.key] = output.value
|
2013-02-22 20:52:23 +00:00
|
|
|
result['stack_outputs'] = stack_outputs
|
|
|
|
|
2013-06-28 22:25:17 +00:00
|
|
|
# absent state is different because of the way delete_stack works.
|
|
|
|
# problem is it it doesn't give an error if stack isn't found
|
|
|
|
# so must describe the stack first
|
2013-02-22 20:52:23 +00:00
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
try:
|
|
|
|
cfn.describe_stacks(stack_name)
|
|
|
|
operation = 'DELETE'
|
|
|
|
except Exception, err:
|
|
|
|
error_msg = boto_exception(err)
|
2013-06-28 22:25:17 +00:00
|
|
|
if 'Stack:%s does not exist' % stack_name in error_msg:
|
|
|
|
result = dict(changed=False, output='Stack not found.')
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=error_msg)
|
2013-02-22 20:52:23 +00:00
|
|
|
if operation == 'DELETE':
|
|
|
|
cfn.delete_stack(stack_name)
|
|
|
|
result = stack_operation(cfn, stack_name, operation)
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|