Skip to content

Commit

Permalink
Merge pull request ansible-collections#731 from tremble/eip_unbound
Browse files Browse the repository at this point in the history
Fix EIP allocation with in_vpc not set

SUMMARY
Our integration tests were setting in_vpc (because most use-cases need it) so we missed that setting the minimum possible configuration for an EIP resulted in an exception:
ec2_eip:
  state: present
  in_vpc: '{{ omit }}'

task path: /root/ansible_collections/community/aws/tests/output/.tmp/integration/ec2_eip-hkuj00lj-ÅÑŚÌβŁÈ/tests/integration/targets/ec2_eip/tasks/main.yml:666
<testhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<testhost> EXEC /bin/sh -c 'echo ~root && sleep 0'
<testhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289 `" && echo ansible-tmp-1632477941.356962-3535-26932306567289="` echo /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289 `" ) && sleep 0'
Using module file /root/ansible_collections/community/aws/plugins/modules/ec2_eip.py
<testhost> PUT /root/.ansible/tmp/ansible-local-1077my3ghuvo/tmp0lqxcnwq TO /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289/AnsiballZ_ec2_eip.py
<testhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289/ /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289/AnsiballZ_ec2_eip.py && sleep 0'
<testhost> EXEC /bin/sh -c 'ANSIBLE_DEBUG_BOTOCORE_LOGS=True /usr/bin/python3.9 /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289/AnsiballZ_ec2_eip.py && sleep 0'
<testhost> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1632477941.356962-3535-26932306567289/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_ec2_eip_payload_rqxrzx_j/ansible_ec2_eip_payload.zip/ansible_collections/community/aws/plugins/modules/ec2_eip.py", line 376, in allocate_address
  File "/tmp/ansible_ec2_eip_payload_rqxrzx_j/ansible_ec2_eip_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/core.py", line 334, in deciding_wrapper
    return retrying_wrapper(*args, **kwargs)
  File "/tmp/ansible_ec2_eip_payload_rqxrzx_j/ansible_ec2_eip_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/cloud.py", line 118, in _retry_wrapper
    return _retry_func(
  File "/tmp/ansible_ec2_eip_payload_rqxrzx_j/ansible_ec2_eip_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/cloud.py", line 68, in _retry_func
    return func()
  File "/usr/local/lib/python3.9/dist-packages/botocore/client.py", line 337, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.9/dist-packages/botocore/client.py", line 628, in _make_api_call
    request_dict = self._convert_to_request_dict(
  File "/usr/local/lib/python3.9/dist-packages/botocore/client.py", line 676, in _convert_to_request_dict
    request_dict = self._serializer.serialize_to_request(
  File "/usr/local/lib/python3.9/dist-packages/botocore/validate.py", line 297, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Domain, value: None, type: <class 'NoneType'>, valid types: <class 'str'>
fatal: [testhost]: FAILED! => {
    "boto3_version": "1.15.0",
    "botocore_version": "1.18.0",
    "changed": false,
    "invocation": {
        "module_args": {
            "allow_reassociation": false,
            "aws_access_key": "AKIAQAEICK57ETHUOKNO",
            "aws_ca_bundle": null,
            "aws_config": null,
            "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "debug_botocore_endpoint_logs": true,
            "device_id": null,
            "ec2_url": null,
            "in_vpc": false,
            "private_ip_address": null,
            "profile": null,
            "public_ip": null,
            "public_ipv4_pool": null,
            "region": "us-east-1",
            "release_on_disassociation": false,
            "reuse_existing_ip_allowed": false,
            "security_token": null,
            "state": "present",
            "tag_name": null,
            "tag_value": null,
            "validate_certs": true,
            "wait_timeout": null
        }
    },
    "msg": "Couldn't allocate Elastic IP address: Parameter validation failed:\nInvalid type for parameter Domain, value: None, type: <class 'NoneType'>, valid types: <class 'str'>",
    "resource_actions": []
}

ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
ec2_eip
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Mark Chappell <None>
Reviewed-by: None <None>
  • Loading branch information
ansible-zuul[bot] committed Sep 24, 2021
2 parents 0954679 + 093aa24 commit a55b7a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/731-ec2_eip-not_in_vpc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2_eip - fix bug when allocating an EIP but not associating it to a VPC (https://github.com/ansible-collections/community.aws/pull/731).
5 changes: 3 additions & 2 deletions plugins/modules/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,11 @@ def address_is_associated_with_device(ec2, module, address, device_id, is_instan

def allocate_address(ec2, module, domain, reuse_existing_ip_allowed, check_mode, tag_dict=None, public_ipv4_pool=None):
""" Allocate a new elastic IP address (when needed) and return it """
if not domain:
domain = 'standard'

if reuse_existing_ip_allowed:
filters = []
if not domain:
domain = 'standard'
filters.append({'Name': 'domain', "Values": [domain]})

if tag_dict is not None:
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/targets/ec2_eip/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,25 @@
state: absent
name: '{{ resource_prefix }}-vpc'
cidr_block: '{{ vpc_cidr }}'

- name: Create an EIP outside a VPC
ec2_eip:
state: present
in_vpc: '{{ omit }}'
register: unbound_eip
- assert:
that:
- unbound_eip is successful
- unbound_eip is changed
- name: Release EIP
ec2_eip:
state: absent
public_ip: '{{ unbound_eip.public_ip }}'
register: release_unbound_eip
- assert:
that:
- release_unbound_eip is successful
- release_unbound_eip is changed
# =====================================================
always:
- name: Cleanup instance (by id)
Expand Down Expand Up @@ -735,6 +754,12 @@
public_ip: '{{ no_tagged_eip.public_ip }}'
when: no_tagged_eip is changed
ignore_errors: true
- name: Cleanup unbound_eip
ec2_eip:
state: absent
public_ip: '{{ unbound_eip.public_ip }}'
when: unbound_eip is changed
ignore_errors: true
- name: Cleanup VPC
ec2_vpc_net:
state: absent
Expand Down

0 comments on commit a55b7a9

Please sign in to comment.