| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 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 |
module: ec2_vpc_nat_gateway_info |
| 9 |
short_description: Retrieves AWS VPC Managed Nat Gateway details using AWS methods |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Gets various details related to AWS VPC Managed Nat Gateways. |
| 13 |
options: |
| 14 |
nat_gateway_ids: |
| 15 |
description: |
| 16 |
- List of specific nat gateway IDs to fetch details for. |
| 17 |
type: list |
| 18 |
elements: str |
| 19 |
default: [] |
| 20 |
filters: |
| 21 |
description: |
| 22 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 23 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNatGateways.html) |
| 24 |
for possible filters. |
| 25 |
type: dict |
| 26 |
default: {} |
| 27 |
author: Karen Cheng (@Etherdaemon) |
| 28 |
extends_documentation_fragment: |
| 29 |
- amazon.aws.common.modules |
| 30 |
- amazon.aws.region.modules |
| 31 |
- amazon.aws.boto3 |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
# Simple example of listing all nat gateways |
| 36 |
- name: List all managed nat gateways in ap-southeast-2 |
| 37 |
amazon.aws.ec2_vpc_nat_gateway_info: |
| 38 |
region: ap-southeast-2 |
| 39 |
register: all_ngws |
| 40 |
|
| 41 |
- name: Debugging the result |
| 42 |
ansible.builtin.debug: |
| 43 |
msg: "{{ all_ngws.result }}" |
| 44 |
|
| 45 |
- name: Get details on specific nat gateways |
| 46 |
amazon.aws.ec2_vpc_nat_gateway_info: |
| 47 |
nat_gateway_ids: |
| 48 |
- nat-1234567891234567 |
| 49 |
- nat-7654321987654321 |
| 50 |
region: ap-southeast-2 |
| 51 |
register: specific_ngws |
| 52 |
|
| 53 |
- name: Get all nat gateways with specific filters |
| 54 |
amazon.aws.ec2_vpc_nat_gateway_info: |
| 55 |
region: ap-southeast-2 |
| 56 |
filters: |
| 57 |
state: ['pending'] |
| 58 |
register: pending_ngws |
| 59 |
|
| 60 |
- name: Get nat gateways with specific filter |
| 61 |
amazon.aws.ec2_vpc_nat_gateway_info: |
| 62 |
region: ap-southeast-2 |
| 63 |
filters: |
| 64 |
subnet-id: subnet-12345678 |
| 65 |
state: ['available'] |
| 66 |
register: existing_nat_gateways |
| 67 |
""" |
| 68 |
|
| 69 |
RETURN = r""" |
| 70 |
changed: |
| 71 |
description: True if listing the internet gateways succeeds. |
| 72 |
type: bool |
| 73 |
returned: always |
| 74 |
sample: false |
| 75 |
result: |
| 76 |
description: |
| 77 |
- The result of the describe, converted to ansible snake case style. |
| 78 |
- See also U(http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.describe_nat_gateways). |
| 79 |
returned: suceess |
| 80 |
type: list |
| 81 |
contains: |
| 82 |
connectivity_type: |
| 83 |
description: |
| 84 |
- Indicates whether the NAT gateway supports public or private connectivity. |
| 85 |
returned: always |
| 86 |
type: str |
| 87 |
sample: public |
| 88 |
create_time: |
| 89 |
description: The date and time the NAT gateway was created. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: "2021-03-11T22:43:25+00:00" |
| 93 |
delete_time: |
| 94 |
description: The date and time the NAT gateway was deleted. |
| 95 |
returned: when the NAT gateway has been deleted |
| 96 |
type: str |
| 97 |
sample: "2021-03-11T22:43:25+00:00" |
| 98 |
nat_gateway_addresses: |
| 99 |
description: List containing a dictionary with the IP addresses and network interface associated with the NAT gateway. |
| 100 |
returned: always |
| 101 |
type: dict |
| 102 |
contains: |
| 103 |
allocation_id: |
| 104 |
description: The allocation ID of the Elastic IP address that's associated with the NAT gateway. |
| 105 |
returned: always |
| 106 |
type: str |
| 107 |
sample: eipalloc-0853e66a40803da76 |
| 108 |
association_id: |
| 109 |
description: The association ID of the Elastic IP address that is associated with the NAT gateway. |
| 110 |
returned: always |
| 111 |
type: str |
| 112 |
sample: eipassoc-0d6365c7eeb7d4932 |
| 113 |
is_primary: |
| 114 |
description: Defines if the IP address is the primary address. |
| 115 |
returned: always |
| 116 |
type: bool |
| 117 |
sample: true |
| 118 |
network_interface_id: |
| 119 |
description: The ID of the network interface associated with the NAT gateway. |
| 120 |
returned: always |
| 121 |
type: str |
| 122 |
sample: eni-0a37acdbe306c661c |
| 123 |
private_ip: |
| 124 |
description: The private IP address associated with the Elastic IP address. |
| 125 |
returned: always |
| 126 |
type: str |
| 127 |
sample: 10.0.238.227 |
| 128 |
public_ip: |
| 129 |
description: The Elastic IP address associated with the NAT gateway. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
sample: 34.204.123.52 |
| 133 |
status: |
| 134 |
description: The address status. |
| 135 |
returned: always |
| 136 |
type: str |
| 137 |
sample: succeeded |
| 138 |
nat_gateway_id: |
| 139 |
description: The ID of the NAT gateway. |
| 140 |
returned: always |
| 141 |
type: str |
| 142 |
sample: nat-0c242a2397acf6173 |
| 143 |
state: |
| 144 |
description: state of the NAT gateway. |
| 145 |
returned: always |
| 146 |
type: str |
| 147 |
sample: available |
| 148 |
subnet_id: |
| 149 |
description: The ID of the subnet in which the NAT gateway is located. |
| 150 |
returned: always |
| 151 |
type: str |
| 152 |
sample: subnet-098c447465d4344f9 |
| 153 |
vpc_id: |
| 154 |
description: The ID of the VPC in which the NAT gateway is located. |
| 155 |
returned: always |
| 156 |
type: str |
| 157 |
sample: vpc-02f37f48438ab7d4c |
| 158 |
tags: |
| 159 |
description: Tags applied to the NAT gateway. |
| 160 |
returned: always |
| 161 |
type: dict |
| 162 |
sample: |
| 163 |
Tag1: tag1 |
| 164 |
Tag_2: tag_2 |
| 165 |
""" |
| 166 |
|
| 167 |
|
| 168 |
try: |
| 169 |
import botocore |
| 170 |
except ImportError: |
| 171 |
pass # Handled by AnsibleAWSModule |
| 172 |
|
| 173 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 174 |
|
| 175 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import normalize_boto3_result |
| 176 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 177 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_nat_gateways |
| 178 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 179 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 180 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 181 |
|
| 182 |
|
| 183 |
def get_nat_gateways(client, module: AnsibleAWSModule) -> None: |
| 184 |
params = dict() |
| 185 |
nat_gateways = list() |
| 186 |
|
| 187 |
params["Filter"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 188 |
params["NatGatewayIds"] = module.params.get("nat_gateway_ids") |
| 189 |
|
| 190 |
try: |
| 191 |
result = normalize_boto3_result(describe_nat_gateways(client, **params)) |
| 192 |
except AnsibleEC2Error as e: |
| 193 |
module.fail_json_aws(e, "Unable to describe NAT gateways.") |
| 194 |
|
| 195 |
for gateway in result: |
| 196 |
# Turn the boto3 result into ansible_friendly_snaked_names |
| 197 |
converted_gateway = camel_dict_to_snake_dict(gateway) |
| 198 |
if "tags" in converted_gateway: |
| 199 |
# Turn the boto3 result into ansible friendly tag dictionary |
| 200 |
converted_gateway["tags"] = boto3_tag_list_to_ansible_dict(converted_gateway["tags"]) |
| 201 |
nat_gateways.append(converted_gateway) |
| 202 |
|
| 203 |
return module.exit_json(result=nat_gateways) |
| 204 |
|
| 205 |
|
| 206 |
def main() -> None: |
| 207 |
argument_spec = dict( |
| 208 |
filters=dict(default={}, type="dict"), |
| 209 |
nat_gateway_ids=dict(default=[], type="list", elements="str"), |
| 210 |
) |
| 211 |
|
| 212 |
module = AnsibleAWSModule( |
| 213 |
argument_spec=argument_spec, |
| 214 |
supports_check_mode=True, |
| 215 |
) |
| 216 |
|
| 217 |
try: |
| 218 |
connection = module.client("ec2") |
| 219 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 220 |
module.fail_json_aws(e, msg="Failed to connect to AWS") |
| 221 |
|
| 222 |
get_nat_gateways(connection, module) |
| 223 |
|
| 224 |
|
| 225 |
if __name__ == "__main__": |
| 226 |
main() |