Merge pull request #1703 from haad/ec2_start_stop_by_tag
Rebase start/stop by instance tag
This commit is contained in:
commit
66288d48a2
1 changed files with 62 additions and 36 deletions
|
@ -144,7 +144,7 @@ options:
|
|||
instance_tags:
|
||||
version_added: "1.0"
|
||||
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
|
||||
default: null
|
||||
aliases: []
|
||||
|
@ -446,6 +446,15 @@ EXAMPLES = '''
|
|||
vpc_subnet_id: subnet-29e63245
|
||||
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
|
||||
# (Highly recommended!)
|
||||
|
@ -1149,7 +1158,7 @@ def terminate_instances(module, ec2, 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
|
||||
|
||||
|
@ -1157,6 +1166,8 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|||
ec2: authenticated ec2 connection object
|
||||
instance_ids: The list of instances to start in the form of
|
||||
[ {id: <inst-id>}, ..]
|
||||
instance_tags: A dict of tag keys and values in the form of
|
||||
{key: value, ... }
|
||||
state: Intended state ("running" or "stopped")
|
||||
|
||||
Returns a dictionary of instance information
|
||||
|
@ -1165,6 +1176,8 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|||
If the instance was not able to change state,
|
||||
"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')
|
||||
|
@ -1173,11 +1186,23 @@ def startstop_instances(module, ec2, instance_ids, state):
|
|||
instance_dict_array = []
|
||||
|
||||
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
|
||||
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:
|
||||
|
||||
# Check "source_dest_check" attribute
|
||||
|
@ -1298,11 +1323,12 @@ def main():
|
|||
(changed, instance_dict_array, new_instance_ids) = terminate_instances(module, ec2, instance_ids)
|
||||
|
||||
elif state in ('running', 'stopped'):
|
||||
instance_ids = module.params['instance_ids']
|
||||
if not instance_ids:
|
||||
module.fail_json(msg='instance_ids list is requried for %s state' % state)
|
||||
instance_ids = module.params.get('instance_ids')
|
||||
instance_tags = module.params.get('instance_tags')
|
||||
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':
|
||||
# Changed is always set to true when provisioning new instances
|
||||
|
|
Loading…
Reference in a new issue