| 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 |
--- |
| 9 |
module: ec2_vpc_vpn_info |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Gather information about EC2 VPN Connections in AWS |
| 13 |
description: |
| 14 |
- Gather information about EC2 VPN Connections in AWS. |
| 15 |
author: |
| 16 |
- Madhura Naniwadekar (@Madhura-CSI) |
| 17 |
options: |
| 18 |
filters: |
| 19 |
description: |
| 20 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 21 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpnConnections.html) for possible filters. |
| 22 |
required: false |
| 23 |
type: dict |
| 24 |
default: {} |
| 25 |
vpn_connection_ids: |
| 26 |
description: |
| 27 |
- Get details of specific EC2 VPN Connection(s) using vpn connection ID/IDs. This value should be provided as a list. |
| 28 |
required: false |
| 29 |
type: list |
| 30 |
elements: str |
| 31 |
default: [] |
| 32 |
extends_documentation_fragment: |
| 33 |
- amazon.aws.common.modules |
| 34 |
- amazon.aws.region.modules |
| 35 |
- amazon.aws.boto3 |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
# # Note: These examples do not set authentication details, see the AWS Guide for details. |
| 40 |
- name: Gather information about all EC2 VPN Connections |
| 41 |
amazon.aws.ec2_vpc_vpn_info: |
| 42 |
|
| 43 |
- name: Gather information about a filtered list of EC2 VPN Connections, based on tags |
| 44 |
amazon.aws.ec2_vpc_vpn_info: |
| 45 |
filters: |
| 46 |
"tag:Name": "test-connection" |
| 47 |
register: vpn_conn_info |
| 48 |
|
| 49 |
- name: Gather information about EC2 VPN Connections by specifying connection IDs |
| 50 |
amazon.aws.ec2_vpc_vpn_info: |
| 51 |
filters: |
| 52 |
"vpn-gateway-id": "vgw-cbe66beb" |
| 53 |
register: vpn_conn_info |
| 54 |
""" |
| 55 |
|
| 56 |
RETURN = r""" |
| 57 |
vpn_connections: |
| 58 |
description: List of one or more EC2 VPN Connections. |
| 59 |
type: list |
| 60 |
elements: dict |
| 61 |
returned: always |
| 62 |
contains: |
| 63 |
category: |
| 64 |
description: The category of the VPN connection. |
| 65 |
returned: always |
| 66 |
type: str |
| 67 |
sample: "VPN" |
| 68 |
customer_gatway_configuration: |
| 69 |
description: The configuration information for the VPN connection's customer gateway (in the native XML format). |
| 70 |
returned: always |
| 71 |
type: str |
| 72 |
customer_gateway_id: |
| 73 |
description: The ID of the customer gateway at your end of the VPN connection. |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
sample: "cgw-17a53c37" |
| 77 |
gateway_association_state: |
| 78 |
description: The current state of the gateway association. |
| 79 |
type: str |
| 80 |
sample: "associated" |
| 81 |
options: |
| 82 |
description: The VPN connection options. |
| 83 |
type: list |
| 84 |
elements: dict |
| 85 |
contains: |
| 86 |
static_routes_only: |
| 87 |
description: If the VPN connection only allows static routes. |
| 88 |
type: bool |
| 89 |
sample: true |
| 90 |
enable_acceleration: |
| 91 |
description: Indicates whether acceleration is enabled for the VPN connection. |
| 92 |
type: bool |
| 93 |
sample: false |
| 94 |
local_ipv4_network_cidr: |
| 95 |
description: The IPv4 CIDR on the customer gateway (on-premises) side of the VPN connection. |
| 96 |
type: str |
| 97 |
sample: "0.0.0.0/0" |
| 98 |
outside_ip_address_type: |
| 99 |
description: The external IP address of the VPN tunnel. |
| 100 |
type: str |
| 101 |
sample: "PublicIpv4" |
| 102 |
remote_ipv4_network_cidr: |
| 103 |
description: The IPv4 CIDR on the Amazon Web Services side of the VPN connection. |
| 104 |
type: str |
| 105 |
sample: "0.0.0.0/0" |
| 106 |
tunnel_inside_ip_version: |
| 107 |
description: Indicates whether the VPN tunnels process IPv4 or IPv6 traffic. |
| 108 |
type: str |
| 109 |
sample: "ipv4" |
| 110 |
tunnel_options: |
| 111 |
description: Indicates the VPN tunnel options. |
| 112 |
type: list |
| 113 |
elements: dict |
| 114 |
sample: [ |
| 115 |
{ |
| 116 |
"log_options": { |
| 117 |
"cloud_watch_log_options": { |
| 118 |
"log_enabled": false |
| 119 |
} |
| 120 |
}, |
| 121 |
"outside_ip_address": "34.225.101.10", |
| 122 |
"pre_shared_key": "8n7hnjNE8zhIt4VpMOIfcrw6XnUTHLW9", |
| 123 |
"tunnel_inside_cidr": "169.254.31.8/30" |
| 124 |
}, |
| 125 |
] |
| 126 |
contains: |
| 127 |
log_options: |
| 128 |
description: Options for logging VPN tunnel activity. |
| 129 |
type: dict |
| 130 |
contains: |
| 131 |
cloud_watch_log_options: |
| 132 |
description: Options for sending VPN tunnel logs to CloudWatch. |
| 133 |
type: dict |
| 134 |
outside_ip_address: |
| 135 |
description: The external IP address of the VPN tunnel. |
| 136 |
type: str |
| 137 |
pre_shared_key: |
| 138 |
description: |
| 139 |
- The pre-shared key (PSK) to establish initial authentication between the |
| 140 |
virtual private gateway and the customer gateway. |
| 141 |
type: str |
| 142 |
tunnel_inside_cidr: |
| 143 |
description: The range of inside IPv4 addresses for the tunnel. |
| 144 |
type: str |
| 145 |
routes: |
| 146 |
description: List of static routes associated with the VPN connection. |
| 147 |
returned: always |
| 148 |
type: list |
| 149 |
elements: dict |
| 150 |
contains: |
| 151 |
destination_cidr_block: |
| 152 |
description: |
| 153 |
- The CIDR block associated with the local subnet of the customer data center. |
| 154 |
type: str |
| 155 |
source: |
| 156 |
description: Indicates how the routes were provided. |
| 157 |
type: str |
| 158 |
state: |
| 159 |
description: The current state of the static route. |
| 160 |
type: str |
| 161 |
state: |
| 162 |
description: The current state of the VPN connection. |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
sample: "available" |
| 166 |
tags: |
| 167 |
description: Any tags assigned to the VPN connection. |
| 168 |
returned: always |
| 169 |
type: dict |
| 170 |
sample: { |
| 171 |
"Name": "test-conn" |
| 172 |
} |
| 173 |
type: |
| 174 |
description: The type of VPN connection. |
| 175 |
returned: always |
| 176 |
type: str |
| 177 |
sample: "ipsec.1" |
| 178 |
vgw_telemetry: |
| 179 |
description: Information about the VPN tunnel. |
| 180 |
returned: always |
| 181 |
type: dict |
| 182 |
contains: |
| 183 |
accepted_route_count: |
| 184 |
description: The number of accepted routes. |
| 185 |
returned: always |
| 186 |
type: int |
| 187 |
sample: 0 |
| 188 |
last_status_change: |
| 189 |
description: The date and time of the last change in status. |
| 190 |
returned: always |
| 191 |
type: str |
| 192 |
sample: "2018-02-09T14:35:27+00:00" |
| 193 |
outside_ip_address: |
| 194 |
description: The Internet-routable IP address of the virtual private gateway's outside interface. |
| 195 |
returned: always |
| 196 |
type: str |
| 197 |
sample: "13.127.79.191" |
| 198 |
status: |
| 199 |
description: The status of the VPN tunnel. |
| 200 |
returned: always |
| 201 |
type: str |
| 202 |
sample: "DOWN" |
| 203 |
status_message: |
| 204 |
description: If an error occurs, a description of the error. |
| 205 |
returned: always |
| 206 |
type: str |
| 207 |
sample: "IPSEC IS DOWN" |
| 208 |
certificate_arn: |
| 209 |
description: The Amazon Resource Name of the virtual private gateway tunnel endpoint certificate. |
| 210 |
returned: when a private certificate is used for authentication |
| 211 |
type: str |
| 212 |
sample: "arn:aws:acm:us-east-1:123456789012:certificate/c544d8ce-20b8-4fff-98b0-example" |
| 213 |
vpn_connection_id: |
| 214 |
description: The ID of the VPN connection. |
| 215 |
returned: always |
| 216 |
type: str |
| 217 |
sample: "vpn-f700d5c0" |
| 218 |
vpn_gateway_id: |
| 219 |
description: The ID of the virtual private gateway at the AWS side of the VPN connection. |
| 220 |
returned: always |
| 221 |
type: str |
| 222 |
sample: "vgw-cbe56bfb" |
| 223 |
""" |
| 224 |
|
| 225 |
import json |
| 226 |
from typing import Any |
| 227 |
from typing import Dict |
| 228 |
from typing import NoReturn |
| 229 |
|
| 230 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 231 |
|
| 232 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 233 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpn_connections |
| 234 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 235 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 236 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 237 |
|
| 238 |
|
| 239 |
def date_handler(obj: Dict[str, Any]) -> Dict[str, Any]: |
| 240 |
return obj.isoformat() if hasattr(obj, "isoformat") else obj |
| 241 |
|
| 242 |
|
| 243 |
def list_vpn_connections(client, module: AnsibleAWSModule) -> NoReturn: |
| 244 |
params: Dict[str, Any] = {} |
| 245 |
|
| 246 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 247 |
params["VpnConnectionIds"] = module.params.get("vpn_connection_ids") |
| 248 |
|
| 249 |
try: |
| 250 |
result = json.loads(json.dumps(describe_vpn_connections(client, **params), default=date_handler)) |
| 251 |
except ValueError as e: |
| 252 |
module.fail_json(e, msg="Cannot validate JSON data") |
| 253 |
except AnsibleEC2Error as e: |
| 254 |
module.fail_json_aws(e, msg="Could not describe customer gateways") |
| 255 |
|
| 256 |
snaked_vpn_connections = [camel_dict_to_snake_dict(vpn_connection) for vpn_connection in result] |
| 257 |
if snaked_vpn_connections: |
| 258 |
for vpn_connection in snaked_vpn_connections: |
| 259 |
vpn_connection["tags"] = boto3_tag_list_to_ansible_dict(vpn_connection.get("tags", [])) |
| 260 |
|
| 261 |
module.exit_json(changed=False, vpn_connections=snaked_vpn_connections) |
| 262 |
|
| 263 |
|
| 264 |
def main(): |
| 265 |
argument_spec = dict( |
| 266 |
vpn_connection_ids=dict(default=[], type="list", elements="str"), |
| 267 |
filters=dict(default={}, type="dict"), |
| 268 |
) |
| 269 |
|
| 270 |
module = AnsibleAWSModule( |
| 271 |
argument_spec=argument_spec, |
| 272 |
mutually_exclusive=[["vpn_connection_ids", "filters"]], |
| 273 |
supports_check_mode=True, |
| 274 |
) |
| 275 |
|
| 276 |
connection = module.client("ec2") |
| 277 |
|
| 278 |
list_vpn_connections(connection, module) |
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__": |
| 282 |
main() |