Skip to content

Commit

Permalink
ec2_eip tagging support (ansible-collections#332)
Browse files Browse the repository at this point in the history
ec2_eip tagging support

SUMMARY
Add support for tagging EIPs on creation.
Todo:

 EIP Tagging
 Tests
 Retry decorator

Note: While it's now possible to pass tagging information into the association call this was only added Dec 2020 so won't work for most folks.
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_eip
ec2_eip_info
ADDITIONAL INFORMATION
fixes: ansible-collections#331

Reviewed-by: Rick Mendes <None>
Reviewed-by: None <None>

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@02836f3
  • Loading branch information
tremble authored and goneri committed Sep 21, 2022
1 parent 4a9af2e commit 60b080c
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 34 deletions.
22 changes: 21 additions & 1 deletion plugins/modules/ec2_eip.py
Expand Up @@ -64,6 +64,16 @@
network interface or instance to be re-associated with the specified instance or interface.
default: false
type: bool
tags:
description: A dictionary of tags to apply to the EIP.
type: dict
version_added: 2.1.0
purge_tags:
description: Whether the I(tags) argument should cause tags not in the
dictionary to be removed.
default: True
type: bool
version_added: 2.1.0
tag_name:
description:
- When I(reuse_existing_ip_allowed=true), supplement with this option to only reuse
Expand Down Expand Up @@ -227,6 +237,7 @@
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ensure_ec2_tags


def associate_ip_and_device(ec2, module, address, private_ip_address, device_id, allow_reassociation, check_mode, is_instance=True):
Expand All @@ -247,7 +258,7 @@ def associate_ip_and_device(ec2, module, address, private_ip_address, device_id,
params['AllocationId'] = address['AllocationId']
else:
params['PublicIp'] = address['PublicIp']
res = ec2.associate_address(**params)
res = ec2.associate_address(aws_retry=True, **params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
msg = "Couldn't associate Elastic IP address with instance '{0}'".format(device_id)
module.fail_json_aws(e, msg=msg)
Expand Down Expand Up @@ -535,6 +546,8 @@ def main():
allow_reassociation=dict(type='bool', default=False),
wait_timeout=dict(type='int', removed_at_date='2022-06-01', removed_from_collection='community.aws'),
private_ip_address=dict(),
tags=dict(required=False, type='dict'),
purge_tags=dict(required=False, type='bool', default=True),
tag_name=dict(),
tag_value=dict(),
public_ipv4_pool=dict()
Expand Down Expand Up @@ -563,6 +576,8 @@ def main():
tag_name = module.params.get('tag_name')
tag_value = module.params.get('tag_value')
public_ipv4_pool = module.params.get('public_ipv4_pool')
tags = module.params.get('tags')
purge_tags = module.params.get('purge_tags')

if instance_id:
is_instance = True
Expand All @@ -575,6 +590,7 @@ def main():
module.fail_json(msg="If you are specifying an ENI, in_vpc must be true")
is_instance = False

# Tags for *searching* for an EIP.
tag_dict = generate_tag_dict(module, tag_name, tag_value)

try:
Expand Down Expand Up @@ -603,6 +619,10 @@ def main():
'public_ip': address['PublicIp'],
'allocation_id': address['AllocationId']
}

result['changed'] |= ensure_ec2_tags(
ec2, module, result['allocation_id'],
resource_type='elastic-ip', tags=tags, purge_tags=purge_tags)
else:
if device_id:
disassociated = ensure_absent(
Expand Down
15 changes: 9 additions & 6 deletions plugins/modules/ec2_eip_info.py
Expand Up @@ -97,22 +97,25 @@
'''

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (ansible_dict_to_boto3_filter_list,
boto3_tag_list_to_ansible_dict,
camel_dict_to_snake_dict,
)
try:
from botocore.exceptions import (BotoCoreError, ClientError)
except ImportError:
pass # caught by imported AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ansible_dict_to_boto3_filter_list
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict


def get_eips_details(module):
connection = module.client('ec2')
connection = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff())
filters = module.params.get("filters")
try:
response = connection.describe_addresses(
aws_retry=True,
Filters=ansible_dict_to_boto3_filter_list(filters)
)
except (BotoCoreError, ClientError) as e:
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_eip/defaults/main.yml
Expand Up @@ -3,3 +3,4 @@
# run multiple copies of the test concurrently.
vpc_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/16'
subnet_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.42.0/24'
subnet_az: '{{ ec2_availability_zone_names[0] }}'
2 changes: 1 addition & 1 deletion tests/integration/targets/ec2_eip/meta/main.yml
@@ -1,3 +1,3 @@
dependencies:
- prepare_tests
- setup_ec2
- setup_ec2_facts

0 comments on commit 60b080c

Please sign in to comment.