| 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_peering_info |
| 9 |
short_description: Retrieves AWS VPC Peering details using AWS methods |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
description: |
| 13 |
- Gets various details related to AWS VPC Peers |
| 14 |
options: |
| 15 |
peer_connection_ids: |
| 16 |
description: |
| 17 |
- List of specific VPC peer IDs to get details for. |
| 18 |
type: list |
| 19 |
elements: str |
| 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_DescribeVpcPeeringConnections.html) |
| 24 |
for possible filters. |
| 25 |
type: dict |
| 26 |
default: {} |
| 27 |
author: |
| 28 |
- Karen Cheng (@Etherdaemon) |
| 29 |
- Alina Buzachis (@alinabuzachis) |
| 30 |
extends_documentation_fragment: |
| 31 |
- amazon.aws.common.modules |
| 32 |
- amazon.aws.region.modules |
| 33 |
- amazon.aws.boto3 |
| 34 |
""" |
| 35 |
|
| 36 |
EXAMPLES = r""" |
| 37 |
- name: List all EC2 VPC Peering Connections |
| 38 |
amazon.aws.ec2_vpc_peering_info: |
| 39 |
region: ap-southeast-2 |
| 40 |
register: all_vpc_peers |
| 41 |
|
| 42 |
- name: Debugging the result |
| 43 |
ansible.builtin.debug: |
| 44 |
msg: "{{ all_vpc_peers.result }}" |
| 45 |
|
| 46 |
- name: Get details on specific EC2 VPC Peering Connection |
| 47 |
amazon.aws.ec2_vpc_peering_info: |
| 48 |
peer_connection_ids: |
| 49 |
- "pcx-12345678" |
| 50 |
- "pcx-87654321" |
| 51 |
region: "ap-southeast-2" |
| 52 |
register: all_vpc_peers |
| 53 |
|
| 54 |
- name: Get all EC2 VPC Peering Connections with specific filters |
| 55 |
amazon.aws.ec2_vpc_peering_info: |
| 56 |
region: "ap-southeast-2" |
| 57 |
filters: |
| 58 |
status-code: ["pending-acceptance"] |
| 59 |
register: pending_vpc_peers |
| 60 |
""" |
| 61 |
|
| 62 |
RETURN = r""" |
| 63 |
vpc_peering_connections: |
| 64 |
description: Details of the matching VPC peering connections. |
| 65 |
returned: success |
| 66 |
type: list |
| 67 |
elements: dict |
| 68 |
contains: |
| 69 |
accepter_vpc_info: |
| 70 |
description: Information about the VPC which accepted the connection. |
| 71 |
returned: success |
| 72 |
type: dict |
| 73 |
contains: |
| 74 |
cidr_block: |
| 75 |
description: The primary CIDR for the VPC. |
| 76 |
returned: when connection is in the accepted state. |
| 77 |
type: str |
| 78 |
sample: "10.10.10.0/23" |
| 79 |
cidr_block_set: |
| 80 |
description: A list of all CIDRs for the VPC. |
| 81 |
returned: when connection is in the accepted state. |
| 82 |
type: list |
| 83 |
elements: dict |
| 84 |
contains: |
| 85 |
cidr_block: |
| 86 |
description: A CIDR block used by the VPC. |
| 87 |
returned: success |
| 88 |
type: str |
| 89 |
sample: "10.10.10.0/23" |
| 90 |
owner_id: |
| 91 |
description: The AWS account that owns the VPC. |
| 92 |
returned: success |
| 93 |
type: str |
| 94 |
sample: "123456789012" |
| 95 |
peering_options: |
| 96 |
description: Additional peering configuration. |
| 97 |
returned: when connection is in the accepted state. |
| 98 |
type: dict |
| 99 |
contains: |
| 100 |
allow_dns_resolution_from_remote_vpc: |
| 101 |
description: Indicates whether a VPC can resolve public DNS hostnames to private IP addresses when queried from instances in a peer VPC. |
| 102 |
returned: success |
| 103 |
type: bool |
| 104 |
allow_egress_from_local_classic_link_to_remote_vpc: |
| 105 |
description: Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering connection. |
| 106 |
returned: success |
| 107 |
type: bool |
| 108 |
allow_egress_from_local_vpc_to_remote_classic_link: |
| 109 |
description: Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering connection. |
| 110 |
returned: success |
| 111 |
type: bool |
| 112 |
region: |
| 113 |
description: The AWS region that the VPC is in. |
| 114 |
returned: success |
| 115 |
type: str |
| 116 |
sample: "us-east-1" |
| 117 |
vpc_id: |
| 118 |
description: The ID of the VPC |
| 119 |
returned: success |
| 120 |
type: str |
| 121 |
sample: "vpc-0123456789abcdef0" |
| 122 |
requester_vpc_info: |
| 123 |
description: Information about the VPC which requested the connection. |
| 124 |
returned: success |
| 125 |
type: dict |
| 126 |
contains: |
| 127 |
cidr_block: |
| 128 |
description: The primary CIDR for the VPC. |
| 129 |
returned: when connection is not in the deleted state. |
| 130 |
type: str |
| 131 |
sample: "10.10.10.0/23" |
| 132 |
cidr_block_set: |
| 133 |
description: A list of all CIDRs for the VPC. |
| 134 |
returned: when connection is not in the deleted state. |
| 135 |
type: list |
| 136 |
elements: dict |
| 137 |
contains: |
| 138 |
cidr_block: |
| 139 |
description: A CIDR block used by the VPC |
| 140 |
returned: success |
| 141 |
type: str |
| 142 |
sample: "10.10.10.0/23" |
| 143 |
owner_id: |
| 144 |
description: The AWS account that owns the VPC. |
| 145 |
returned: success |
| 146 |
type: str |
| 147 |
sample: "123456789012" |
| 148 |
peering_options: |
| 149 |
description: Additional peering configuration. |
| 150 |
returned: when connection is not in the deleted state. |
| 151 |
type: dict |
| 152 |
contains: |
| 153 |
allow_dns_resolution_from_remote_vpc: |
| 154 |
description: Indicates whether a VPC can resolve public DNS hostnames to private IP addresses when queried from instances in a peer VPC. |
| 155 |
returned: success |
| 156 |
type: bool |
| 157 |
allow_egress_from_local_classic_link_to_remote_vpc: |
| 158 |
description: Indicates whether a local ClassicLink connection can communicate with the peer VPC over the VPC peering connection. |
| 159 |
returned: success |
| 160 |
type: bool |
| 161 |
allow_egress_from_local_vpc_to_remote_classic_link: |
| 162 |
description: Indicates whether a local VPC can communicate with a ClassicLink connection in the peer VPC over the VPC peering connection. |
| 163 |
returned: success |
| 164 |
type: bool |
| 165 |
region: |
| 166 |
description: The AWS region that the VPC is in. |
| 167 |
returned: success |
| 168 |
type: str |
| 169 |
sample: "us-east-1" |
| 170 |
vpc_id: |
| 171 |
description: The ID of the VPC |
| 172 |
returned: success |
| 173 |
type: str |
| 174 |
sample: "vpc-0123456789abcdef0" |
| 175 |
status: |
| 176 |
description: Details of the current status of the connection. |
| 177 |
returned: success |
| 178 |
type: dict |
| 179 |
contains: |
| 180 |
code: |
| 181 |
description: A short code describing the status of the connection. |
| 182 |
returned: success |
| 183 |
type: str |
| 184 |
sample: "active" |
| 185 |
message: |
| 186 |
description: Additional information about the status of the connection. |
| 187 |
returned: success |
| 188 |
type: str |
| 189 |
sample: "Pending Acceptance by 123456789012" |
| 190 |
tags: |
| 191 |
description: Tags applied to the connection. |
| 192 |
returned: success |
| 193 |
type: dict |
| 194 |
vpc_peering_connection_id: |
| 195 |
description: The ID of the VPC peering connection. |
| 196 |
returned: success |
| 197 |
type: str |
| 198 |
sample: "pcx-0123456789abcdef0" |
| 199 |
""" |
| 200 |
|
| 201 |
|
| 202 |
from typing import Any |
| 203 |
from typing import Dict |
| 204 |
from typing import List |
| 205 |
|
| 206 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 207 |
|
| 208 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import normalize_boto3_result |
| 209 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_peering_connections |
| 210 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 211 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 212 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 213 |
|
| 214 |
|
| 215 |
def get_vpc_peers(client, module: AnsibleAWSModule) -> List[Dict[str, Any]]: |
| 216 |
params: Dict = {} |
| 217 |
params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 218 |
|
| 219 |
if module.params.get("peer_connection_ids"): |
| 220 |
params["VpcPeeringConnectionIds"] = module.params.get("peer_connection_ids") |
| 221 |
|
| 222 |
result = describe_vpc_peering_connections(client, **params) |
| 223 |
|
| 224 |
return normalize_boto3_result(result) |
| 225 |
|
| 226 |
|
| 227 |
def main(): |
| 228 |
argument_spec = dict( |
| 229 |
filters=dict(default=dict(), type="dict"), |
| 230 |
peer_connection_ids=dict(default=None, type="list", elements="str"), |
| 231 |
) |
| 232 |
|
| 233 |
module = AnsibleAWSModule( |
| 234 |
argument_spec=argument_spec, |
| 235 |
supports_check_mode=True, |
| 236 |
) |
| 237 |
|
| 238 |
client = module.client("ec2") |
| 239 |
|
| 240 |
# Turn the boto3 result in to ansible friendly_snaked_names |
| 241 |
results = [camel_dict_to_snake_dict(peer) for peer in get_vpc_peers(client, module)] |
| 242 |
|
| 243 |
# Turn the boto3 result in to ansible friendly tag dictionary |
| 244 |
for peer in results: |
| 245 |
peer["tags"] = boto3_tag_list_to_ansible_dict(peer.get("tags", [])) |
| 246 |
|
| 247 |
module.exit_json(vpc_peering_connections=results) |
| 248 |
|
| 249 |
|
| 250 |
if __name__ == "__main__": |
| 251 |
main() |