Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/ec2_spot_instance_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://wwww.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: ec2_spot_instance_info
10 version_added: 2.0.0
11 short_description: Gather information about ec2 spot instance requests
12 description:
13 - Describes the specified Spot Instance requests.
14 author:
15 - Mandar Vijay Kulkarni (@mandar242)
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.
20 - Filter names and values are case sensitive.
21 - See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSpotInstanceRequests.html) for possible filters.
22 required: false
23 default: {}
24 type: dict
25 spot_instance_request_ids:
26 description:
27 - One or more Spot Instance request IDs.
28 required: false
29 type: list
30 elements: str
31 default: []
32
33 extends_documentation_fragment:
34 - amazon.aws.common.modules
35 - amazon.aws.region.modules
36 - amazon.aws.boto3
37 """
38
39 EXAMPLES = r"""
40 # Note: These examples do not set authentication details, see the AWS Guide for details.
41
42 - name: describe the Spot Instance requests based on request IDs
43 amazon.aws.ec2_spot_instance_info:
44 spot_instance_request_ids:
45 - sir-12345678
46
47 - name: describe the Spot Instance requests and filter results based on instance type
48 amazon.aws.ec2_spot_instance_info:
49 spot_instance_request_ids:
50 - sir-12345678
51 - sir-13579246
52 - sir-87654321
53 filters:
54 launch.instance-type: t3.medium
55
56 - name: describe the Spot requests filtered using multiple filters
57 amazon.aws.ec2_spot_instance_info:
58 filters:
59 state: active
60 launch.block-device-mapping.device-name: /dev/sdb
61 """
62
63 RETURN = r"""
64 spot_request:
65 description: The gathered information about specified spot instance requests.
66 returned: when success
67 type: list
68 elements: dict
69 contains:
70 create_time:
71 description: The date and time when the Spot Instance request was created.
72 returned: always
73 type: str
74 instance_id:
75 description: The instance ID, if an instance has been launched to fulfill the Spot Instance request.
76 returned: when instance exists
77 type: str
78 instance_interruption_behavior:
79 description: The behavior when a Spot Instance is interruped.
80 returned: always
81 type: str
82 launch_specification:
83 description: Additional information for launching instances.
84 returned: always
85 type: dict
86 contains:
87 ebs_optimized:
88 description: Indicates whether the instance is optimized for EBS I/O.
89 returned: always
90 type: bool
91 image_id:
92 description: The ID of the AMI.
93 returned: always
94 type: str
95 instance_type:
96 description: The instance type.
97 returned: always
98 type: str
99 key_name:
100 description: The name of the key pair.
101 returned: always
102 type: str
103 monitoring:
104 description: Described the monitoring of an instance.
105 returned: always
106 type: dict
107 contains:
108 enabled:
109 description: Indicated whether detailed monitoring is enabled.
110 returned: always
111 type: bool
112 placement:
113 description: The placement information for the instance.
114 returned: always
115 type: dict
116 contains:
117 availability_zone:
118 description: The name of the availability zone.
119 returned: always
120 type: str
121 security_groups:
122 description: List of security groups.
123 returned: always
124 type: list
125 elements: dict
126 contains:
127 group_id:
128 description: The ID of the security group.
129 returned: always
130 type: str
131 group_name:
132 description: The name of the security group.
133 returned: always
134 type: str
135 subnet_id:
136 description: The ID of the subnet.
137 returned: when creating a network interface when launching an instance
138 type: str
139 launched_availability_zone:
140 description: The availability zone in which the request is launched.
141 returned: always
142 type: str
143 product_description:
144 description: The product description associated with the Spot Instance.
145 returned: always
146 type: str
147 spot_instance_request_id:
148 description: The ID of the Spot Instance request.
149 returned: always
150 type: str
151 spot_price:
152 description: The maximum price per hour that you are willing to pay for a Spot Instance.
153 returned: always
154 type: str
155 state:
156 description: The state of the Spot Instance request.
157 returned: always
158 type: str
159 status:
160 description: Extra information about the status of the Spot Instance request.
161 returned: always
162 type: dict
163 contains:
164 code:
165 description:
166 - The status code.
167 - See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-request-status.html#spot-instance-request-status-understand for codes.
168 returned: always
169 type: str
170 message:
171 description: The description of the status code.
172 returned: always
173 type: str
174 update_time:
175 description: The date and time of the most recent status update in UTC format.
176 returned: always
177 type: str
178 tags:
179 description: List of tags associated with the resource.
180 returned: always
181 type: list
182 elements: dict
183 contains:
184 key:
185 description: The key of the tag.
186 returned: always
187 type: str
188 value:
189 description: The value of the tag.
190 returned: always
191 type: str
192 type:
193 description: The Spot Instance request type.
194 returned: always
195 type: str
196 valid_until:
197 description: The end date of the request in UTC format.
198 returned: always
199 type: str
200 sample: {
201 "create_time": "2021-09-01T21:05:57+00:00",
202 "instance_id": "i-08877936b801ac475",
203 "instance_interruption_behavior": "terminate",
204 "launch_specification": {
205 "ebs_optimized": false,
206 "image_id": "ami-0443305dabd4be2bc",
207 "instance_type": "t2.medium",
208 "key_name": "zuul",
209 "monitoring": {
210 "enabled": false
211 },
212 "placement": {
213 "availability_zone": "us-east-2b"
214 },
215 "security_groups": [
216 {
217 "group_id": "sg-01f9833207d53b937",
218 "group_name": "default"
219 }
220 ],
221 "subnet_id": "subnet-07d906b8358869bda"
222 },
223 "launched_availability_zone": "us-east-2b",
224 "product_description": "Linux/UNIX",
225 "spot_instance_request_id": "sir-c3cp9jsk",
226 "spot_price": "0.046400",
227 "state": "active",
228 "status": {
229 "code": "fulfilled",
230 "message": "Your spot request is fulfilled.",
231 "update_time": "2021-09-01T21:05:59+00:00"
232 },
233 "tags": {},
234 "type": "one-time",
235 "valid_until": "2021-09-08T21:05:57+00:00"
236 }
237 """
238
239 try:
240 import botocore
241 except ImportError:
242 pass # Handled by AnsibleAWSModule
243 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
244
245 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error
246 from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_spot_instance_requests
247 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
248 from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list
249
250
251 def list_spot_instance_requests(connection, module: AnsibleAWSModule) -> None:
252 params = {}
253
254 if module.params.get("filters"):
255 params["Filters"] = ansible_dict_to_boto3_filter_list(module.params.get("filters"))
256 if module.params.get("spot_instance_request_ids"):
257 params["SpotInstanceRequestIds"] = module.params.get("spot_instance_request_ids")
258
259 try:
260 describe_spot_instance_requests_response = describe_spot_instance_requests(connection, **params)
261 except AnsibleEC2Error as e:
262 module.fail_json_aws(e, msg="Failed to describe spot instance requests")
263
264 spot_request = []
265 for response_list_item in describe_spot_instance_requests_response:
266 spot_request.append(camel_dict_to_snake_dict(response_list_item))
267
268 if len(spot_request) == 0:
269 module.exit_json(msg="No spot requests found for specified options")
270
271 module.exit_json(spot_request=spot_request)
272
273
274 def main():
275 argument_spec = dict(
276 filters=dict(default={}, type="dict"),
277 spot_instance_request_ids=dict(default=[], type="list", elements="str"),
278 )
279 module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
280 try:
281 connection = module.client("ec2")
282 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
283 module.fail_json_aws(e, msg="Failed to connect to AWS")
284
285 list_spot_instance_requests(connection, module)
286
287
288 if __name__ == "__main__":
289 main()