Merge pull request #2143 from lwade/ec2_group_id

Add support for security group id parameter ISSUE #2029
pull/2118/merge
Michael DeHaan 12 years ago
commit 405471bbab

@ -19,7 +19,7 @@ DOCUMENTATION = '''
module: ec2 module: ec2
short_description: create an instance in ec2, return instanceid short_description: create an instance in ec2, return instanceid
description: description:
- creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on boto and m2crypt. - creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on python-boto.
version_added: "0.9" version_added: "0.9"
options: options:
key_name: key_name:
@ -30,9 +30,16 @@ options:
aliases: ['keypair'] aliases: ['keypair']
group: group:
description: description:
- security group to use on the instance - security group to use with the instance
required: false required: false
default: 'default' default: null
aliases: []
group_id:
version_added: "1.1"
description:
- security group id to use with the instance
required: false
default: null
aliases: [] aliases: []
instance_type: instance_type:
description: description:
@ -61,25 +68,25 @@ options:
wait: wait:
description: description:
- wait for the instance to be in state 'running' before returning - wait for the instance to be in state 'running' before returning
required: False required: false
default: False default: false
aliases: [] aliases: []
ec2_url: ec2_url:
description: description:
- url to use to connect to ec2 or your Eucalyptus cloud (by default the module will use ec2 endpoints) - url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints)
required: False required: false
default: null default: null
aliases: [] aliases: []
ec2_secret_key: ec2_secret_key:
description: description:
- ec2 secret key - ec2 secret key
required: False required: false
default: null default: null
aliases: [] aliases: []
ec2_access_key: ec2_access_key:
description: description:
- ec2 access key - ec2 access key
required: False required: false
default: null default: null
aliases: [] aliases: []
count: count:
@ -89,27 +96,28 @@ options:
default: 1 default: 1
aliases: [] aliases: []
monitor: monitor:
version_added: "1.1"
description: description:
- enable detailed monitoring (CloudWatch) for instance - enable detailed monitoring (CloudWatch) for instance
required: False required: false
default: null default: null
aliases: [] aliases: []
user_data: user_data:
version_added: "0.9" version_added: "0.9"
description: description:
- opaque blob of data which is made available to the ec2 instance - opaque blob of data which is made available to the ec2 instance
required: False required: false
default: null default: null
aliases: [] aliases: []
instance_tags: instance_tags:
version_added: "1.0" version_added: "1.0"
description: description:
- a hash/dictionary of tags, in quoted json format, to add to the new instance - a hash/dictionary of tags to add to the new instance; '{"key":"value"}' and '{"key":"value","key":"value"}'
required: False required: false
default: null default: null
aliases: [] aliases: []
examples: 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 group=webservers'
description: "Examples from Ansible Playbooks" description: "Examples from Ansible Playbooks"
requirements: [ "boto" ] requirements: [ "boto" ]
author: Seth Vidal, Tim Gerla, Lester Wade author: Seth Vidal, Tim Gerla, Lester Wade
@ -128,7 +136,8 @@ 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']),
group = dict(default='default'), group = dict(),
group_id = dict(),
instance_type = dict(aliases=['type']), instance_type = dict(aliases=['type']),
image = dict(required=True), image = dict(required=True),
kernel = dict(), kernel = dict(),
@ -145,7 +154,8 @@ def main():
) )
key_name = module.params.get('key_name') key_name = module.params.get('key_name')
group = module.params.get('group') group_name = module.params.get('group')
group_id = module.params.get('group_id')
instance_type = module.params.get('instance_type') instance_type = module.params.get('instance_type')
image = module.params.get('image') image = module.params.get('image')
count = module.params.get('count') count = module.params.get('count')
@ -175,6 +185,15 @@ def main():
except boto.exception.NoAuthHandlerFound, e: except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e)) module.fail_json(msg = str(e))
# Here we try to lookup the group name from the security group id - if group_id is set.
try:
if group_id:
grp_details = ec2.get_all_security_groups(group_ids=group_id)
grp_item = grp_details[0]
group_name = grp_item.name
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e))
# 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.
@ -183,7 +202,7 @@ def main():
min_count = count, min_count = count,
max_count = count, max_count = count,
monitoring_enabled = monitoring, monitoring_enabled = monitoring,
security_groups = [group], security_groups = [group_name],
instance_type = instance_type, instance_type = instance_type,
kernel_id = kernel, kernel_id = kernel,
ramdisk_id = ramdisk, ramdisk_id = ramdisk,
@ -219,7 +238,9 @@ def main():
} }
instances.append(d) instances.append(d)
module.exit_json(changed=True, instances=instances) result = {"changed": True,
"instances": instances }
module.exit_json(**result)
# 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>>

Loading…
Cancel
Save