Merge pull request #4502 from dxsarmit/ansible

New library/cloud/ec2 module parameter (assign_public_ip) for non-default VPC instance launching.
This commit is contained in:
James Tanner 2013-12-02 09:43:20 -05:00
commit 576cd98d62

View file

@ -156,6 +156,13 @@ options:
required: false required: false
default: null default: null
aliases: [] aliases: []
assign_public_ip:
version_added: "1.4"
description:
- when provisioning within vpc, assign a public IP address. Boto library must be 2.13.0+
required: false
default: null
aliases: []
private_ip: private_ip:
version_added: "1.2" version_added: "1.2"
description: description:
@ -238,7 +245,7 @@ local_action:
image: ami-6e649707 image: ami-6e649707
wait: yes wait: yes
vpc_subnet_id: subnet-29e63245 vpc_subnet_id: subnet-29e63245
assign_public_ip: yes
# Launch instances, runs some tasks # Launch instances, runs some tasks
# and then terminate them # and then terminate them
@ -336,6 +343,24 @@ def get_instance_info(inst):
return instance_info return instance_info
def boto_supports_associate_public_ip_address(ec2):
"""
Check if Boto library has associate_public_ip_address in the NetworkInterfaceSpecification
class. Added in Boto 2.13.0
ec2: authenticated ec2 connection object
Returns:
True if Boto library accepts associate_public_ip_address argument, else false
"""
try:
network_interface = boto.ec2.networkinterface.NetworkInterfaceSpecification()
getattr(network_interface, "associate_public_ip_address")
return True
except AttributeError:
return False
def boto_supports_profile_name_arg(ec2): def boto_supports_profile_name_arg(ec2):
""" """
Check if Boto library has instance_profile_name argument. instance_profile_name has been added in Boto 2.5.0 Check if Boto library has instance_profile_name argument. instance_profile_name has been added in Boto 2.5.0
@ -378,10 +403,10 @@ def create_instances(module, ec2):
user_data = module.params.get('user_data') user_data = module.params.get('user_data')
instance_tags = module.params.get('instance_tags') instance_tags = module.params.get('instance_tags')
vpc_subnet_id = module.params.get('vpc_subnet_id') vpc_subnet_id = module.params.get('vpc_subnet_id')
assign_public_ip = module.boolean(module.params.get('assign_public_ip'))
private_ip = module.params.get('private_ip') private_ip = module.params.get('private_ip')
instance_profile_name = module.params.get('instance_profile_name') instance_profile_name = module.params.get('instance_profile_name')
# group_id and group_name are exclusive of each other # group_id and group_name are exclusive of each other
if group_id and group_name: if group_id and group_name:
module.fail_json(msg = str("Use only one type of parameter (group_name) or (group_id)")) module.fail_json(msg = str("Use only one type of parameter (group_name) or (group_id)"))
@ -440,7 +465,6 @@ def create_instances(module, ec2):
'instance_type': instance_type, 'instance_type': instance_type,
'kernel_id': kernel, 'kernel_id': kernel,
'ramdisk_id': ramdisk, 'ramdisk_id': ramdisk,
'subnet_id': vpc_subnet_id,
'private_ip_address': private_ip, 'private_ip_address': private_ip,
'user_data': user_data} 'user_data': user_data}
@ -451,6 +475,24 @@ def create_instances(module, ec2):
module.fail_json( module.fail_json(
msg="instance_profile_name parameter requires Boto version 2.5.0 or higher") msg="instance_profile_name parameter requires Boto version 2.5.0 or higher")
if assign_public_ip:
if not boto_supports_associate_public_ip_address(ec2):
module.fail_json(
msg="assign_public_ip parameter requires Boto version 2.13.0 or higher.")
elif not vpc_subnet_id:
module.fail_json(
msg="assign_public_ip only available with vpc_subnet_id")
else:
interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
subnet_id=vpc_subnet_id,
groups=group_id,
associate_public_ip_address=assign_public_ip)
interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)
params['network_interfaces'] = interfaces
else:
params['subnet_id'] = vpc_subnet_id
if vpc_subnet_id: if vpc_subnet_id:
params['security_group_ids'] = group_id params['security_group_ids'] = group_id
else: else:
@ -603,6 +645,7 @@ def main():
user_data = dict(), user_data = dict(),
instance_tags = dict(type='dict'), instance_tags = dict(type='dict'),
vpc_subnet_id = dict(), vpc_subnet_id = dict(),
assign_public_ip = dict(type='bool', default=False),
private_ip = dict(), private_ip = dict(),
instance_profile_name = dict(), instance_profile_name = dict(),
instance_ids = dict(type='list'), instance_ids = dict(type='list'),