| 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_vpc_net_info |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Gather information about EC2 VPCs in AWS |
| 12 |
description: |
| 13 |
- Gather information about EC2 VPCs in AWS. |
| 14 |
author: "Rob White (@wimnat)" |
| 15 |
options: |
| 16 |
vpc_ids: |
| 17 |
description: |
| 18 |
- A list of VPC IDs that exist in your account. |
| 19 |
type: list |
| 20 |
elements: str |
| 21 |
default: [] |
| 22 |
filters: |
| 23 |
description: |
| 24 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. |
| 25 |
See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpcs.html) for possible filters. |
| 26 |
type: dict |
| 27 |
default: {} |
| 28 |
extends_documentation_fragment: |
| 29 |
- amazon.aws.common.modules |
| 30 |
- amazon.aws.region.modules |
| 31 |
- amazon.aws.boto3 |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 36 |
|
| 37 |
# Gather information about all VPCs |
| 38 |
- amazon.aws.ec2_vpc_net_info: |
| 39 |
|
| 40 |
# Gather information about a particular VPC using VPC ID |
| 41 |
- amazon.aws.ec2_vpc_net_info: |
| 42 |
vpc_ids: vpc-00112233 |
| 43 |
|
| 44 |
# Gather information about any VPC with a tag key Name and value Example |
| 45 |
- amazon.aws.ec2_vpc_net_info: |
| 46 |
filters: |
| 47 |
"tag:Name": Example |
| 48 |
""" |
| 49 |
|
| 50 |
RETURN = r""" |
| 51 |
vpcs: |
| 52 |
description: Returns an array of complex objects as described below. |
| 53 |
returned: success |
| 54 |
type: complex |
| 55 |
contains: |
| 56 |
id: |
| 57 |
description: The ID of the VPC (for backwards compatibility). |
| 58 |
returned: always |
| 59 |
type: str |
| 60 |
vpc_id: |
| 61 |
description: The ID of the VPC. |
| 62 |
returned: always |
| 63 |
type: str |
| 64 |
state: |
| 65 |
description: The state of the VPC. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
tags: |
| 69 |
description: A dict of tags associated with the VPC. |
| 70 |
returned: always |
| 71 |
type: dict |
| 72 |
instance_tenancy: |
| 73 |
description: The instance tenancy setting for the VPC. |
| 74 |
returned: always |
| 75 |
type: str |
| 76 |
is_default: |
| 77 |
description: True if this is the default VPC for account. |
| 78 |
returned: always |
| 79 |
type: bool |
| 80 |
cidr_block: |
| 81 |
description: The IPv4 CIDR block assigned to the VPC. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
enable_dns_hostnames: |
| 85 |
description: True/False depending on attribute setting for DNS hostnames support. |
| 86 |
returned: always |
| 87 |
type: bool |
| 88 |
enable_dns_support: |
| 89 |
description: True/False depending on attribute setting for DNS support. |
| 90 |
returned: always |
| 91 |
type: bool |
| 92 |
cidr_block_association_set: |
| 93 |
description: An array of IPv4 cidr block association set information. |
| 94 |
returned: always |
| 95 |
type: complex |
| 96 |
contains: |
| 97 |
association_id: |
| 98 |
description: The association ID. |
| 99 |
returned: always |
| 100 |
type: str |
| 101 |
cidr_block: |
| 102 |
description: The IPv4 CIDR block that is associated with the VPC. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
cidr_block_state: |
| 106 |
description: A hash/dict that contains a single item. The state of the cidr block association. |
| 107 |
returned: always |
| 108 |
type: dict |
| 109 |
contains: |
| 110 |
state: |
| 111 |
description: The CIDR block association state. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
ipv6_cidr_block_association_set: |
| 115 |
description: An array of IPv6 cidr block association set information. |
| 116 |
returned: always |
| 117 |
type: complex |
| 118 |
contains: |
| 119 |
association_id: |
| 120 |
description: The association ID. |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
ipv6_cidr_block: |
| 124 |
description: The IPv6 CIDR block that is associated with the VPC. |
| 125 |
returned: always |
| 126 |
type: str |
| 127 |
ipv6_cidr_block_state: |
| 128 |
description: A hash/dict that contains a single item. The state of the cidr block association. |
| 129 |
returned: always |
| 130 |
type: dict |
| 131 |
contains: |
| 132 |
state: |
| 133 |
description: The CIDR block association state. |
| 134 |
returned: always |
| 135 |
type: str |
| 136 |
owner_id: |
| 137 |
description: The AWS account which owns the VPC. |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: 123456789012 |
| 141 |
dhcp_options_id: |
| 142 |
description: The ID of the DHCP options associated with this VPC. |
| 143 |
returned: always |
| 144 |
type: str |
| 145 |
sample: dopt-12345678 |
| 146 |
""" |
| 147 |
|
| 148 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 149 |
|
| 150 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 151 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpc_attribute |
| 152 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_vpcs |
| 153 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 154 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 155 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 156 |
|
| 157 |
|
| 158 |
def list_vpcs(connection, module: AnsibleAWSModule) -> None: |
| 159 |
""" |
| 160 |
Describe VPCs. |
| 161 |
|
| 162 |
connection : boto3 client connection object |
| 163 |
module : AnsibleAWSModule object |
| 164 |
""" |
| 165 |
# collect parameters |
| 166 |
filters = ansible_dict_to_boto3_filter_list(module.params.get("filters")) |
| 167 |
vpc_ids = module.params.get("vpc_ids") |
| 168 |
|
| 169 |
# init empty list for return vars |
| 170 |
vpc_info = list() |
| 171 |
|
| 172 |
# Get the basic VPC info |
| 173 |
try: |
| 174 |
vpcs = describe_vpcs(connection, VpcIds=vpc_ids, Filters=filters) |
| 175 |
except AnsibleEC2Error as e: |
| 176 |
module.fail_json_aws(e, msg=f"Unable to describe VPCs {vpc_ids}") |
| 177 |
|
| 178 |
# Loop through the results and add the other VPC attributes we gathered |
| 179 |
for vpc in vpcs: |
| 180 |
error_message = "Unable to describe VPC attribute {0} on VPC {1}" |
| 181 |
vpc_attributes = {} |
| 182 |
for attribute in ("enableDnsSupport", "enableDnsHostnames"): |
| 183 |
try: |
| 184 |
vpc_attributes[attribute] = describe_vpc_attribute(connection, vpc_id=vpc["VpcId"], attribute=attribute) |
| 185 |
if not vpc_attributes[attribute]: |
| 186 |
module.warn(error_message.format(attribute, vpc["VpcId"])) |
| 187 |
except AnsibleEC2Error as e: |
| 188 |
module.fail_json_aws(e, msg=error_message.format(attribute, vpc)) |
| 189 |
|
| 190 |
# add the two DNS attributes |
| 191 |
if vpc_attributes["enableDnsSupport"]: |
| 192 |
vpc["EnableDnsSupport"] = vpc_attributes["enableDnsSupport"]["EnableDnsSupport"].get("Value") |
| 193 |
if vpc_attributes["enableDnsHostnames"]: |
| 194 |
vpc["EnableDnsHostnames"] = vpc_attributes["enableDnsHostnames"]["EnableDnsHostnames"].get("Value") |
| 195 |
# for backwards compatibility |
| 196 |
vpc["id"] = vpc["VpcId"] |
| 197 |
vpc_info.append(camel_dict_to_snake_dict(vpc)) |
| 198 |
# convert tag list to ansible dict |
| 199 |
vpc_info[-1]["tags"] = boto3_tag_list_to_ansible_dict(vpc.get("Tags", [])) |
| 200 |
|
| 201 |
module.exit_json(vpcs=vpc_info) |
| 202 |
|
| 203 |
|
| 204 |
def main() -> None: |
| 205 |
argument_spec = dict( |
| 206 |
vpc_ids=dict(type="list", elements="str", default=[]), |
| 207 |
filters=dict(type="dict", default={}), |
| 208 |
) |
| 209 |
|
| 210 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 211 |
connection = module.client("ec2") |
| 212 |
|
| 213 |
list_vpcs(connection, module) |
| 214 |
|
| 215 |
|
| 216 |
if __name__ == "__main__": |
| 217 |
main() |