2013-08-07 21:38:19 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-04 05:57:53 +02:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-08-07 21:38:19 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: ec2_eip
|
2016-08-30 15:16:42 +02:00
|
|
|
short_description: manages EC2 elastic IP (EIP) addresses.
|
2013-08-07 21:38:19 +02:00
|
|
|
description:
|
2016-08-30 15:16:42 +02:00
|
|
|
- This module can allocate or release an EIP.
|
|
|
|
- This module can associate/disassociate an EIP with instances or network interfaces.
|
2015-07-28 20:00:37 +02:00
|
|
|
version_added: "1.4"
|
2013-08-07 21:38:19 +02:00
|
|
|
options:
|
2015-08-31 18:06:18 +02:00
|
|
|
device_id:
|
2013-08-07 21:38:19 +02:00
|
|
|
description:
|
2015-08-31 18:06:18 +02:00
|
|
|
- The id of the device for the EIP. Can be an EC2 Instance id or Elastic Network Interface (ENI) id.
|
2015-08-05 20:22:09 +02:00
|
|
|
required: false
|
2015-08-31 18:06:18 +02:00
|
|
|
aliases: [ instance_id ]
|
2015-08-05 20:22:09 +02:00
|
|
|
version_added: "2.0"
|
2013-08-07 21:38:19 +02:00
|
|
|
public_ip:
|
|
|
|
description:
|
2016-08-30 15:16:42 +02:00
|
|
|
- The IP address of a previously allocated EIP.
|
|
|
|
- If present and device is specified, the EIP is associated with the device.
|
|
|
|
- If absent and device is specified, the EIP is disassociated from the device.
|
2013-09-13 03:11:24 +02:00
|
|
|
required: false
|
2016-08-30 15:16:42 +02:00
|
|
|
aliases: [ ip ]
|
2013-08-07 21:38:19 +02:00
|
|
|
state:
|
|
|
|
description:
|
2016-08-30 15:16:42 +02:00
|
|
|
- If present, allocate an EIP or associate an existing EIP with a device.
|
|
|
|
- If absent, disassociate the EIP from the device and optionally release it.
|
2013-08-07 21:38:19 +02:00
|
|
|
required: false
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: present
|
2013-10-18 23:34:17 +02:00
|
|
|
in_vpc:
|
|
|
|
description:
|
|
|
|
- allocate an EIP inside a VPC or not
|
|
|
|
required: false
|
|
|
|
default: false
|
2013-10-24 18:19:43 +02:00
|
|
|
version_added: "1.4"
|
2014-03-11 17:12:23 +01:00
|
|
|
reuse_existing_ip_allowed:
|
2013-11-15 10:14:13 +01:00
|
|
|
description:
|
2016-08-30 15:16:42 +02:00
|
|
|
- Reuse an EIP that is not associated to a device (when available), instead of allocating a new one.
|
2013-11-15 10:14:13 +01:00
|
|
|
required: false
|
|
|
|
default: false
|
2014-03-11 17:12:23 +01:00
|
|
|
version_added: "1.6"
|
2015-08-31 18:06:18 +02:00
|
|
|
release_on_disassociation:
|
|
|
|
description:
|
|
|
|
- whether or not to automatically release the EIP when it is disassociated
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "2.0"
|
2015-10-07 22:16:24 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- aws
|
|
|
|
- ec2
|
2015-08-31 18:06:18 +02:00
|
|
|
author: "Rick Mendes (@rickmendes) <rmendes@illumina.com>"
|
2013-09-13 03:11:24 +02:00
|
|
|
notes:
|
|
|
|
- This module will return C(public_ip) on success, which will contain the
|
2016-08-30 15:16:42 +02:00
|
|
|
public IP address associated with the device.
|
|
|
|
- There may be a delay between the time the EIP is assigned and when
|
2014-11-14 04:10:23 +01:00
|
|
|
the cloud instance is reachable via the new address. Use wait_for and
|
|
|
|
pause to delay further playbook execution until the instance is reachable,
|
|
|
|
if necessary.
|
2015-08-31 18:06:18 +02:00
|
|
|
- This module returns multiple changed statuses on disassociation or release.
|
|
|
|
It returns an overall status based on any changes occuring. It also returns
|
|
|
|
individual changed statuses for disassociation and release.
|
2013-08-07 21:38:19 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-09-13 03:11:24 +02:00
|
|
|
- name: associate an elastic IP with an instance
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: device_id=i-1212f003 ip=93.184.216.119
|
2015-08-05 20:22:09 +02:00
|
|
|
- name: associate an elastic IP with a device
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: device_id=eni-c8ad70f3 ip=93.184.216.119
|
2013-09-13 03:11:24 +02:00
|
|
|
- name: disassociate an elastic IP from an instance
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: device_id=i-1212f003 ip=93.184.216.119 state=absent
|
2015-08-05 20:22:09 +02:00
|
|
|
- name: disassociate an elastic IP with a device
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: device_id=eni-c8ad70f3 ip=93.184.216.119 state=absent
|
2013-09-13 03:11:24 +02:00
|
|
|
- name: allocate a new elastic IP and associate it with an instance
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: device_id=i-1212f003
|
2013-09-13 03:11:24 +02:00
|
|
|
- name: allocate a new elastic IP without associating it to anything
|
2014-12-01 21:14:57 +01:00
|
|
|
action: ec2_eip
|
2013-09-13 03:11:24 +02:00
|
|
|
register: eip
|
|
|
|
- name: output the IP
|
|
|
|
debug: msg="Allocated IP is {{ eip.public_ip }}"
|
2014-11-11 03:12:44 +01:00
|
|
|
- name: another way of allocating an elastic IP without associating it to anything
|
|
|
|
ec2_eip: state='present'
|
2013-09-13 03:11:24 +02:00
|
|
|
- name: provision new instances with ec2
|
2016-08-30 15:16:42 +02:00
|
|
|
ec2: keypair=mykey instance_type=c1.medium image=ami-40603AD1 wait=yes'''
|
2014-11-14 04:10:23 +01:00
|
|
|
''' group=webserver count=3
|
2013-09-13 03:11:24 +02:00
|
|
|
register: ec2
|
|
|
|
- name: associate new elastic IPs with each of the instances
|
2015-08-31 18:06:18 +02:00
|
|
|
ec2_eip: "device_id={{ item }}"
|
2013-09-13 03:11:24 +02:00
|
|
|
with_items: ec2.instance_ids
|
2013-10-18 23:34:17 +02:00
|
|
|
- name: allocate a new elastic IP inside a VPC in us-west-2
|
|
|
|
ec2_eip: region=us-west-2 in_vpc=yes
|
|
|
|
register: eip
|
|
|
|
- name: output the IP
|
|
|
|
debug: msg="Allocated IP inside a VPC is {{ eip.public_ip }}"
|
2013-08-07 21:38:19 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import boto.ec2
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2013-08-07 21:38:19 +02:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
class EIPException(Exception):
|
|
|
|
pass
|
2014-07-29 08:59:11 +02:00
|
|
|
|
2015-09-14 17:27:37 +02:00
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def associate_ip_and_device(ec2, address, device_id, check_mode, isinstance=True):
|
|
|
|
if address_is_associated_with_device(ec2, address, device_id, isinstance):
|
2014-11-14 05:05:49 +01:00
|
|
|
return {'changed': False}
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
# If we're in check mode, nothing else to do
|
|
|
|
if not check_mode:
|
2015-08-05 20:22:09 +02:00
|
|
|
if isinstance:
|
|
|
|
if address.domain == "vpc":
|
|
|
|
res = ec2.associate_address(device_id, allocation_id=address.allocation_id)
|
|
|
|
else:
|
|
|
|
res = ec2.associate_address(device_id, public_ip=address.public_ip)
|
2013-10-18 23:34:17 +02:00
|
|
|
else:
|
2015-08-05 20:22:09 +02:00
|
|
|
res = ec2.associate_address(network_interface_id=device_id, allocation_id=address.allocation_id)
|
2014-11-14 05:05:49 +01:00
|
|
|
if not res:
|
|
|
|
raise EIPException('association failed')
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
return {'changed': True}
|
2013-08-07 21:38:19 +02:00
|
|
|
|
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def disassociate_ip_and_device(ec2, address, device_id, check_mode, isinstance=True):
|
|
|
|
if not address_is_associated_with_device(ec2, address, device_id, isinstance):
|
2014-11-14 05:05:49 +01:00
|
|
|
return {'changed': False}
|
2013-08-07 21:38:19 +02:00
|
|
|
|
|
|
|
# If we're in check mode, nothing else to do
|
2014-11-14 05:05:49 +01:00
|
|
|
if not check_mode:
|
|
|
|
if address.domain == 'vpc':
|
2014-11-14 04:10:23 +01:00
|
|
|
res = ec2.disassociate_address(
|
|
|
|
association_id=address.association_id)
|
2013-10-19 00:12:49 +02:00
|
|
|
else:
|
|
|
|
res = ec2.disassociate_address(public_ip=address.public_ip)
|
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
if not res:
|
|
|
|
raise EIPException('disassociation failed')
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
return {'changed': True}
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
|
2014-11-14 06:31:45 +01:00
|
|
|
def _find_address_by_ip(ec2, public_ip):
|
|
|
|
try:
|
|
|
|
return ec2.get_all_addresses([public_ip])[0]
|
|
|
|
except boto.exception.EC2ResponseError as e:
|
|
|
|
if "Address '{}' not found.".format(public_ip) not in e.message:
|
|
|
|
raise
|
|
|
|
|
2014-07-25 16:56:25 +02:00
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def _find_address_by_device_id(ec2, device_id, isinstance=True):
|
|
|
|
if isinstance:
|
|
|
|
addresses = ec2.get_all_addresses(None, {'instance-id': device_id})
|
|
|
|
else:
|
|
|
|
addresses = ec2.get_all_addresses(None, {'network-interface-id': device_id})
|
2014-11-14 06:31:45 +01:00
|
|
|
if addresses:
|
|
|
|
return addresses[0]
|
|
|
|
|
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def find_address(ec2, public_ip, device_id, isinstance=True):
|
2014-11-14 06:31:45 +01:00
|
|
|
""" Find an existing Elastic IP address """
|
|
|
|
if public_ip:
|
|
|
|
return _find_address_by_ip(ec2, public_ip)
|
2015-08-05 20:22:09 +02:00
|
|
|
elif device_id and isinstance:
|
|
|
|
return _find_address_by_device_id(ec2, device_id)
|
|
|
|
elif device_id:
|
|
|
|
return _find_address_by_device_id(ec2, device_id, isinstance=False)
|
2013-10-18 23:34:17 +02:00
|
|
|
|
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def address_is_associated_with_device(ec2, address, device_id, isinstance=True):
|
|
|
|
""" Check if the elastic IP is currently associated with the device """
|
|
|
|
address = ec2.get_all_addresses(address.public_ip)
|
2013-10-18 23:34:17 +02:00
|
|
|
if address:
|
2015-08-05 20:22:09 +02:00
|
|
|
if isinstance:
|
|
|
|
return address and address[0].instance_id == device_id
|
|
|
|
else:
|
|
|
|
return address and address[0].network_interface_id == device_id
|
2014-11-14 04:10:23 +01:00
|
|
|
return False
|
|
|
|
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
def allocate_address(ec2, domain, reuse_existing_ip_allowed):
|
2013-11-15 10:14:13 +01:00
|
|
|
""" Allocate a new elastic IP address (when needed) and return it """
|
2014-03-11 17:12:23 +01:00
|
|
|
if reuse_existing_ip_allowed:
|
2014-11-14 04:10:23 +01:00
|
|
|
domain_filter = {'domain': domain or 'standard'}
|
|
|
|
all_addresses = ec2.get_all_addresses(filters=domain_filter)
|
2013-11-15 10:14:13 +01:00
|
|
|
|
2015-09-14 17:27:37 +02:00
|
|
|
if domain == 'vpc':
|
|
|
|
unassociated_addresses = [a for a in all_addresses
|
|
|
|
if not a.association_id]
|
|
|
|
else:
|
|
|
|
unassociated_addresses = [a for a in all_addresses
|
|
|
|
if not a.instance_id]
|
2014-11-14 04:10:23 +01:00
|
|
|
if unassociated_addresses:
|
|
|
|
return unassociated_addresses[0]
|
|
|
|
|
|
|
|
return ec2.allocate_address(domain=domain)
|
2013-09-13 03:11:24 +02:00
|
|
|
|
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
def release_address(ec2, address, check_mode):
|
2013-10-18 23:34:17 +02:00
|
|
|
""" Release a previously allocated elastic IP address """
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2013-10-18 23:34:17 +02:00
|
|
|
# If we're in check mode, nothing else to do
|
2014-11-14 05:05:49 +01:00
|
|
|
if not check_mode:
|
|
|
|
if not address.release():
|
|
|
|
EIPException('release failed')
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
return {'changed': True}
|
2013-10-18 23:34:17 +02:00
|
|
|
|
|
|
|
|
2016-07-31 20:54:21 +02:00
|
|
|
def find_device(ec2, module, device_id, isinstance=True):
|
2013-10-18 23:34:17 +02:00
|
|
|
""" Attempt to find the EC2 instance and return it """
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
if isinstance:
|
|
|
|
try:
|
|
|
|
reservations = ec2.get_all_reservations(instance_ids=[device_id])
|
2016-06-02 21:56:48 +02:00
|
|
|
except boto.exception.EC2ResponseError as e:
|
2015-08-05 20:22:09 +02:00
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
if len(reservations) == 1:
|
|
|
|
instances = reservations[0].instances
|
|
|
|
if len(instances) == 1:
|
|
|
|
return instances[0]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
interfaces = ec2.get_all_network_interfaces(network_interface_ids=[device_id])
|
2016-06-02 21:56:48 +02:00
|
|
|
except boto.exception.EC2ResponseError as e:
|
2015-08-05 20:22:09 +02:00
|
|
|
module.fail_json(msg=str(e))
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
if len(interfaces) == 1:
|
|
|
|
return interfaces[0]
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
raise EIPException("could not find instance" + device_id)
|
2014-11-14 05:05:49 +01:00
|
|
|
|
|
|
|
|
2016-07-31 20:54:21 +02:00
|
|
|
def ensure_present(ec2, module, domain, address, device_id,
|
2015-08-05 20:22:09 +02:00
|
|
|
reuse_existing_ip_allowed, check_mode, isinstance=True):
|
2014-11-14 05:05:49 +01:00
|
|
|
changed = False
|
2014-11-14 04:10:23 +01:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
# Return the EIP object since we've been given a public IP
|
|
|
|
if not address:
|
|
|
|
if check_mode:
|
|
|
|
return {'changed': True}
|
2013-10-18 23:34:17 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
address = allocate_address(ec2, domain, reuse_existing_ip_allowed)
|
|
|
|
changed = True
|
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
if device_id:
|
2014-11-14 05:05:49 +01:00
|
|
|
# Allocate an IP for instance since no public_ip was provided
|
2015-08-05 20:22:09 +02:00
|
|
|
if isinstance:
|
2016-07-31 20:54:21 +02:00
|
|
|
instance = find_device(ec2, module, device_id)
|
2015-09-14 17:27:37 +02:00
|
|
|
if reuse_existing_ip_allowed:
|
|
|
|
if len(instance.vpc_id) > 0 and domain is None:
|
|
|
|
raise EIPException("You must set 'in_vpc' to true to associate an instance with an existing ip in a vpc")
|
2015-08-05 20:22:09 +02:00
|
|
|
# Associate address object (provided or allocated) with instance
|
|
|
|
assoc_result = associate_ip_and_device(ec2, address, device_id,
|
|
|
|
check_mode)
|
|
|
|
else:
|
2016-07-31 20:54:21 +02:00
|
|
|
instance = find_device(ec2, module, device_id, isinstance=False)
|
2015-08-05 20:22:09 +02:00
|
|
|
# Associate address object (provided or allocated) with instance
|
|
|
|
assoc_result = associate_ip_and_device(ec2, address, device_id,
|
|
|
|
check_mode, isinstance=False)
|
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
if instance.vpc_id:
|
|
|
|
domain = 'vpc'
|
|
|
|
|
|
|
|
changed = changed or assoc_result['changed']
|
|
|
|
|
2016-01-02 12:16:46 +01:00
|
|
|
return {'changed': changed, 'public_ip': address.public_ip, 'allocation_id': address.allocation_id}
|
2014-11-14 05:05:49 +01:00
|
|
|
|
|
|
|
|
2015-08-05 20:22:09 +02:00
|
|
|
def ensure_absent(ec2, domain, address, device_id, check_mode, isinstance=True):
|
2014-11-14 05:05:49 +01:00
|
|
|
if not address:
|
|
|
|
return {'changed': False}
|
|
|
|
|
|
|
|
# disassociating address from instance
|
2015-08-05 20:22:09 +02:00
|
|
|
if device_id:
|
|
|
|
if isinstance:
|
|
|
|
return disassociate_ip_and_device(ec2, address, device_id,
|
|
|
|
check_mode)
|
|
|
|
else:
|
|
|
|
return disassociate_ip_and_device(ec2, address, device_id,
|
|
|
|
check_mode, isinstance=False)
|
2014-11-14 05:05:49 +01:00
|
|
|
# releasing address
|
|
|
|
else:
|
|
|
|
return release_address(ec2, address, check_mode)
|
2014-11-11 03:12:44 +01:00
|
|
|
|
|
|
|
|
2013-08-07 21:38:19 +02:00
|
|
|
def main():
|
2014-02-05 12:11:06 +01:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
2015-08-31 18:06:18 +02:00
|
|
|
device_id=dict(required=False, aliases=['instance_id']),
|
2014-11-14 04:10:23 +01:00
|
|
|
public_ip=dict(required=False, aliases=['ip']),
|
|
|
|
state=dict(required=False, default='present',
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
in_vpc=dict(required=False, type='bool', default=False),
|
|
|
|
reuse_existing_ip_allowed=dict(required=False, type='bool',
|
|
|
|
default=False),
|
2015-08-31 18:06:18 +02:00
|
|
|
release_on_disassociation=dict(required=False, type='bool', default=False),
|
2014-11-14 04:10:23 +01:00
|
|
|
wait_timeout=dict(default=300),
|
|
|
|
))
|
2014-02-05 12:11:06 +01:00
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2013-08-07 21:38:19 +02:00
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2013-12-17 03:04:12 +01:00
|
|
|
ec2 = ec2_connect(module)
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2015-08-31 18:06:18 +02:00
|
|
|
device_id = module.params.get('device_id')
|
2015-09-14 17:27:37 +02:00
|
|
|
instance_id = module.params.get('instance_id')
|
2013-08-07 21:38:19 +02:00
|
|
|
public_ip = module.params.get('public_ip')
|
|
|
|
state = module.params.get('state')
|
2013-10-18 23:34:17 +02:00
|
|
|
in_vpc = module.params.get('in_vpc')
|
2014-11-14 04:10:23 +01:00
|
|
|
domain = 'vpc' if in_vpc else None
|
2014-07-29 08:59:11 +02:00
|
|
|
reuse_existing_ip_allowed = module.params.get('reuse_existing_ip_allowed')
|
2015-08-31 18:06:18 +02:00
|
|
|
release_on_disassociation = module.params.get('release_on_disassociation')
|
|
|
|
|
2015-09-14 17:27:37 +02:00
|
|
|
if instance_id:
|
|
|
|
warnings = ["instance_id is no longer used, please use device_id going forward"]
|
|
|
|
is_instance = True
|
|
|
|
device_id = instance_id
|
|
|
|
else:
|
|
|
|
if device_id and device_id.startswith('i-'):
|
|
|
|
is_instance = True
|
|
|
|
elif device_id:
|
|
|
|
is_instance = False
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
try:
|
2015-08-31 18:06:18 +02:00
|
|
|
if device_id:
|
|
|
|
address = find_address(ec2, public_ip, device_id, isinstance=is_instance)
|
2015-08-05 20:22:09 +02:00
|
|
|
else:
|
|
|
|
address = False
|
2014-07-29 08:59:11 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
if state == 'present':
|
2015-08-31 18:06:18 +02:00
|
|
|
if device_id:
|
2016-07-31 20:54:21 +02:00
|
|
|
result = ensure_present(ec2, module, domain, address, device_id,
|
2015-08-05 20:22:09 +02:00
|
|
|
reuse_existing_ip_allowed,
|
2015-08-31 18:06:18 +02:00
|
|
|
module.check_mode, isinstance=is_instance)
|
2015-08-05 20:22:09 +02:00
|
|
|
else:
|
|
|
|
address = allocate_address(ec2, domain, reuse_existing_ip_allowed)
|
2016-01-02 12:16:46 +01:00
|
|
|
result = {'changed': True, 'public_ip': address.public_ip, 'allocation_id': address.allocation_id}
|
2014-07-29 08:59:11 +02:00
|
|
|
else:
|
2015-08-31 18:06:18 +02:00
|
|
|
if device_id:
|
|
|
|
disassociated = ensure_absent(ec2, domain, address, device_id, module.check_mode, isinstance=is_instance)
|
|
|
|
|
|
|
|
if release_on_disassociation and disassociated['changed']:
|
|
|
|
released = release_address(ec2, address, module.check_mode)
|
2015-09-14 17:27:37 +02:00
|
|
|
result = {'changed': True, 'disassociated': disassociated, 'released': released}
|
2015-08-31 18:06:18 +02:00
|
|
|
else:
|
2015-09-14 17:27:37 +02:00
|
|
|
result = {'changed': disassociated['changed'], 'disassociated': disassociated, 'released': {'changed': False}}
|
2015-08-05 20:22:09 +02:00
|
|
|
else:
|
|
|
|
address = find_address(ec2, public_ip, None)
|
2015-08-31 18:06:18 +02:00
|
|
|
released = release_address(ec2, address, module.check_mode)
|
2015-09-14 17:27:37 +02:00
|
|
|
result = {'changed': released['changed'], 'disassociated': {'changed': False}, 'released': released}
|
2015-08-05 20:22:09 +02:00
|
|
|
|
2014-11-14 05:05:49 +01:00
|
|
|
except (boto.exception.EC2ResponseError, EIPException) as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
2015-09-14 17:27:37 +02:00
|
|
|
if instance_id:
|
|
|
|
result['warnings'] = warnings
|
2014-11-14 05:05:49 +01:00
|
|
|
module.exit_json(**result)
|
2013-08-07 21:38:19 +02:00
|
|
|
|
2013-11-01 16:59:24 +01:00
|
|
|
# import module snippets
|
2014-11-14 04:10:23 +01:00
|
|
|
from ansible.module_utils.basic import * # noqa
|
|
|
|
from ansible.module_utils.ec2 import * # noqa
|
2013-08-07 21:38:19 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|