Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Cloudformation's parameter attribute UsePreviousValue #31775

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions lib/ansible/modules/cloud/amazon/cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
choices: [ "true", "false" ]
template_parameters:
description:
- a list of hashes of all the template variables for the stack
- a list of hashes of all the template variables for the stack. The value can be a string or a dict.
Dict can be used to set additional template parameter attributes like UsePreviousValue (see example)
required: false
default: {}
state:
Expand Down Expand Up @@ -198,6 +199,24 @@
tags:
Stack: ansible-cloudformation

# Pass a template parameter which uses Cloudformation's UsePreviousValue attribute
# When use_previous_value is set to True, the given value will be ignored and
# Cloudformation will use the value from a previously submitted template.
# If use_previous_value is set to False (default) the given value is used.
- cloudformation:
stack_name: "ansible-cloudformation"
state: "present"
region: "us-east-1"
template: "files/cloudformation-example.json"
template_parameters:
DBSnapshotIdentifier:
use_previous_value: True
value: arn:aws:rds:es-east-1:000000000000:snapshot:rds:my-db-snapshot
DBName:
use_previous_value: True
tags:
Stack: "ansible-cloudformation"

# Enable termination protection on a stack.
# If the stack already exists, this will update its termination protection
- name: enable termination protection during stack creation
Expand Down Expand Up @@ -586,7 +605,24 @@ def main():
stack_params['StackPolicyBody'] = open(module.params['stack_policy'], 'r').read()

template_parameters = module.params['template_parameters']
stack_params['Parameters'] = [{'ParameterKey': k, 'ParameterValue': str(v)} for k, v in template_parameters.items()]

stack_params['Parameters'] = []
for k, v in template_parameters.items():
if isinstance(v, dict):
# set parameter based on a dict to allow additional CFN Parameter Attributes
param = dict(ParameterKey=k)

if 'value' in v:
param['ParameterValue'] = str(v['value'])

if 'use_previous_value' in v and bool(v['use_previous_value']):
param['UsePreviousValue'] = True
param.pop('ParameterValue', None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this pattern 👍


stack_params['Parameters'].append(param)
else:
# allow default k/v configuration to set a template parameter
stack_params['Parameters'].append({'ParameterKey': k, 'ParameterValue': str(v)})

if isinstance(module.params.get('tags'), dict):
stack_params['Tags'] = ansible.module_utils.ec2.ansible_dict_to_boto3_tag_list(module.params['tags'])
Expand Down