|
|
@ -142,7 +142,7 @@ options:
|
|
|
|
instance_tags:
|
|
|
|
instance_tags:
|
|
|
|
version_added: "1.0"
|
|
|
|
version_added: "1.0"
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- a hash/dictionary of tags to add to the new instance; '{"key":"value"}' and '{"key":"value","key":"value"}'
|
|
|
|
- a hash/dictionary of tags to add to the new instance or for for starting/stopping instance by tag; '{"key":"value"}' and '{"key":"value","key":"value"}'
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
aliases: []
|
|
|
@ -443,6 +443,15 @@ EXAMPLES = '''
|
|
|
|
vpc_subnet_id: subnet-29e63245
|
|
|
|
vpc_subnet_id: subnet-29e63245
|
|
|
|
assign_public_ip: yes
|
|
|
|
assign_public_ip: yes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
# Start stopped instances specified by tag
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
- local_action:
|
|
|
|
|
|
|
|
module: ec2
|
|
|
|
|
|
|
|
instance_tags:
|
|
|
|
|
|
|
|
Name: ExtraPower
|
|
|
|
|
|
|
|
state: running
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Enforce that 5 instances with a tag "foo" are running
|
|
|
|
# Enforce that 5 instances with a tag "foo" are running
|
|
|
|
# (Highly recommended!)
|
|
|
|
# (Highly recommended!)
|
|
|
@ -1143,7 +1152,7 @@ def terminate_instances(module, ec2, instance_ids):
|
|
|
|
return (changed, instance_dict_array, terminated_instance_ids)
|
|
|
|
return (changed, instance_dict_array, terminated_instance_ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def startstop_instances(module, ec2, instance_ids, state):
|
|
|
|
def startstop_instances(module, ec2, instance_ids, state, instance_tags):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Starts or stops a list of existing instances
|
|
|
|
Starts or stops a list of existing instances
|
|
|
|
|
|
|
|
|
|
|
@ -1151,6 +1160,8 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|
|
|
ec2: authenticated ec2 connection object
|
|
|
|
ec2: authenticated ec2 connection object
|
|
|
|
instance_ids: The list of instances to start in the form of
|
|
|
|
instance_ids: The list of instances to start in the form of
|
|
|
|
[ {id: <inst-id>}, ..]
|
|
|
|
[ {id: <inst-id>}, ..]
|
|
|
|
|
|
|
|
instance_tags: A dict of tag keys and values in the form of
|
|
|
|
|
|
|
|
{key: value, ... }
|
|
|
|
state: Intended state ("running" or "stopped")
|
|
|
|
state: Intended state ("running" or "stopped")
|
|
|
|
|
|
|
|
|
|
|
|
Returns a dictionary of instance information
|
|
|
|
Returns a dictionary of instance information
|
|
|
@ -1159,6 +1170,8 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|
|
|
If the instance was not able to change state,
|
|
|
|
If the instance was not able to change state,
|
|
|
|
"changed" will be set to False.
|
|
|
|
"changed" will be set to False.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note that if instance_ids and instance_tags are both non-empty,
|
|
|
|
|
|
|
|
this method will process the intersection of the two
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
wait = module.params.get('wait')
|
|
|
|
wait = module.params.get('wait')
|
|
|
@ -1167,11 +1180,23 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|
|
|
instance_dict_array = []
|
|
|
|
instance_dict_array = []
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
# Fail unless the user defined instance tags
|
|
|
|
|
|
|
|
if not instance_tags:
|
|
|
|
|
|
|
|
module.fail_json(msg='instance_ids should be a list of instances, aborting')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# To make an EC2 tag filter, we need to prepend 'tag:' to each key.
|
|
|
|
|
|
|
|
# An empty filter does no filtering, so it's safe to pass it to the
|
|
|
|
|
|
|
|
# get_all_instances method even if the user did not specify instance_tags
|
|
|
|
|
|
|
|
filters = {}
|
|
|
|
|
|
|
|
if instance_tags:
|
|
|
|
|
|
|
|
for key, value in instance_tags.items():
|
|
|
|
|
|
|
|
filters["tag:" + key] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Check that our instances are not in the state we want to take
|
|
|
|
|
|
|
|
|
|
|
|
# Check (and eventually change) instances attributes and instances state
|
|
|
|
# Check (and eventually change) instances attributes and instances 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, filters=filters):
|
|
|
|
for inst in res.instances:
|
|
|
|
for inst in res.instances:
|
|
|
|
|
|
|
|
|
|
|
|
# Check "source_dest_check" attribute
|
|
|
|
# Check "source_dest_check" attribute
|
|
|
@ -1292,11 +1317,12 @@ def main():
|
|
|
|
(changed, instance_dict_array, new_instance_ids) = terminate_instances(module, ec2, instance_ids)
|
|
|
|
(changed, instance_dict_array, new_instance_ids) = terminate_instances(module, ec2, instance_ids)
|
|
|
|
|
|
|
|
|
|
|
|
elif state in ('running', 'stopped'):
|
|
|
|
elif state in ('running', 'stopped'):
|
|
|
|
instance_ids = module.params['instance_ids']
|
|
|
|
instance_ids = module.params.get('instance_ids')
|
|
|
|
if not instance_ids:
|
|
|
|
instance_tags = module.params.get('instance_tags')
|
|
|
|
module.fail_json(msg='instance_ids list is requried for %s state' % state)
|
|
|
|
if not (isinstance(instance_ids, list) or isinstance(instance_tags, dict)):
|
|
|
|
|
|
|
|
module.fail_json(msg='running list needs to be a list of instances or set of tags to run: %s' % instance_ids)
|
|
|
|
|
|
|
|
|
|
|
|
(changed, instance_dict_array, new_instance_ids) = startstop_instances(module, ec2, instance_ids, state)
|
|
|
|
(changed, instance_dict_array, new_instance_ids) = startstop_instances(module, ec2, instance_ids, state, instance_tags)
|
|
|
|
|
|
|
|
|
|
|
|
elif state == 'present':
|
|
|
|
elif state == 'present':
|
|
|
|
# Changed is always set to true when provisioning new instances
|
|
|
|
# Changed is always set to true when provisioning new instances
|
|
|
|