Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/ec2_vpc_subnet_info.py

raw
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_subnet_info
10 version_added: 1.0.0
11 short_description: Gather information about EC2 VPC subnets in AWS
12 description:
13 - Gather information about EC2 VPC subnets in AWS.
14 author: "Rob White (@wimnat)"
15 options:
16 subnet_ids:
17 description:
18 - A list of subnet IDs to gather information for.
19 aliases: ['subnet_id']
20 type: list
21 elements: str
22 default: []
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_DescribeSubnets.html) for possible filters.
27 type: dict
28 default: {}
29 extends_documentation_fragment:
30 - amazon.aws.common.modules
31 - amazon.aws.region.modules
32 - amazon.aws.boto3
33 """
34
35 EXAMPLES = r"""
36 # Note: These examples do not set authentication details, see the AWS Guide for details.
37
38 # Gather information about all VPC subnets
39 - amazon.aws.ec2_vpc_subnet_info:
40
41 # Gather information about a particular VPC subnet using ID
42 - amazon.aws.ec2_vpc_subnet_info:
43 subnet_ids: subnet-00112233
44
45 # Gather information about any VPC subnet with a tag key Name and value Example
46 - amazon.aws.ec2_vpc_subnet_info:
47 filters:
48 "tag:Name": Example
49
50 # Gather information about any VPC subnet within VPC with ID vpc-abcdef00
51 - amazon.aws.ec2_vpc_subnet_info:
52 filters:
53 vpc-id: vpc-abcdef00
54
55 # Gather information about a set of VPC subnets, publicA, publicB and publicC within a
56 # VPC with ID vpc-abcdef00 and then use the jinja map function to return the
57 # subnet_ids as a list.
58
59 - amazon.aws.ec2_vpc_subnet_info:
60 filters:
61 vpc-id: vpc-abcdef00
62 "tag:Name": "{{ item }}"
63 loop:
64 - publicA
65 - publicB
66 - publicC
67 register: subnet_info
68
69 - set_fact:
70 subnet_ids: "{{ subnet_info.results | sum(attribute='subnets', start=[]) | map(attribute='subnet_id') }}"
71 """
72
73 RETURN = r"""
74 subnets:
75 description: Returns an array of complex objects as described below.
76 returned: success
77 type: complex
78 contains:
79 id:
80 description: Subnet resource id.
81 returned: always
82 type: str
83 sample: subnet-b883b2c4
84 cidr_block:
85 description: The IPv4 CIDR of the Subnet.
86 returned: always
87 type: str
88 sample: "10.0.0.0/16"
89 ipv6_cidr_block:
90 description: The IPv6 CIDR block actively associated with the Subnet.
91 returned: always
92 type: str
93 sample: "2001:db8:0:102::/64"
94 availability_zone:
95 description: Availability zone of the Subnet.
96 returned: always
97 type: str
98 sample: us-east-1a
99 availability_zone_id:
100 description: The AZ ID of the subnet.
101 returned: always
102 type: str
103 sample: use1-az6
104 state:
105 description: state of the Subnet.
106 returned: always
107 type: str
108 sample: available
109 tags:
110 description: tags attached to the Subnet, includes name.
111 returned: always
112 type: dict
113 sample: {"Name": "My Subnet", "env": "staging"}
114 map_public_ip_on_launch:
115 description: whether public IP is auto-assigned to new instances.
116 returned: always
117 type: bool
118 sample: false
119 assign_ipv6_address_on_creation:
120 description: whether IPv6 address is auto-assigned to new instances.
121 returned: always
122 type: bool
123 sample: false
124 vpc_id:
125 description: the id of the VPC where this Subnet exists.
126 returned: always
127 type: str
128 sample: vpc-67236184
129 available_ip_address_count:
130 description: number of available IPv4 addresses.
131 returned: always
132 type: str
133 sample: 251
134 default_for_az:
135 description: indicates whether this is the default Subnet for this Availability Zone.
136 returned: always
137 type: bool
138 sample: false
139 enable_dns64:
140 description:
141 - Indicates whether DNS queries made should return synthetic IPv6 addresses for IPv4-only destinations.
142 type: bool
143 sample: false
144 ipv6_association_id:
145 description: The IPv6 association ID for the currently associated CIDR.
146 returned: always
147 type: str
148 sample: subnet-cidr-assoc-b85c74d2
149 ipv6_native:
150 description: Indicates whether this is an IPv6 only subnet.
151 type: bool
152 sample: false
153 ipv6_cidr_block_association_set:
154 description: An array of IPv6 cidr block association set information.
155 returned: always
156 type: complex
157 contains:
158 association_id:
159 description: The association ID.
160 returned: always
161 type: str
162 ipv6_cidr_block:
163 description: The IPv6 CIDR block that is associated with the subnet.
164 returned: always
165 type: str
166 ipv6_cidr_block_state:
167 description: A hash/dict that contains a single item. The state of the cidr block association.
168 returned: always
169 type: dict
170 contains:
171 state:
172 description: The CIDR block association state.
173 returned: always
174 type: str
175 map_customer_owned_ip_on_launch:
176 description:
177 - Indicates whether a network interface receives a customer-owned IPv4 address.
178 type: bool
179 sample: flase
180 owner_id:
181 description: The ID of the Amazon Web Services account that owns the subnet.
182 type: str
183 sample: 12344567
184 private_dns_name_options_on_launch:
185 description:
186 - The type of hostnames to assign to instances in the subnet at launch.
187 - An instance hostname is based on the IPv4 address or ID of the instance.
188 type: dict
189 sample: {
190 "enable_resource_name_dns_a_record": false,
191 "enable_resource_name_dns_aaaa_record": false,
192 "hostname_type": "ip-name"
193 }
194 subnet_arn:
195 description: The Amazon Resource Name (ARN) of the subnet.
196 type: str
197 sample: arn:aws:ec2:us-east-1:xxx:subnet/subnet-xxx
198 subnet_id:
199 description: The ID of the Subnet.
200 returned: always
201 type: str
202 """
203
204 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
205
206 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_subnets
207 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
208 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
209 from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list
210
211
212 def _describe_subnets(connection, module: AnsibleAWSModule) -> None:
213 """
214 Describe Subnets.
215
216 module : AnsibleAWSModule object
217 connection : boto3 client connection object
218 """
219 # collect parameters
220 filters = ansible_dict_to_boto3_filter_list(module.params.get("filters"))
221 subnet_ids = module.params.get("subnet_ids")
222
223 if subnet_ids is None:
224 # Set subnet_ids to empty list if it is None
225 subnet_ids = []
226
227 # init empty list for return vars
228 subnet_info = list()
229
230 # Get the basic VPC info
231 subnets = describe_subnets(connection, SubnetIds=subnet_ids, Filters=filters)
232
233 for subnet in subnets:
234 # for backwards compatibility
235 subnet["id"] = subnet["SubnetId"]
236 subnet_info.append(camel_dict_to_snake_dict(subnet))
237 # convert tag list to ansible dict
238 subnet_info[-1]["tags"] = boto3_tag_list_to_ansible_dict(subnet.get("Tags", []))
239
240 module.exit_json(subnets=subnet_info)
241
242
243 def main() -> None:
244 argument_spec = dict(
245 subnet_ids=dict(type="list", elements="str", default=[], aliases=["subnet_id"]),
246 filters=dict(type="dict", default={}),
247 )
248
249 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
250
251 connection = module.client("ec2")
252
253 _describe_subnets(connection, module)
254
255
256 if __name__ == "__main__":
257 main()