Add parameter to ec2 module to control instance shutdown behavior (stop|terminate)

This commit is contained in:
Richard Adams 2016-04-05 13:40:34 +01:00 committed by Matt Clay
parent 9b7e866964
commit b0135fadea

View file

@ -203,6 +203,13 @@ options:
required: false
default: no
choices: [ "yes", "no" ]
instance_initiated_shutdown_behavior:
version_added: "2.2"
description:
- Set whether AWS will Stop or Terminate an instance on shutdown
required: false
default: 'stop'
choices: [ "stop", "terminate" ]
state:
version_added: "1.3"
description:
@ -889,6 +896,7 @@ def create_instances(module, ec2, vpc, override_count=None):
termination_protection = module.boolean(module.params.get('termination_protection'))
network_interfaces = module.params.get('network_interfaces')
spot_launch_group = module.params.get('spot_launch_group')
instance_initiated_shutdown_behavior = module.params.get('instance_initiated_shutdown_behavior')
# group_id and group_name are exclusive of each other
if group_id and group_name:
@ -1040,6 +1048,9 @@ def create_instances(module, ec2, vpc, override_count=None):
private_ip_address = private_ip,
))
# Spot instances do not support start/stop thereby not having the option to change shutdown behavior
params['instance_initiated_shutdown_behavior'] = instance_initiated_shutdown_behavior
res = ec2.run_instances(**params)
instids = [ i.id for i in res.instances ]
while True:
@ -1074,6 +1085,12 @@ def create_instances(module, ec2, vpc, override_count=None):
module.fail_json(
msg="placement_group parameter requires Boto version 2.3.0 or higher.")
if boto_supports_param_in_spot_request(ec2, 'instance_initiated_shutdown_behavior'):
params['instance_initiated_shutdown_behavior'] = instance_initiated_shutdown_behavior
elif instance_initiated_shutdown_behavior:
module.fail_json(
msg="instance_initiated_shutdown_behavior parameter is not supported by your Boto version.")
if spot_launch_group and isinstance(spot_launch_group, basestring):
params['launch_group'] = spot_launch_group
@ -1435,6 +1452,7 @@ def main():
source_dest_check = dict(type='bool', default=True),
termination_protection = dict(type='bool', default=False),
state = dict(default='present', choices=['present', 'absent', 'running', 'restarted', 'stopped']),
instance_initiated_shutdown_behavior=dict(default='stop', choices=['stop', 'terminate']),
exact_count = dict(type='int', default=None),
count_tag = dict(),
volumes = dict(type='list'),