1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

If an AWS account does not have a default subnet in their VPC configuration, then creating instances in that VPC will not automatically provide a public IP/DNS. Boto added this functionality in 2.13.0 (NetworkInterfaceSpecification.associate_public_ip_address).

This change adds assign_pubic_ip as a parameter to the ec2 module, ensuring that it is not set to one of BOOLEANS_TRUE if vpc_subnet_id is not also set, and if Boto is less than 2.13.0.
This commit is contained in:
Scott Armit 2013-10-11 13:13:04 -04:00
parent 73dbab702f
commit ef317ecde7

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)"))
@ -442,7 +467,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}
@ -453,6 +477,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:
@ -579,6 +621,7 @@ def main():
user_data = dict(), user_data = dict(),
instance_tags = dict(), instance_tags = 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'),
@ -591,7 +634,6 @@ def main():
aws_access_key = module.params.get('aws_access_key') aws_access_key = module.params.get('aws_access_key')
region = module.params.get('region') region = module.params.get('region')
# allow eucarc environment variables to be used if ansible vars aren't set # allow eucarc environment variables to be used if ansible vars aren't set
if not ec2_url and 'EC2_URL' in os.environ: if not ec2_url and 'EC2_URL' in os.environ:
ec2_url = os.environ['EC2_URL'] ec2_url = os.environ['EC2_URL']