Merge pull request #2421 from robparrott/ec2-idempotency

Make ec2 module idempotent with Amazon EC2 via client-token attribute.
This commit is contained in:
Michael DeHaan 2013-03-17 08:19:15 -07:00
commit 24f619030f

42
ec2
View file

@ -1,4 +1,4 @@
#!/usr/bin/python -tt #!/usr/local/bin/python -tt
# This file is part of Ansible # This file is part of Ansible
# #
# Ansible is free software: you can redistribute it and/or modify # Ansible is free software: you can redistribute it and/or modify
@ -28,6 +28,12 @@ options:
required: true required: true
default: null default: null
aliases: ['keypair'] aliases: ['keypair']
id:
description:
- identifier for this instance or set of instances, so that the module will be idempotent with respect to EC2 instances.
required: false
default: null
aliases: []
group: group:
description: description:
- security group to use with the instance - security group to use with the instance
@ -149,6 +155,7 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
key_name = dict(required=True, aliases = ['keypair']), key_name = dict(required=True, aliases = ['keypair']),
id = dict(),
group = dict(), group = dict(),
group_id = dict(), group_id = dict(),
instance_type = dict(aliases=['type']), instance_type = dict(aliases=['type']),
@ -169,6 +176,7 @@ def main():
) )
key_name = module.params.get('key_name') key_name = module.params.get('key_name')
id = module.params.get('id')
group_name = module.params.get('group') group_name = module.params.get('group')
group_id = module.params.get('group_id') group_id = module.params.get('group_id')
instance_type = module.params.get('instance_type') instance_type = module.params.get('instance_type')
@ -212,12 +220,30 @@ def main():
except boto.exception.NoAuthHandlerFound, e: except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e)) module.fail_json(msg = str(e))
# Lookup any instances that much our run id.
running_instances = []
count_remaining = int(count)
if id != None:
filter_dict = {'client-token':id, 'instance-state-name' : 'running'}
previous_reservations = ec2.get_all_instances(None, filter_dict )
for res in previous_reservations:
for prev_instance in res.instances:
running_instances.append(prev_instance)
count_remaining = count_remaining - len(running_instances)
# module.fail_json(msg = "known running instances: %s" % (running_instances))
# Both min_count and max_count equal count parameter. This means the launch request is explicit (we want count, or fail) in how many instances we want. # Both min_count and max_count equal count parameter. This means the launch request is explicit (we want count, or fail) in how many instances we want.
if count_remaining > 0:
try: try:
res = ec2.run_instances(image, key_name = key_name, res = ec2.run_instances(image, key_name = key_name,
min_count = count, client_token=id,
max_count = count, min_count = count_remaining,
max_count = count_remaining,
monitoring_enabled = monitoring, monitoring_enabled = monitoring,
security_groups = [group_name], security_groups = [group_name],
instance_type = instance_type, instance_type = instance_type,
@ -259,16 +285,20 @@ def main():
if wait and wait_timeout <= time.time(): if wait and wait_timeout <= time.time():
# waiting took too long # waiting took too long
module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime()) module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime())
instances = []
for inst in this_res.instances: for inst in this_res.instances:
running_instances.append(inst)
instance_dict_array = []
for inst in running_instances:
d = { d = {
'id': inst.id, 'id': inst.id,
'public_ip': inst.ip_address, 'public_ip': inst.ip_address,
'public_dns_name': inst.public_dns_name 'public_dns_name': inst.public_dns_name
} }
instances.append(d) instance_dict_array.append(d)
module.exit_json(changed=True, instances=instances) module.exit_json(changed=True, instances=instance_dict_array)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>