| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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_eni_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about EC2 ENI interfaces in AWS |
| 12 |
description: |
| 13 |
- Gather information about EC2 ENI interfaces in AWS. |
| 14 |
author: |
| 15 |
- "Rob White (@wimnat)" |
| 16 |
options: |
| 17 |
eni_id: |
| 18 |
description: |
| 19 |
- The ID of the ENI. |
| 20 |
- This option is mutually exclusive of O(filters). |
| 21 |
type: str |
| 22 |
version_added: 1.3.0 |
| 23 |
filters: |
| 24 |
description: |
| 25 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 26 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNetworkInterfaces.html) for possible filters. |
| 27 |
- This option is mutually exclusive of O(eni_id). |
| 28 |
type: dict |
| 29 |
default: {} |
| 30 |
extends_documentation_fragment: |
| 31 |
- amazon.aws.common.modules |
| 32 |
- amazon.aws.region.modules |
| 33 |
- amazon.aws.boto3 |
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = r""" |
| 37 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 38 |
|
| 39 |
# Gather information about all ENIs |
| 40 |
- amazon.aws.ec2_eni_info: |
| 41 |
|
| 42 |
# Gather information about a particular ENI |
| 43 |
- amazon.aws.ec2_eni_info: |
| 44 |
filters: |
| 45 |
network-interface-id: eni-xxxxxxx |
| 46 |
""" |
| 47 |
|
| 48 |
RETURN = r""" |
| 49 |
network_interfaces: |
| 50 |
description: List of matching elastic network interfaces. |
| 51 |
returned: always |
| 52 |
type: complex |
| 53 |
contains: |
| 54 |
association: |
| 55 |
description: Info of associated elastic IP (EIP). |
| 56 |
returned: When an ENI is associated with an EIP |
| 57 |
type: dict |
| 58 |
sample: { |
| 59 |
allocation_id: "eipalloc-5sdf123", |
| 60 |
association_id: "eipassoc-8sdf123", |
| 61 |
ip_owner_id: "123456789012", |
| 62 |
public_dns_name: "ec2-52-1-0-63.compute-1.amazonaws.com", |
| 63 |
public_ip: "52.1.0.63" |
| 64 |
} |
| 65 |
attachment: |
| 66 |
description: Info about attached ec2 instance. |
| 67 |
returned: When an ENI is attached to an ec2 instance |
| 68 |
type: dict |
| 69 |
sample: { |
| 70 |
attach_time: "2017-08-05T15:25:47+00:00", |
| 71 |
attachment_id: "eni-attach-149d21234", |
| 72 |
delete_on_termination: false, |
| 73 |
device_index: 1, |
| 74 |
instance_id: "i-15b8d3cadbafa1234", |
| 75 |
instance_owner_id: "123456789012", |
| 76 |
"network_card_index": 0, |
| 77 |
status: "attached" |
| 78 |
} |
| 79 |
availability_zone: |
| 80 |
description: Availability zone of ENI. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: "us-east-1b" |
| 84 |
description: |
| 85 |
description: Description text for ENI. |
| 86 |
returned: always |
| 87 |
type: str |
| 88 |
sample: "My favourite network interface" |
| 89 |
groups: |
| 90 |
description: List of attached security groups. |
| 91 |
returned: always |
| 92 |
type: list |
| 93 |
sample: [ |
| 94 |
{ |
| 95 |
group_id: "sg-26d0f1234", |
| 96 |
group_name: "my_ec2_security_group" |
| 97 |
} |
| 98 |
] |
| 99 |
id: |
| 100 |
description: The id of the ENI (alias for RV(network_interfaces.network_interface_id)). |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: "eni-392fsdf" |
| 104 |
interface_type: |
| 105 |
description: Type of the network interface. |
| 106 |
returned: always |
| 107 |
type: str |
| 108 |
sample: "interface" |
| 109 |
ipv6_addresses: |
| 110 |
description: List of IPv6 addresses for this interface. |
| 111 |
returned: always |
| 112 |
type: list |
| 113 |
sample: [] |
| 114 |
mac_address: |
| 115 |
description: MAC address of the network interface. |
| 116 |
returned: always |
| 117 |
type: str |
| 118 |
sample: "0a:f8:10:2f:ab:a1" |
| 119 |
name: |
| 120 |
description: The Name tag of the ENI, often displayed in the AWS UIs as Name. |
| 121 |
returned: When a Name tag has been set |
| 122 |
type: str |
| 123 |
version_added: 1.3.0 |
| 124 |
network_interface_id: |
| 125 |
description: The id of the ENI. |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
sample: "eni-392fsdf" |
| 129 |
owner_id: |
| 130 |
description: AWS account id of the owner of the ENI. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: "123456789012" |
| 134 |
private_dns_name: |
| 135 |
description: Private DNS name for the ENI. |
| 136 |
returned: always |
| 137 |
type: str |
| 138 |
sample: "ip-172-16-1-180.ec2.internal" |
| 139 |
private_ip_address: |
| 140 |
description: Private IP address for the ENI. |
| 141 |
returned: always |
| 142 |
type: str |
| 143 |
sample: "172.16.1.180" |
| 144 |
private_ip_addresses: |
| 145 |
description: List of private IP addresses attached to the ENI. |
| 146 |
returned: always |
| 147 |
type: list |
| 148 |
sample: [] |
| 149 |
requester_id: |
| 150 |
description: The ID of the entity that launched the ENI. |
| 151 |
type: str |
| 152 |
sample: "AIDA12345EXAMPLE54321" |
| 153 |
requester_managed: |
| 154 |
description: Indicates whether the network interface is being managed by an AWS service. |
| 155 |
returned: always |
| 156 |
type: bool |
| 157 |
sample: false |
| 158 |
source_dest_check: |
| 159 |
description: Indicates whether the network interface performs source/destination checking. |
| 160 |
returned: always |
| 161 |
type: bool |
| 162 |
sample: false |
| 163 |
status: |
| 164 |
description: Indicates if the network interface is attached to an instance or not. |
| 165 |
returned: always |
| 166 |
type: str |
| 167 |
sample: "in-use" |
| 168 |
subnet_id: |
| 169 |
description: Subnet ID the ENI is in. |
| 170 |
returned: always |
| 171 |
type: str |
| 172 |
sample: "subnet-7bbf01234" |
| 173 |
tags: |
| 174 |
description: Dictionary of tags added to the ENI. |
| 175 |
returned: always |
| 176 |
type: dict |
| 177 |
sample: {} |
| 178 |
version_added: 1.3.0 |
| 179 |
tag_set: |
| 180 |
description: Dictionary of tags added to the ENI. |
| 181 |
returned: always |
| 182 |
type: dict |
| 183 |
sample: {} |
| 184 |
vpc_id: |
| 185 |
description: ID of the VPC the network interface it part of. |
| 186 |
returned: always |
| 187 |
type: str |
| 188 |
sample: "vpc-b3f1f123" |
| 189 |
""" |
| 190 |
|
| 191 |
from typing import Any |
| 192 |
from typing import Dict |
| 193 |
from typing import List |
| 194 |
|
| 195 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 196 |
|
| 197 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 198 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_network_interfaces |
| 199 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 200 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 201 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 202 |
|
| 203 |
|
| 204 |
def build_request_args(eni_id, filters): |
| 205 |
request_args = { |
| 206 |
"NetworkInterfaceIds": [eni_id] if eni_id else [], |
| 207 |
"Filters": ansible_dict_to_boto3_filter_list(filters), |
| 208 |
} |
| 209 |
|
| 210 |
request_args = {k: v for k, v in request_args.items() if v} |
| 211 |
|
| 212 |
return request_args |
| 213 |
|
| 214 |
|
| 215 |
def get_network_interfaces(connection, module: AnsibleAWSModule, request_args: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 216 |
try: |
| 217 |
network_interfaces_result = describe_network_interfaces(connection, **request_args) |
| 218 |
if not network_interfaces_result: |
| 219 |
module.exit_json(network_interfaces=[]) |
| 220 |
except AnsibleEC2Error as e: |
| 221 |
module.fail_json_aws(e) |
| 222 |
|
| 223 |
return network_interfaces_result |
| 224 |
|
| 225 |
|
| 226 |
def list_eni(connection, module: AnsibleAWSModule, request_args: Dict[str, Any]) -> List[Dict[str, Any]]: |
| 227 |
network_interfaces_result = get_network_interfaces(connection, module, request_args) |
| 228 |
|
| 229 |
# Modify boto3 tags list to be ansible friendly dict and then camel_case |
| 230 |
camel_network_interfaces = [] |
| 231 |
for network_interface in network_interfaces_result: |
| 232 |
network_interface["TagSet"] = boto3_tag_list_to_ansible_dict(network_interface["TagSet"]) |
| 233 |
network_interface["Tags"] = network_interface["TagSet"] |
| 234 |
if "Name" in network_interface["Tags"]: |
| 235 |
network_interface["Name"] = network_interface["Tags"]["Name"] |
| 236 |
# Added id to interface info to be compatible with return values of ec2_eni module: |
| 237 |
network_interface["Id"] = network_interface["NetworkInterfaceId"] |
| 238 |
camel_network_interfaces.append(camel_dict_to_snake_dict(network_interface, ignore_list=["Tags", "TagSet"])) |
| 239 |
|
| 240 |
return camel_network_interfaces |
| 241 |
|
| 242 |
|
| 243 |
def main(): |
| 244 |
argument_spec = dict( |
| 245 |
eni_id=dict(type="str"), |
| 246 |
filters=dict(default={}, type="dict"), |
| 247 |
) |
| 248 |
mutually_exclusive = [ |
| 249 |
["eni_id", "filters"], |
| 250 |
] |
| 251 |
|
| 252 |
module = AnsibleAWSModule( |
| 253 |
argument_spec=argument_spec, |
| 254 |
supports_check_mode=True, |
| 255 |
mutually_exclusive=mutually_exclusive, |
| 256 |
) |
| 257 |
|
| 258 |
connection = module.client("ec2") |
| 259 |
|
| 260 |
request_args = build_request_args( |
| 261 |
eni_id=module.params["eni_id"], |
| 262 |
filters=module.params["filters"], |
| 263 |
) |
| 264 |
|
| 265 |
result = list_eni(connection, module, request_args) |
| 266 |
|
| 267 |
module.exit_json(network_interfaces=result) |
| 268 |
|
| 269 |
|
| 270 |
if __name__ == "__main__": |
| 271 |
main() |