Squashed commit of the following:

Changed ami_tags to instance_tags to better follow naming convention
    Add support for creating tags on the new instances
This commit is contained in:
Steve Frank 2013-02-09 11:42:14 -05:00 committed by Michael DeHaan
parent 775b8a9643
commit 56f0354026

21
ec2
View file

@ -95,8 +95,15 @@ options:
required: False
default: null
aliases: []
instance_tags:
version_added: "1.0"
description:
- string of tags, in json format, to add to the new instance
required: False
default: null
aliases: []
examples:
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver count=3"
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver count=3 instance_tags='{"Name":"My Server"}'"
description: "Examples from Ansible Playbooks"
requirements: [ "boto" ]
author: Seth Vidal, Tim Gerla, Lester Wade
@ -126,6 +133,7 @@ def main():
ec2_secret_key = dict(aliases=['EC2_SECRET_KEY']),
ec2_access_key = dict(aliases=['EC2_ACCESS_KEY']),
user_data = dict(),
instance_tags = dict(),
)
)
@ -141,7 +149,8 @@ def main():
ec2_secret_key = module.params.get('ec2_secret_key')
ec2_access_key = module.params.get('ec2_access_key')
user_data = module.params.get('user_data')
instance_tags = module.params.get('instance_tags')
# allow eucarc environment variables to be used if ansible vars aren't set
if not ec2_url and 'EC2_URL' in os.environ:
ec2_url = os.environ['EC2_URL']
@ -156,7 +165,7 @@ def main():
ec2 = boto.connect_ec2(ec2_access_key, ec2_secret_key)
# 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.
try:
res = ec2.run_instances(image, key_name = key_name,
min_count = count, max_count = count,
@ -170,6 +179,12 @@ def main():
instids = [ i.id for i in res.instances ]
if instance_tags:
try:
ec2.create_tags(instids, module.from_json(instance_tags))
except boto.exception.EC2ResponseError as e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
res_list = res.connection.get_all_instances(instids)
this_res = res_list[0]
if wait: