Merge pull request #205 from amandolo/ec2_module
AWS "EC2 instance" termination_protection and source_dest_check changeability at run-time + block_device_mapping output
This commit is contained in:
commit
5f6128a300
1 changed files with 52 additions and 14 deletions
|
@ -190,6 +190,13 @@ options:
|
||||||
required: false
|
required: false
|
||||||
default: yes
|
default: yes
|
||||||
choices: [ "yes", "no" ]
|
choices: [ "yes", "no" ]
|
||||||
|
termination_protection:
|
||||||
|
version_added: "2.0"
|
||||||
|
description:
|
||||||
|
- Enable or Disable the Termination Protection
|
||||||
|
required: false
|
||||||
|
default: no
|
||||||
|
choices: [ "yes", "no" ]
|
||||||
state:
|
state:
|
||||||
version_added: "1.3"
|
version_added: "1.3"
|
||||||
description:
|
description:
|
||||||
|
@ -612,6 +619,19 @@ def get_instance_info(inst):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
instance_info['ebs_optimized'] = False
|
instance_info['ebs_optimized'] = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
bdm_dict = {}
|
||||||
|
bdm = getattr(inst, 'block_device_mapping')
|
||||||
|
for device_name in bdm.keys():
|
||||||
|
bdm_dict[device_name] = {
|
||||||
|
'status': bdm[device_name].status,
|
||||||
|
'volume_id': bdm[device_name].volume_id,
|
||||||
|
'delete_on_termination': bdm[device_name].delete_on_termination
|
||||||
|
}
|
||||||
|
instance_info['block_device_mapping'] = bdm_dict
|
||||||
|
except AttributeError:
|
||||||
|
instance_info['block_device_mapping'] = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
instance_info['tenancy'] = getattr(inst, 'placement_tenancy')
|
instance_info['tenancy'] = getattr(inst, 'placement_tenancy')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -786,6 +806,7 @@ def create_instances(module, ec2, vpc, override_count=None):
|
||||||
exact_count = module.params.get('exact_count')
|
exact_count = module.params.get('exact_count')
|
||||||
count_tag = module.params.get('count_tag')
|
count_tag = module.params.get('count_tag')
|
||||||
source_dest_check = module.boolean(module.params.get('source_dest_check'))
|
source_dest_check = module.boolean(module.params.get('source_dest_check'))
|
||||||
|
termination_protection = module.boolean(module.params.get('termination_protection'))
|
||||||
|
|
||||||
# group_id and group_name are exclusive of each other
|
# group_id and group_name are exclusive of each other
|
||||||
if group_id and group_name:
|
if group_id and group_name:
|
||||||
|
@ -1014,11 +1035,16 @@ def create_instances(module, ec2, vpc, override_count=None):
|
||||||
for res in res_list:
|
for res in res_list:
|
||||||
running_instances.extend(res.instances)
|
running_instances.extend(res.instances)
|
||||||
|
|
||||||
# Enabled by default by Amazon
|
# Enabled by default by AWS
|
||||||
if not source_dest_check:
|
if source_dest_check is False:
|
||||||
for inst in res.instances:
|
for inst in res.instances:
|
||||||
inst.modify_attribute('sourceDestCheck', False)
|
inst.modify_attribute('sourceDestCheck', False)
|
||||||
|
|
||||||
|
# Disabled by default by AWS
|
||||||
|
if termination_protection is True:
|
||||||
|
for inst in res.instances:
|
||||||
|
inst.modify_attribute('disableApiTermination', True)
|
||||||
|
|
||||||
# Leave this as late as possible to try and avoid InvalidInstanceID.NotFound
|
# Leave this as late as possible to try and avoid InvalidInstanceID.NotFound
|
||||||
if instance_tags:
|
if instance_tags:
|
||||||
try:
|
try:
|
||||||
|
@ -1135,21 +1161,32 @@ def startstop_instances(module, ec2, instance_ids, state):
|
||||||
if not isinstance(instance_ids, list) or len(instance_ids) < 1:
|
if not isinstance(instance_ids, list) or len(instance_ids) < 1:
|
||||||
module.fail_json(msg='instance_ids should be a list of instances, aborting')
|
module.fail_json(msg='instance_ids should be a list of instances, aborting')
|
||||||
|
|
||||||
# Check that our instances are not in the state we want to take them to
|
# Check (and eventually change) instances attributes and instances state
|
||||||
# and change them to our desired state
|
|
||||||
running_instances_array = []
|
running_instances_array = []
|
||||||
for res in ec2.get_all_instances(instance_ids):
|
for res in ec2.get_all_instances(instance_ids):
|
||||||
for inst in res.instances:
|
for inst in res.instances:
|
||||||
if inst.state != state:
|
|
||||||
instance_dict_array.append(get_instance_info(inst))
|
# Check "source_dest_check" attribute
|
||||||
try:
|
if inst.get_attribute('sourceDestCheck')['sourceDestCheck'] != source_dest_check:
|
||||||
if state == 'running':
|
inst.modify_attribute('sourceDestCheck', source_dest_check)
|
||||||
inst.start()
|
changed = True
|
||||||
else:
|
|
||||||
inst.stop()
|
# Check "termination_protection" attribute
|
||||||
except EC2ResponseError, e:
|
if inst.get_attribute('disableApiTermination')['disableApiTermination'] != termination_protection:
|
||||||
module.fail_json(msg='Unable to change state for instance {0}, error: {1}'.format(inst.id, e))
|
inst.modify_attribute('disableApiTermination', termination_protection)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
# Check instance state
|
||||||
|
if inst.state != state:
|
||||||
|
instance_dict_array.append(get_instance_info(inst))
|
||||||
|
try:
|
||||||
|
if state == 'running':
|
||||||
|
inst.start()
|
||||||
|
else:
|
||||||
|
inst.stop()
|
||||||
|
except EC2ResponseError, e:
|
||||||
|
module.fail_json(msg='Unable to change state for instance {0}, error: {1}'.format(inst.id, e))
|
||||||
|
changed = True
|
||||||
|
|
||||||
## Wait for all the instances to finish starting or stopping
|
## Wait for all the instances to finish starting or stopping
|
||||||
wait_timeout = time.time() + wait_timeout
|
wait_timeout = time.time() + wait_timeout
|
||||||
|
@ -1200,6 +1237,7 @@ def main():
|
||||||
instance_profile_name = dict(),
|
instance_profile_name = dict(),
|
||||||
instance_ids = dict(type='list', aliases=['instance_id']),
|
instance_ids = dict(type='list', aliases=['instance_id']),
|
||||||
source_dest_check = dict(type='bool', default=True),
|
source_dest_check = dict(type='bool', default=True),
|
||||||
|
termination_protection = dict(type='bool', default=False),
|
||||||
state = dict(default='present', choices=['present', 'absent', 'running', 'stopped']),
|
state = dict(default='present', choices=['present', 'absent', 'running', 'stopped']),
|
||||||
exact_count = dict(type='int', default=None),
|
exact_count = dict(type='int', default=None),
|
||||||
count_tag = dict(),
|
count_tag = dict(),
|
||||||
|
|
Loading…
Reference in a new issue