| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: ec2_eip_info |
| 10 |
version_added: 5.0.0 |
| 11 |
short_description: List EC2 EIP details |
| 12 |
description: |
| 13 |
- List details of EC2 Elastic IP addresses. |
| 14 |
- This module was originally added to C(community.aws) in release 1.0.0. |
| 15 |
author: |
| 16 |
- "Brad Macpherson (@iiibrad)" |
| 17 |
options: |
| 18 |
filters: |
| 19 |
description: |
| 20 |
- A dict of filters to apply. Each dict item consists of a filter key and filter |
| 21 |
value. See U(https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-addresses.html#options) |
| 22 |
for possible filters. Filter names and values are case sensitive. |
| 23 |
required: false |
| 24 |
default: {} |
| 25 |
type: dict |
| 26 |
extends_documentation_fragment: |
| 27 |
- amazon.aws.common.modules |
| 28 |
- amazon.aws.region.modules |
| 29 |
- amazon.aws.boto3 |
| 30 |
""" |
| 31 |
|
| 32 |
EXAMPLES = r""" |
| 33 |
# Note: These examples do not set authentication details or the AWS region, |
| 34 |
# see the AWS Guide for details. |
| 35 |
|
| 36 |
- name: List all EIP addresses in the current region. |
| 37 |
amazon.aws.ec2_eip_info: |
| 38 |
register: regional_eip_addresses |
| 39 |
|
| 40 |
- name: List all EIP addresses for a VM. |
| 41 |
amazon.aws.ec2_eip_info: |
| 42 |
filters: |
| 43 |
instance-id: i-123456789 |
| 44 |
register: my_vm_eips |
| 45 |
|
| 46 |
- ansible.builtin.debug: |
| 47 |
msg: "{{ my_vm_eips.addresses | selectattr('private_ip_address', 'equalto', '10.0.0.5') }}" |
| 48 |
|
| 49 |
- name: List all EIP addresses for several VMs. |
| 50 |
amazon.aws.ec2_eip_info: |
| 51 |
filters: |
| 52 |
instance-id: |
| 53 |
- i-123456789 |
| 54 |
- i-987654321 |
| 55 |
register: my_vms_eips |
| 56 |
|
| 57 |
- name: List all EIP addresses using the 'Name' tag as a filter. |
| 58 |
amazon.aws.ec2_eip_info: |
| 59 |
filters: |
| 60 |
tag:Name: www.example.com |
| 61 |
register: my_vms_eips |
| 62 |
|
| 63 |
- name: List all EIP addresses using the Allocation-id as a filter |
| 64 |
amazon.aws.ec2_eip_info: |
| 65 |
filters: |
| 66 |
allocation-id: eipalloc-64de1b01 |
| 67 |
register: my_vms_eips |
| 68 |
|
| 69 |
# Set the variable eip_alloc to the value of the first allocation_id |
| 70 |
# and set the variable my_pub_ip to the value of the first public_ip |
| 71 |
- ansible.builtin.set_fact: |
| 72 |
eip_alloc: my_vms_eips.addresses[0].allocation_id |
| 73 |
my_pub_ip: my_vms_eips.addresses[0].public_ip |
| 74 |
""" |
| 75 |
|
| 76 |
|
| 77 |
RETURN = r""" |
| 78 |
addresses: |
| 79 |
description: Properties of all Elastic IP addresses matching the provided filters. Each element is a dict with all the information related to an EIP. |
| 80 |
returned: on success |
| 81 |
type: list |
| 82 |
elements: dict |
| 83 |
contains: |
| 84 |
"allocation_id": |
| 85 |
description: The ID representing the allocation of the address. |
| 86 |
returned: always |
| 87 |
type: str |
| 88 |
sample: "eipalloc-64de1b01" |
| 89 |
"association_id": |
| 90 |
description: The ID representing the association of the address with an instance. |
| 91 |
type: str |
| 92 |
sample: "eipassoc-0fe9ce90d6e983e97" |
| 93 |
"domain": |
| 94 |
description: The network (vpc). |
| 95 |
type: str |
| 96 |
returned: always |
| 97 |
sample: "vpc" |
| 98 |
"instance_id": |
| 99 |
description: The ID of the instance that the address is associated with (if any). |
| 100 |
returned: if any instance is associated |
| 101 |
type: str |
| 102 |
sample: "i-01020cfeb25b0c84f" |
| 103 |
"network_border_group": |
| 104 |
description: The name of the unique set of Availability Zones, Local Zones, or Wavelength Zones from which Amazon Web Services advertises IP addresses. |
| 105 |
returned: if any instance is associated |
| 106 |
type: str |
| 107 |
sample: "us-east-1" |
| 108 |
"network_interface_id": |
| 109 |
description: The ID of the network interface. |
| 110 |
returned: if any instance is associated |
| 111 |
type: str |
| 112 |
sample: "eni-02fdeadfd4beef9323b" |
| 113 |
"network_interface_owner_id": |
| 114 |
description: The ID of the network interface. |
| 115 |
returned: if any instance is associated |
| 116 |
type: str |
| 117 |
sample: "0123456789" |
| 118 |
"private_ip_address": |
| 119 |
description: The private IP address associated with the Elastic IP address. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
sample: "10.0.0.1" |
| 123 |
"public_ip": |
| 124 |
description: The Elastic IP address. |
| 125 |
returned: if any instance is associated |
| 126 |
type: str |
| 127 |
sample: "54.81.104.1" |
| 128 |
"tags": |
| 129 |
description: Any tags assigned to the Elastic IP address. |
| 130 |
type: dict |
| 131 |
sample: { |
| 132 |
"Name": "test-vm-54.81.104.1" |
| 133 |
} |
| 134 |
""" |
| 135 |
|
| 136 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 137 |
|
| 138 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 139 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_addresses |
| 140 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 141 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 142 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 143 |
|
| 144 |
|
| 145 |
def get_eips_details(module): |
| 146 |
connection = module.client("ec2") |
| 147 |
filters = module.params.get("filters") |
| 148 |
try: |
| 149 |
addresses = describe_addresses(connection, Filters=ansible_dict_to_boto3_filter_list(filters)) |
| 150 |
except AnsibleEC2Error as e: |
| 151 |
module.fail_json_aws(e, msg="Error retrieving EIPs") |
| 152 |
|
| 153 |
addresses = [camel_dict_to_snake_dict(addr) for addr in addresses] |
| 154 |
for address in addresses: |
| 155 |
if "tags" in address: |
| 156 |
address["tags"] = boto3_tag_list_to_ansible_dict(address["tags"]) |
| 157 |
return addresses |
| 158 |
|
| 159 |
|
| 160 |
def main(): |
| 161 |
module = AnsibleAWSModule(argument_spec=dict(filters=dict(type="dict", default={})), supports_check_mode=True) |
| 162 |
|
| 163 |
module.exit_json(changed=False, addresses=get_eips_details(module)) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |