Fix EIP release in ec2_vpc_nat_gateway (#20167)

* Check if EIP exists before deleting it

After deleting the NAT gateway, the EIP sometimes seems to
cease to exist afterwards. Check if it exists before deleting it.

Otherwise you get:
```
Failed to release EIP eipalloc-abdc1234: An error occurred (InvalidAllocationID.NotFound) \
when calling the ReleaseAddress operation: The allocation ID 'eipalloc-abcd1234' does not \
exist", "success": false}
```

* Fix flake8 errors with ec2_vpc_nat_gateway
pull/20949/head
Will Thames 8 years ago committed by Ryan Brown
parent 0d2d25b515
commit 950ff3f24a

@ -14,6 +14,18 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# import module snippets
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import ec2_argument_spec, get_aws_connection_info, boto3_conn
import datetime
import random
import re
import time
from dateutil.tz import tzutc
ANSIBLE_METADATA = {'status': ['preview'], ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community', 'supported_by': 'community',
'version': '1.0'} 'version': '1.0'}
@ -216,12 +228,6 @@ try:
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
import datetime
import random
import re
import time
from dateutil.tz import tzutc
DRY_RUN_GATEWAYS = [ DRY_RUN_GATEWAYS = [
{ {
@ -662,11 +668,15 @@ def release_address(client, allocation_id, check_mode=False):
return True, '' return True, ''
ip_released = False ip_released = False
params = {
'AllocationId': allocation_id,
}
try: try:
client.release_address(**params) client.describe_addresses(AllocationIds=[allocation_id])
except botocore.exceptions.ClientError as e:
# IP address likely already released
# Happens with gateway in 'deleted' state that
# still lists associations
return True, str(e)
try:
client.release_address(AllocationId=allocation_id)
ip_released = True ip_released = True
except botocore.exceptions.ClientError as e: except botocore.exceptions.ClientError as e:
err_msg = str(e) err_msg = str(e)
@ -996,17 +1006,18 @@ def remove(client, nat_gateway_id, wait=False, wait_timeout=0,
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update(dict( argument_spec.update(
subnet_id=dict(type='str'), dict(
eip_address=dict(type='str'), subnet_id=dict(type='str'),
allocation_id=dict(type='str'), eip_address=dict(type='str'),
if_exist_do_not_create=dict(type='bool', default=False), allocation_id=dict(type='str'),
state=dict(default='present', choices=['present', 'absent']), if_exist_do_not_create=dict(type='bool', default=False),
wait=dict(type='bool', default=False), state=dict(default='present', choices=['present', 'absent']),
wait_timeout=dict(type='int', default=320, required=False), wait=dict(type='bool', default=False),
release_eip=dict(type='bool', default=False), wait_timeout=dict(type='int', default=320, required=False),
nat_gateway_id=dict(type='str'), release_eip=dict(type='bool', default=False),
client_token=dict(type='str'), nat_gateway_id=dict(type='str'),
client_token=dict(type='str'),
) )
) )
module = AnsibleModule( module = AnsibleModule(
@ -1081,9 +1092,6 @@ def main():
msg=err_msg, success=success, changed=changed, **results msg=err_msg, success=success, changed=changed, **results
) )
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save