Merge branch 'silkapp-ec2-eip-reuse' into devel
This commit is contained in:
commit
b10d174ab4
1 changed files with 27 additions and 5 deletions
|
@ -75,6 +75,12 @@ options:
|
||||||
default: null
|
default: null
|
||||||
aliases: []
|
aliases: []
|
||||||
version_added: "1.6"
|
version_added: "1.6"
|
||||||
|
reuse_existing_ip_allowed:
|
||||||
|
description:
|
||||||
|
- Reuse an EIP that is not associated to an instance (when available), instead of allocating a new one.
|
||||||
|
required: false
|
||||||
|
default: false
|
||||||
|
version_added: "1.6"
|
||||||
|
|
||||||
requirements: [ "boto" ]
|
requirements: [ "boto" ]
|
||||||
author: Lorin Hochstein <lorin@nimbisservices.com>
|
author: Lorin Hochstein <lorin@nimbisservices.com>
|
||||||
|
@ -189,13 +195,27 @@ def ip_is_associated_with_instance(ec2, public_ip, instance_id, module):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def allocate_address(ec2, domain, module):
|
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
|
||||||
""" Allocate a new elastic IP address and return it """
|
""" Allocate a new elastic IP address (when needed) and return it """
|
||||||
# If we're in check mode, nothing else to do
|
# If we're in check mode, nothing else to do
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(change=True)
|
module.exit_json(change=True)
|
||||||
|
|
||||||
|
if reuse_existing_ip_allowed:
|
||||||
|
if domain:
|
||||||
|
domain_filter = { 'domain' : domain }
|
||||||
|
else:
|
||||||
|
domain_filter = { 'domain' : 'standard' }
|
||||||
|
all_addresses = ec2.get_all_addresses(filters=domain_filter)
|
||||||
|
|
||||||
|
unassociated_addresses = filter(lambda a: a.instance_id is None, all_addresses)
|
||||||
|
if unassociated_addresses:
|
||||||
|
address = unassociated_addresses[0];
|
||||||
|
else:
|
||||||
address = ec2.allocate_address(domain=domain)
|
address = ec2.allocate_address(domain=domain)
|
||||||
|
else:
|
||||||
|
address = ec2.allocate_address(domain=domain)
|
||||||
|
|
||||||
return address
|
return address
|
||||||
|
|
||||||
|
|
||||||
|
@ -239,6 +259,7 @@ def main():
|
||||||
state = dict(required=False, default='present',
|
state = dict(required=False, default='present',
|
||||||
choices=['present', 'absent']),
|
choices=['present', 'absent']),
|
||||||
in_vpc = dict(required=False, choices=BOOLEANS, default=False),
|
in_vpc = dict(required=False, choices=BOOLEANS, default=False),
|
||||||
|
reuse_existing_ip_allowed = dict(required=False, type='bool', default=False),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -257,18 +278,19 @@ def main():
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
in_vpc = module.params.get('in_vpc')
|
in_vpc = module.params.get('in_vpc')
|
||||||
domain = "vpc" if in_vpc else None
|
domain = "vpc" if in_vpc else None
|
||||||
|
reuse_existing_ip_allowed = module.params.get('reuse_existing_ip_allowed');
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if public_ip is None:
|
if public_ip is None:
|
||||||
if instance_id is None:
|
if instance_id is None:
|
||||||
address = allocate_address(ec2, domain, module)
|
address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed)
|
||||||
module.exit_json(changed=True, public_ip=address.public_ip)
|
module.exit_json(changed=True, public_ip=address.public_ip)
|
||||||
else:
|
else:
|
||||||
# Determine if the instance is inside a VPC or not
|
# Determine if the instance is inside a VPC or not
|
||||||
instance = find_instance(ec2, instance_id, module)
|
instance = find_instance(ec2, instance_id, module)
|
||||||
if instance.vpc_id != None:
|
if instance.vpc_id != None:
|
||||||
domain = "vpc"
|
domain = "vpc"
|
||||||
address = allocate_address(ec2, domain, module)
|
address = allocate_address(ec2, domain, module, reuse_existing_ip_allowed)
|
||||||
else:
|
else:
|
||||||
address = find_address(ec2, public_ip, module)
|
address = find_address(ec2, public_ip, module)
|
||||||
associate_ip_and_instance(ec2, address, instance_id, module)
|
associate_ip_and_instance(ec2, address, instance_id, module)
|
||||||
|
|
Loading…
Reference in a new issue