Debian Codegraph @

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

raw
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_security_group_info
10 version_added: 1.0.0
11 short_description: Gather information about EC2 security groups in AWS
12 description:
13 - Gather information about EC2 security groups in AWS.
14 author:
15 - Henrique Rodrigues (@Sodki)
16 options:
17 filters:
18 description:
19 - A dict of filters to apply. Each dict item consists of a filter key and a filter value. See
20 U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSecurityGroups.html) for
21 possible filters. Filter names and values are case sensitive. You can also use underscores (_)
22 instead of dashes (-) in the filter keys, which will take precedence in case of conflict.
23 required: false
24 default: {}
25 type: dict
26 notes:
27 - By default, the module will return all security groups in a region. To limit results use the
28 appropriate filters.
29 - Prior to release 5.0.0 this module was called M(amazon.aws.ec2_group_info). The usage did not
30 change.
31
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
41 # Gather information about all security groups
42 - amazon.aws.ec2_security_group_info:
43
44 # Gather information about all security groups in a specific VPC
45 - amazon.aws.ec2_security_group_info:
46 filters:
47 vpc-id: vpc-12345678
48
49 # Gather information about all security groups in a specific VPC
50 - amazon.aws.ec2_security_group_info:
51 filters:
52 vpc-id: vpc-12345678
53
54 # Gather information about a security group
55 - amazon.aws.ec2_security_group_info:
56 filters:
57 group-name: example-1
58
59 # Gather information about a security group by id
60 - amazon.aws.ec2_security_group_info:
61 filters:
62 group-id: sg-12345678
63
64 # Gather information about a security group with multiple filters, also mixing the use of underscores as filter keys
65 - amazon.aws.ec2_security_group_info:
66 filters:
67 group_id: sg-12345678
68 vpc-id: vpc-12345678
69
70 # Gather information about various security groups
71 - amazon.aws.ec2_security_group_info:
72 filters:
73 group-name:
74 - example-1
75 - example-2
76 - example-3
77
78 # Gather information about any security group with a tag key Name and value Example.
79 # The quotes around 'tag:name' are important because of the colon in the value
80 - amazon.aws.ec2_security_group_info:
81 filters:
82 "tag:Name": Example
83 """
84
85 RETURN = r"""
86 security_groups:
87 description: Security groups that match the provided filters. Each element consists of a dict with all the information related to that security group.
88 type: list
89 returned: always
90 elements: dict
91 contains:
92 description:
93 description: The description of the security group.
94 returned: always
95 type: str
96 group_id:
97 description: The ID of the security group.
98 returned: always
99 type: str
100 group_name:
101 description: The name of the security group.
102 returned: always
103 type: str
104 ip_permissions:
105 description: The inbound rules associated with the security group.
106 returned: always
107 type: list
108 elements: dict
109 contains:
110 from_port:
111 description: If the protocol is TCP or UDP, this is the start of the port range.
112 type: int
113 sample: 80
114 ip_protocol:
115 description: The IP protocol name or number.
116 returned: always
117 type: str
118 ip_ranges:
119 description: The IPv4 ranges.
120 returned: always
121 type: list
122 elements: dict
123 contains:
124 cidr_ip:
125 description: The IPv4 CIDR range.
126 returned: always
127 type: str
128 ipv6_ranges:
129 description: The IPv6 ranges.
130 returned: always
131 type: list
132 elements: dict
133 contains:
134 cidr_ipv6:
135 description: The IPv6 CIDR range.
136 returned: always
137 type: str
138 prefix_list_ids:
139 description: The prefix list IDs.
140 returned: always
141 type: list
142 elements: dict
143 contains:
144 prefix_list_id:
145 description: The ID of the prefix.
146 returned: always
147 type: str
148 to_group:
149 description: If the protocol is TCP or UDP, this is the end of the port range.
150 type: int
151 sample: 80
152 user_id_group_pairs:
153 description: The security group and AWS account ID pairs.
154 returned: always
155 type: list
156 elements: dict
157 contains:
158 group_id:
159 description: The security group ID of the pair.
160 returned: always
161 type: str
162 user_id:
163 description: The user ID of the pair.
164 returned: always
165 type: str
166 ip_permissions_egress:
167 description: The outbound rules associated with the security group.
168 returned: always
169 type: list
170 elements: dict
171 contains:
172 ip_protocol:
173 description: The IP protocol name or number.
174 returned: always
175 type: str
176 ip_ranges:
177 description: The IPv4 ranges.
178 returned: always
179 type: list
180 elements: dict
181 contains:
182 cidr_ip:
183 description: The IPv4 CIDR range.
184 returned: always
185 type: str
186 ipv6_ranges:
187 description: The IPv6 ranges.
188 returned: always
189 type: list
190 elements: dict
191 contains:
192 cidr_ipv6:
193 description: The IPv6 CIDR range.
194 returned: always
195 type: str
196 prefix_list_ids:
197 description: The prefix list IDs.
198 returned: always
199 type: list
200 elements: dict
201 contains:
202 prefix_list_id:
203 description: The ID of the prefix.
204 returned: always
205 type: str
206 user_id_group_pairs:
207 description: The security group and AWS account ID pairs.
208 returned: always
209 type: list
210 elements: dict
211 contains:
212 group_id:
213 description: The security group ID of the pair.
214 returned: always
215 type: str
216 user_id:
217 description: The user ID of the pair.
218 returned: always
219 type: str
220 owner_id:
221 description: The AWS account ID of the owner of the security group.
222 returned: always
223 type: str
224 tags:
225 description: The tags associated with the security group.
226 returned: always
227 type: dict
228 vpc_id:
229 description: The ID of the VPC for the security group.
230 returned: always
231 type: str
232 sample: [
233 {
234 "description": "created by rds_instance integration tests",
235 "group_id": "sg-036496a610b79da88",
236 "group_name": "ansible-test-89355088-unknown5c5f67f3ad09-sg-1",
237 "ip_permissions": [],
238 "ip_permissions_egress": [
239 {
240 "ip_protocol": "-1",
241 "ip_ranges": [
242 {
243 "cidr_ip": "0.0.0.0/0"
244 }
245 ],
246 "ipv6_ranges": [],
247 "prefix_list_ids": [],
248 "user_id_group_pairs": []
249 }
250 ],
251 "owner_id": "123456789012",
252 "tags": {},
253 "vpc_id": "vpc-0bc3bb03f97405435"
254 }
255 ]
256 """
257
258 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
259
260 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
261 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_security_groups
262 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
263 from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict
264 from ansible_collections.amazon.aws.plugins.module_utils.transformation import sanitize_filters_to_boto3_filter_list
265
266
267 def main() -> None:
268 argument_spec = dict(filters=dict(default={}, type="dict"))
269
270 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
271
272 connection = module.client("ec2")
273
274 # Sanitize filters
275 sanitized_filters = sanitize_filters_to_boto3_filter_list(module.params.get("filters"), ignore_keys=["tag:"])
276 try:
277 security_groups = describe_security_groups(connection, Filters=sanitized_filters)
278 except AnsibleEC2Error as e:
279 module.fail_json_aws(e, msg="Failed to describe security groups")
280
281 snaked_security_groups = []
282 for security_group in security_groups:
283 # Modify boto3 tags list to be ansible friendly dict
284 # but don't camel case tags
285 security_group = camel_dict_to_snake_dict(security_group)
286 security_group["tags"] = boto3_tag_list_to_ansible_dict(
287 security_group.get("tags", {}), tag_name_key_name="key", tag_value_key_name="value"
288 )
289 snaked_security_groups.append(security_group)
290
291 module.exit_json(security_groups=snaked_security_groups)
292
293
294 if __name__ == "__main__":
295 main()