|
|
@ -37,13 +37,15 @@ options:
|
|
|
|
version_added: "1.4"
|
|
|
|
version_added: "1.4"
|
|
|
|
reuse_existing_ip_allowed:
|
|
|
|
reuse_existing_ip_allowed:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Reuse an EIP that is not associated to an instance (when available), instead of allocating a new one.
|
|
|
|
- Reuse an EIP that is not associated to an instance (when available),'''
|
|
|
|
|
|
|
|
''' instead of allocating a new one.
|
|
|
|
required: false
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
default: false
|
|
|
|
version_added: "1.6"
|
|
|
|
version_added: "1.6"
|
|
|
|
wait_timeout:
|
|
|
|
wait_timeout:
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- how long to wait in seconds for newly provisioned EIPs to become available
|
|
|
|
- how long to wait in seconds for newly provisioned EIPs to become'''
|
|
|
|
|
|
|
|
''' available
|
|
|
|
default: 300
|
|
|
|
default: 300
|
|
|
|
version_added: "1.7"
|
|
|
|
version_added: "1.7"
|
|
|
|
|
|
|
|
|
|
|
@ -53,9 +55,9 @@ notes:
|
|
|
|
- This module will return C(public_ip) on success, which will contain the
|
|
|
|
- This module will return C(public_ip) on success, which will contain the
|
|
|
|
public IP address associated with the instance.
|
|
|
|
public IP address associated with the instance.
|
|
|
|
- There may be a delay between the time the Elastic IP is assigned and when
|
|
|
|
- There may be a delay between the time the Elastic IP is assigned and when
|
|
|
|
the cloud instance is reachable via the new address. Use wait_for and pause
|
|
|
|
the cloud instance is reachable via the new address. Use wait_for and
|
|
|
|
to delay further playbook execution until the instance is reachable, if
|
|
|
|
pause to delay further playbook execution until the instance is reachable,
|
|
|
|
necessary.
|
|
|
|
if necessary.
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
EXAMPLES = '''
|
|
|
@ -78,7 +80,8 @@ EXAMPLES = '''
|
|
|
|
ec2_eip: state='present'
|
|
|
|
ec2_eip: state='present'
|
|
|
|
|
|
|
|
|
|
|
|
- name: provision new instances with ec2
|
|
|
|
- name: provision new instances with ec2
|
|
|
|
ec2: keypair=mykey instance_type=c1.medium image=ami-40603AD1 wait=yes group=webserver count=3
|
|
|
|
ec2: keypair=mykey instance_type=c1.medium image=emi-40603AD1 wait=yes'''
|
|
|
|
|
|
|
|
''' group=webserver count=3
|
|
|
|
register: ec2
|
|
|
|
register: ec2
|
|
|
|
- name: associate new elastic IPs with each of the instances
|
|
|
|
- name: associate new elastic IPs with each of the instances
|
|
|
|
ec2_eip: "instance_id={{ item }}"
|
|
|
|
ec2_eip: "instance_id={{ item }}"
|
|
|
@ -99,8 +102,10 @@ except ImportError:
|
|
|
|
|
|
|
|
|
|
|
|
wait_timeout = 0
|
|
|
|
wait_timeout = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def associate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
def associate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
if ip_is_associated_with_instance(ec2, address.public_ip, instance_id, module):
|
|
|
|
if ip_is_associated_with_instance(
|
|
|
|
|
|
|
|
ec2, address.public_ip, instance_id, module):
|
|
|
|
module.exit_json(changed=False, public_ip=address.public_ip)
|
|
|
|
module.exit_json(changed=False, public_ip=address.public_ip)
|
|
|
|
|
|
|
|
|
|
|
|
# If we're in check mode, nothing else to do
|
|
|
|
# If we're in check mode, nothing else to do
|
|
|
@ -108,21 +113,23 @@ def associate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if address.domain == "vpc":
|
|
|
|
if address.domain == 'vpc':
|
|
|
|
res = ec2.associate_address(instance_id, allocation_id=address.allocation_id)
|
|
|
|
res = ec2.associate_address(instance_id,
|
|
|
|
|
|
|
|
allocation_id=address.allocation_id)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
res = ec2.associate_address(instance_id, public_ip=address.public_ip)
|
|
|
|
res = ec2.associate_address(instance_id,
|
|
|
|
|
|
|
|
public_ip=address.public_ip)
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if res:
|
|
|
|
if res:
|
|
|
|
module.exit_json(changed=True, public_ip=address.public_ip)
|
|
|
|
module.exit_json(changed=True, public_ip=address.public_ip)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='association failed')
|
|
|
|
module.fail_json(msg="association failed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disassociate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
def disassociate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
if not ip_is_associated_with_instance(ec2, address.public_ip, instance_id, module):
|
|
|
|
if not ip_is_associated_with_instance(
|
|
|
|
|
|
|
|
ec2, address.public_ip, instance_id, module):
|
|
|
|
module.exit_json(changed=False, public_ip=address.public_ip)
|
|
|
|
module.exit_json(changed=False, public_ip=address.public_ip)
|
|
|
|
|
|
|
|
|
|
|
|
# If we're in check mode, nothing else to do
|
|
|
|
# If we're in check mode, nothing else to do
|
|
|
@ -131,7 +138,8 @@ def disassociate_ip_and_instance(ec2, address, instance_id, module):
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if address.domain == "vpc":
|
|
|
|
if address.domain == "vpc":
|
|
|
|
res = ec2.disassociate_address(association_id=address.association_id)
|
|
|
|
res = ec2.disassociate_address(
|
|
|
|
|
|
|
|
association_id=address.association_id)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
res = ec2.disassociate_address(public_ip=address.public_ip)
|
|
|
|
res = ec2.disassociate_address(public_ip=address.public_ip)
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
@ -152,7 +160,7 @@ def find_address(ec2, public_ip, module, fail_on_not_found=True):
|
|
|
|
addresses = ec2.get_all_addresses([public_ip])
|
|
|
|
addresses = ec2.get_all_addresses([public_ip])
|
|
|
|
break
|
|
|
|
break
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
if "Address '%s' not found." % public_ip in e.message :
|
|
|
|
if "Address '%s' not found." % public_ip in e.message:
|
|
|
|
if not fail_on_not_found:
|
|
|
|
if not fail_on_not_found:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -160,12 +168,13 @@ def find_address(ec2, public_ip, module, fail_on_not_found=True):
|
|
|
|
time.sleep(5)
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
if timeout <= time.time():
|
|
|
|
if timeout <= time.time():
|
|
|
|
module.fail_json(msg = "wait for EIPs timeout on %s" % time.asctime())
|
|
|
|
module.fail_json(msg="wait for EIPs timeout on %s" %
|
|
|
|
|
|
|
|
time.asctime())
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
addresses = ec2.get_all_addresses([public_ip])
|
|
|
|
addresses = ec2.get_all_addresses([public_ip])
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
except boto.exception.EC2ResponseError, e:
|
|
|
|
if "Address '%s' not found." % public_ip in e.message :
|
|
|
|
if "Address '%s' not found." % public_ip in e.message:
|
|
|
|
if not fail_on_not_found:
|
|
|
|
if not fail_on_not_found:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
module.fail_json(msg=str(e.message))
|
|
|
|
module.fail_json(msg=str(e.message))
|
|
|
@ -178,9 +187,9 @@ def ip_is_associated_with_instance(ec2, public_ip, instance_id, module):
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
if address:
|
|
|
|
if address:
|
|
|
|
return address.instance_id == instance_id
|
|
|
|
return address.instance_id == instance_id
|
|
|
|
else:
|
|
|
|
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def instance_is_associated(ec2, instance, module):
|
|
|
|
def instance_is_associated(ec2, instance, module):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Check if the given instance object is already associated with an
|
|
|
|
Check if the given instance object is already associated with an
|
|
|
@ -192,6 +201,7 @@ def instance_is_associated(ec2, instance, module):
|
|
|
|
eip = find_address(ec2, instance_ip, module, fail_on_not_found=False)
|
|
|
|
eip = find_address(ec2, instance_ip, module, fail_on_not_found=False)
|
|
|
|
return (eip and (eip.public_ip == instance_ip))
|
|
|
|
return (eip and (eip.public_ip == instance_ip))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
|
|
|
|
def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
|
|
|
|
""" Allocate a new elastic IP address (when needed) 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
|
|
|
@ -199,21 +209,15 @@ def allocate_address(ec2, domain, module, reuse_existing_ip_allowed):
|
|
|
|
module.exit_json(change=True)
|
|
|
|
module.exit_json(change=True)
|
|
|
|
|
|
|
|
|
|
|
|
if reuse_existing_ip_allowed:
|
|
|
|
if reuse_existing_ip_allowed:
|
|
|
|
if domain:
|
|
|
|
domain_filter = {'domain': domain or 'standard'}
|
|
|
|
domain_filter = { 'domain' : domain }
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
domain_filter = { 'domain' : 'standard' }
|
|
|
|
|
|
|
|
all_addresses = ec2.get_all_addresses(filters=domain_filter)
|
|
|
|
all_addresses = ec2.get_all_addresses(filters=domain_filter)
|
|
|
|
|
|
|
|
|
|
|
|
unassociated_addresses = filter(lambda a: not a.instance_id, all_addresses)
|
|
|
|
unassociated_addresses = [a for a in all_addresses
|
|
|
|
|
|
|
|
if not a.instance_id]
|
|
|
|
if unassociated_addresses:
|
|
|
|
if unassociated_addresses:
|
|
|
|
address = unassociated_addresses[0];
|
|
|
|
return unassociated_addresses[0]
|
|
|
|
else:
|
|
|
|
|
|
|
|
address = ec2.allocate_address(domain=domain)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
address = ec2.allocate_address(domain=domain)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return address
|
|
|
|
return ec2.allocate_address(domain=domain)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def release_address(ec2, public_ip, module):
|
|
|
|
def release_address(ec2, public_ip, module):
|
|
|
@ -260,15 +264,15 @@ def allocate_eip(ec2, eip_domain, module, reuse_existing_ip_allowed, new_eip_tim
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
argument_spec.update(dict(
|
|
|
|
instance_id = dict(required=False),
|
|
|
|
instance_id=dict(required=False),
|
|
|
|
public_ip = dict(required=False, aliases= ['ip']),
|
|
|
|
public_ip=dict(required=False, aliases=['ip']),
|
|
|
|
state = dict(required=False, default='present',
|
|
|
|
state=dict(required=False, default='present',
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
in_vpc = dict(required=False, type='bool', default=False),
|
|
|
|
in_vpc=dict(required=False, type='bool', default=False),
|
|
|
|
reuse_existing_ip_allowed = dict(required=False, type='bool', default=False),
|
|
|
|
reuse_existing_ip_allowed=dict(required=False, type='bool',
|
|
|
|
wait_timeout = dict(default=300),
|
|
|
|
default=False),
|
|
|
|
)
|
|
|
|
wait_timeout=dict(default=300),
|
|
|
|
)
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
argument_spec=argument_spec,
|
|
|
@ -284,7 +288,7 @@ def main():
|
|
|
|
public_ip = module.params.get('public_ip')
|
|
|
|
public_ip = module.params.get('public_ip')
|
|
|
|
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')
|
|
|
|
reuse_existing_ip_allowed = module.params.get('reuse_existing_ip_allowed')
|
|
|
|
new_eip_timeout = int(module.params.get('wait_timeout'))
|
|
|
|
new_eip_timeout = int(module.params.get('wait_timeout'))
|
|
|
|
|
|
|
|
|
|
|
@ -300,6 +304,7 @@ def main():
|
|
|
|
if public_ip:
|
|
|
|
if public_ip:
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Allocate an IP for instance since no public_ip was provided
|
|
|
|
if instance_id and not public_ip:
|
|
|
|
if instance_id and not public_ip:
|
|
|
|
instance = find_instance(ec2, instance_id, module)
|
|
|
|
instance = find_instance(ec2, instance_id, module)
|
|
|
|
|
|
|
|
|
|
|
@ -320,18 +325,18 @@ def main():
|
|
|
|
associate_ip_and_instance(ec2, address, instance_id, module)
|
|
|
|
associate_ip_and_instance(ec2, address, instance_id, module)
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
#disassociating address from instance
|
|
|
|
# disassociating address from instance
|
|
|
|
if instance_id:
|
|
|
|
if instance_id:
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
address = find_address(ec2, public_ip, module)
|
|
|
|
disassociate_ip_and_instance(ec2, address, instance_id, module)
|
|
|
|
disassociate_ip_and_instance(ec2, address, instance_id, module)
|
|
|
|
#releasing address
|
|
|
|
# releasing address
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
release_address(ec2, public_ip, module)
|
|
|
|
release_address(ec2, public_ip, module)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.basic import * # noqa
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
from ansible.module_utils.ec2 import * # noqa
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
main()
|
|
|
|