Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/ec2_snapshot/tasks/main.yml

raw
1 ---
2 # Tests for EC2 Snapshot
3 #
4 # Tests amazon.aws.ec2_snapshot:
5 # - Snapshot creation
6 # - Create with last_snapshot_min_age
7 # - Snapshot deletion
8 #
9 # Tests amazon.aws.ec2_snapshot_info:
10 # - Listing snapshots for filter: tag
11 #
12 - name: Integration testing for ec2_snapshot
13 module_defaults:
14 group/aws:
15 access_key: "{{ aws_access_key }}"
16 secret_key: "{{ aws_secret_key }}"
17 session_token: "{{ security_token | default(omit) }}"
18 region: "{{ aws_region }}"
19
20 block:
21 - name: Gather availability zones
22 amazon.aws.aws_az_info:
23 register: azs
24
25 - name: Run tasks for testing snapshot createVolumePermissions modifications
26 ansible.builtin.import_tasks: test_modify_create_volume_permissions.yml
27 - name: Create a detached volume without tags
28 amazon.aws.ec2_vol:
29 volume_size: 1
30 zone: "{{ azs.availability_zones[0].zone_name }}"
31 register: volume_detached
32
33 # Capture snapshot of this detached volume and assert the results
34 - name: Create a snapshot of detached volume without tags and store results
35 amazon.aws.ec2_snapshot:
36 volume_id: "{{ volume_detached.volume_id }}"
37 register: untagged_snapshot
38
39 - ansible.builtin.assert:
40 that:
41 - untagged_snapshot is changed
42 - untagged_snapshot.snapshots| length == 1
43 - untagged_snapshot.snapshots[0].volume_id == volume_detached.volume_id
44
45 - name: Setup an instance for testing, make sure volumes are attached before next task
46 amazon.aws.ec2_instance:
47 name: "{{ resource_prefix }}"
48 instance_type: t2.nano
49 image_id: "{{ ec2_ami_id }}"
50 volumes:
51 - device_name: /dev/xvda
52 ebs:
53 volume_size: 8
54 delete_on_termination: true
55 state: running
56 wait: true
57 register: instance
58
59 - ansible.builtin.set_fact:
60 volume_id: "{{ instance.instances[0].block_device_mappings[0].ebs.volume_id }}"
61 instance_id: "{{ instance.instances[0].instance_id }}"
62 device_name: "{{ instance.instances[0].block_device_mappings[0].device_name }}"
63
64 - name: Take snapshot (check mode)
65 amazon.aws.ec2_snapshot:
66 instance_id: "{{ instance_id }}"
67 device_name: "{{ device_name }}"
68 snapshot_tags:
69 Test: "{{ resource_prefix }}"
70 check_mode: true
71 register: result
72
73 - ansible.builtin.assert:
74 that:
75 - result is changed
76
77 - name: Take snapshot of volume
78 amazon.aws.ec2_snapshot:
79 volume_id: "{{ volume_id }}"
80 register: result
81
82 # The Name tag is created automatically as the instance_name; ie the resource_prefix
83 - name: Get info about snapshots
84 amazon.aws.ec2_snapshot_info:
85 filters:
86 tag:Name: "{{ resource_prefix }}"
87 register: info_result
88
89 - ansible.builtin.assert:
90 that:
91 - result is changed
92 - info_result is not changed
93 - info_result.snapshots| length == 1
94 - info_result.snapshots[0].snapshot_id == result.snapshot_id
95 - info_result.snapshots[0].volume_id == result.volume_id
96 - info_result.snapshots[0].volume_size == result.volume_size
97 - info_result.snapshots[0].tags == result.tags
98
99 - name: Get info about snapshots (check_mode)
100 amazon.aws.ec2_snapshot_info:
101 filters:
102 tag:Name: "{{ resource_prefix }}"
103 register: info_check
104 check_mode: true
105
106 - ansible.builtin.assert:
107 that:
108 - info_check is not changed
109 - info_check.snapshots| length == 1
110 - info_check.snapshots[0].snapshot_id == result.snapshot_id
111 - info_check.snapshots[0].volume_id == result.volume_id
112 - info_check.snapshots[0].volume_size == result.volume_size
113 - info_check.snapshots[0].tags == result.tags
114
115 - name: Take snapshot if most recent >1hr (False) (check mode)
116 amazon.aws.ec2_snapshot:
117 volume_id: "{{ volume_id }}"
118 snapshot_tags:
119 Name: "{{ resource_prefix }}"
120 last_snapshot_min_age: 60
121 check_mode: true
122 register: result
123 - ansible.builtin.assert:
124 that:
125 - result is not changed
126
127 - name: Take snapshot if most recent >1hr (False)
128 amazon.aws.ec2_snapshot:
129 volume_id: "{{ volume_id }}"
130 last_snapshot_min_age: 60
131 register: result
132
133 - name: Get info about snapshots
134 amazon.aws.ec2_snapshot_info:
135 filters:
136 tag:Name: "{{ resource_prefix }}"
137 register: info_result
138
139 - ansible.builtin.assert:
140 that:
141 - result is not changed
142 - info_result.snapshots| length == 1
143
144 - name: Pause so we can do a last_snapshot_min_age test
145 ansible.builtin.pause:
146 minutes: 1
147
148 - name: Take snapshot if most recent >1min (True) (check mode)
149 amazon.aws.ec2_snapshot:
150 volume_id: "{{ volume_id }}"
151 snapshot_tags:
152 Name: "{{ resource_prefix }}"
153 last_snapshot_min_age: 1
154 check_mode: true
155 register: result
156 - ansible.builtin.assert:
157 that:
158 - result is changed
159
160 - name: Take snapshot if most recent >1min (True)
161 amazon.aws.ec2_snapshot:
162 volume_id: "{{ volume_id }}"
163 last_snapshot_min_age: 1
164 register: result
165
166 - name: Get info about snapshots
167 amazon.aws.ec2_snapshot_info:
168 filters:
169 tag:Name: "{{ resource_prefix }}"
170 register: info_result
171
172 - ansible.builtin.assert:
173 that:
174 - result is changed
175 - info_result.snapshots| length == 2
176 - result.snapshot_id in ( info_result.snapshots | map(attribute='snapshot_id') | list )
177
178 - name: Take snapshot with a tag (check mode)
179 amazon.aws.ec2_snapshot:
180 volume_id: "{{ volume_id }}"
181 snapshot_tags:
182 MyTag: "{{ resource_prefix }}"
183 check_mode: true
184 register: result
185 - ansible.builtin.assert:
186 that:
187 - result is changed
188
189 - name: Take snapshot and tag it
190 amazon.aws.ec2_snapshot:
191 volume_id: "{{ volume_id }}"
192 snapshot_tags:
193 MyTag: "{{ resource_prefix }}"
194 register: tagged_result
195
196 - name: Get info about snapshots by tag
197 amazon.aws.ec2_snapshot_info:
198 filters:
199 tag:MyTag: "{{ resource_prefix }}"
200 register: tag_info_result
201
202 - ansible.builtin.set_fact:
203 tagged_snapshot_id: "{{ tag_info_result.snapshots[0].snapshot_id }}"
204
205 - ansible.builtin.assert:
206 that:
207 - tagged_result is changed
208 - tagged_result.tags| length == 2
209 - tag_info_result.snapshots| length == 1
210 - tagged_result.tags.MyTag == resource_prefix
211 - tagged_result.snapshot_id == tagged_snapshot_id
212
213 - name: Get info about all snapshots for this test
214 amazon.aws.ec2_snapshot_info:
215 filters:
216 tag:Name: "{{ resource_prefix }}"
217 register: info_result
218
219 - ansible.builtin.assert:
220 that:
221 - info_result.snapshots | length == 3
222
223 - name: Generate extra snapshots
224 amazon.aws.ec2_snapshot:
225 volume_id: "{{ volume_id }}"
226 snapshot_tags:
227 ResourcePrefix: "{{ resource_prefix }}"
228 loop: "{{ range(1, 6, 1) | list }}"
229 loop_control:
230 # Anything under 15 will trigger SnapshotCreationPerVolumeRateExceeded,
231 # this should now be automatically handled, but pause a little anyway to
232 # avoid being aggressive
233 pause: 15
234 label: Generate extra snapshots - {{ item }}
235
236 # Retrieve snapshots in paginated mode
237 - name: Get snapshots in paginated mode using max_results option
238 amazon.aws.ec2_snapshot_info:
239 filters:
240 tag:Name: "{{ resource_prefix }}"
241 max_results: 5
242 register: info_result
243
244 - ansible.builtin.assert:
245 that:
246 - info_result.next_token_id is defined
247
248 # Pagination : 2nd request
249 - name: Get snapshots for a second paginated request
250 amazon.aws.ec2_snapshot_info:
251 filters:
252 tag:Name: "{{ resource_prefix }}"
253 next_token_id: "{{ info_result.next_token_id }}"
254 max_results: 5
255 register: info_result_2
256
257 # note: *MAX* 5 results, sometimes they'll throw us fewer...
258 # 8 is the absolute max it should find
259 - ansible.builtin.assert:
260 that:
261 - (length_1 | int ) + (length_2 | int) <= 8
262 vars:
263 length_1: "{{ info_result.snapshots | length }}"
264 length_2: "{{ info_result_2.snapshots | length }}"
265
266 # delete the tagged snapshot - check mode
267 - name: Delete the tagged snapshot (check mode)
268 amazon.aws.ec2_snapshot:
269 state: absent
270 snapshot_id: "{{ tagged_snapshot_id }}"
271 register: delete_result_check_mode
272 check_mode: true
273
274 - ansible.builtin.assert:
275 that:
276 - delete_result_check_mode is changed
277
278 # check that snapshot_ids and max_results are mutually exclusive
279 - name: Check that max_results and snapshot_ids are mutually exclusive
280 amazon.aws.ec2_snapshot_info:
281 snapshot_ids:
282 - "{{ tagged_snapshot_id }}"
283 max_results: 5
284 ignore_errors: true
285 register: info_result
286
287 - name: Assert that operation failed
288 ansible.builtin.assert:
289 that:
290 - info_result is failed
291
292 # check that snapshot_ids and next_token_id are mutually exclusive
293 - name: Check that snapshot_ids and next_token_id are mutually exclusive
294 amazon.aws.ec2_snapshot_info:
295 snapshot_ids:
296 - "{{ tagged_snapshot_id }}"
297 next_token_id: random_value_token
298 ignore_errors: true
299 register: info_result
300
301 - name: Assert that operation failed
302 ansible.builtin.assert:
303 that:
304 - info_result is failed
305
306 # delete the tagged snapshot
307 - name: Delete the tagged snapshot
308 amazon.aws.ec2_snapshot:
309 state: absent
310 snapshot_id: "{{ tagged_snapshot_id }}"
311
312 # delete the tagged snapshot again (results in InvalidSnapshot.NotFound)
313 - name: Delete already removed snapshot (check mode)
314 amazon.aws.ec2_snapshot:
315 state: absent
316 snapshot_id: "{{ tagged_snapshot_id }}"
317 register: delete_result_second_check_mode
318 check_mode: true
319
320 - ansible.builtin.assert:
321 that:
322 - delete_result_second_check_mode is not changed
323
324 - name: Delete already removed snapshot (idempotent)
325 amazon.aws.ec2_snapshot:
326 state: absent
327 snapshot_id: "{{ tagged_snapshot_id }}"
328 register: delete_result_second_idempotent
329
330 - ansible.builtin.assert:
331 that:
332 - delete_result_second_idempotent is not changed
333
334 - name: Get info about all snapshots for this test
335 amazon.aws.ec2_snapshot_info:
336 filters:
337 tag:Name: "{{ resource_prefix }}"
338 register: info_result
339
340 - ansible.builtin.assert:
341 that:
342 - info_result.snapshots| length == 7
343 - tagged_snapshot_id not in ( info_result.snapshots | map(attribute='snapshot_id') | list )
344
345 - name: Delete snapshots
346 amazon.aws.ec2_snapshot:
347 state: absent
348 snapshot_id: "{{ item.snapshot_id }}"
349 with_items: "{{ info_result.snapshots }}"
350
351 - name: Get info about all snapshots for this test
352 amazon.aws.ec2_snapshot_info:
353 filters:
354 tag:Name: "{{ resource_prefix }}"
355 register: info_result
356
357 - ansible.builtin.assert:
358 that:
359 - info_result.snapshots| length == 0
360
361 always:
362 - name: Snapshots to delete
363 amazon.aws.ec2_snapshot_info:
364 filters:
365 tag:Name: "{{ resource_prefix }}"
366 register: tagged_snapshots
367
368 - name: Delete tagged snapshots
369 amazon.aws.ec2_snapshot:
370 state: absent
371 snapshot_id: "{{ item.snapshot_id }}"
372 with_items: "{{ tagged_snapshots.snapshots }}"
373 ignore_errors: true
374
375 - name: Delete instance
376 amazon.aws.ec2_instance:
377 instance_ids: "{{ instance_id }}"
378 state: absent
379 ignore_errors: true
380
381 - name: Delete volume
382 amazon.aws.ec2_vol:
383 id: "{{ volume_id }}"
384 state: absent
385 ignore_errors: true
386
387 - name: Delete detached and untagged volume
388 amazon.aws.ec2_vol:
389 id: "{{ volume_detached.volume_id }}"
390 state: absent
391 ignore_errors: true
392
393 - name: Delete untagged snapshot
394 amazon.aws.ec2_snapshot:
395 state: absent
396 snapshot_id: "{{ untagged_snapshot.snapshot_id }}"
397 ignore_errors: true