Debian Codegraph @

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

raw
1 ---
2 # Test suite for ec2_ami
3 - module_defaults:
4 group/aws:
5 aws_region: "{{ aws_region }}"
6 access_key: "{{ aws_access_key }}"
7 secret_key: "{{ aws_secret_key }}"
8 session_token: "{{ security_token | default(omit) }}"
9 block:
10 # ============================================================
11
12 # SETUP: vpc, ec2 key pair, subnet, security group, ec2 instance, snapshot
13 - name: Create a VPC to work in
14 amazon.aws.ec2_vpc_net:
15 cidr_block: "{{ vpc_cidr }}"
16 state: present
17 name: "{{ ec2_ami_name }}_setup"
18 resource_tags:
19 Name: "{{ ec2_ami_name }}_setup"
20 register: setup_vpc
21
22 - name: Create a key pair to use for creating an ec2 instance
23 amazon.aws.ec2_key:
24 name: "{{ ec2_ami_name }}_setup"
25 state: present
26 register: setup_key
27
28 - name: Create a subnet to use for creating an ec2 instance
29 amazon.aws.ec2_vpc_subnet:
30 az: "{{ availability_zone }}"
31 tags: "{{ ec2_ami_name }}_setup"
32 vpc_id: "{{ setup_vpc.vpc.id }}"
33 cidr: "{{ subnet_cidr }}"
34 state: present
35 resource_tags:
36 Name: "{{ ec2_ami_name }}_setup"
37 register: setup_subnet
38
39 - name: Create a security group to use for creating an ec2 instance
40 amazon.aws.ec2_security_group:
41 name: "{{ ec2_ami_name }}_setup"
42 description: created by Ansible integration tests
43 state: present
44 vpc_id: "{{ setup_vpc.vpc.id }}"
45 register: setup_sg
46
47 - name: Provision ec2 instance to create an image
48 amazon.aws.ec2_instance:
49 state: running
50 key_name: "{{ setup_key.key.name }}"
51 instance_type: t2.micro
52 image_id: "{{ ec2_ami_id }}"
53 tags:
54 "{{ec2_ami_name}}_instance_setup": integration_tests
55 security_group: "{{ setup_sg.group_id }}"
56 vpc_subnet_id: "{{ setup_subnet.subnet.id }}"
57 volumes:
58 - device_name: /dev/sdc
59 virtual_name: ephemeral1
60 wait: true
61 register: setup_instance
62
63 - name: Store EC2 Instance ID
64 ansible.builtin.set_fact:
65 ec2_instance_id: "{{ setup_instance.instances[0].instance_id }}"
66
67 - name: Take a snapshot of the instance to create an image
68 amazon.aws.ec2_snapshot:
69 instance_id: "{{ ec2_instance_id }}"
70 device_name: "{{ ec2_ami_root_disk }}"
71 state: present
72 register: setup_snapshot
73
74 # ============================================================
75
76 - name: Create an image from the snapshot
77 amazon.aws.ec2_ami:
78 name: "{{ ec2_ami_name }}_ami"
79 description: "{{ ec2_ami_description }}"
80 state: present
81 launch_permissions:
82 user_ids: []
83 tags:
84 Name: "{{ ec2_ami_name }}_ami"
85 root_device_name: "{{ ec2_ami_root_disk }}"
86 device_mapping:
87 - device_name: "{{ ec2_ami_root_disk }}"
88 volume_type: gp2
89 size: 8
90 delete_on_termination: true
91 snapshot_id: "{{ setup_snapshot.snapshot_id }}"
92 register: result
93 ignore_errors: true
94
95 - name: Set image id fact for deletion later
96 ansible.builtin.set_fact:
97 ec2_ami_image_id: "{{ result.image_id }}"
98 ec2_ami_snapshot: "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}"
99
100 - name: Assert a new ami has been created
101 ansible.builtin.assert:
102 that:
103 - result.changed
104 - result.image_id.startswith('ami-')
105
106 # ============================================================
107
108 - name: Test default launch permissions idempotence (check mode)
109 amazon.aws.ec2_ami:
110 description: "{{ ec2_ami_description }}"
111 state: present
112 name: "{{ ec2_ami_name }}_ami"
113 tags:
114 Name: "{{ ec2_ami_name }}_ami"
115 root_device_name: "{{ ec2_ami_root_disk }}"
116 image_id: "{{ result.image_id }}"
117 launch_permissions:
118 user_ids: []
119 device_mapping:
120 - device_name: "{{ ec2_ami_root_disk }}"
121 volume_type: gp2
122 size: 8
123 delete_on_termination: true
124 snapshot_id: "{{ setup_snapshot.snapshot_id }}"
125 check_mode: true
126 register: check_mode_result
127
128 - name: Assert that check_mode result is not changed
129 ansible.builtin.assert:
130 that:
131 - check_mode_result is not changed
132
133 - name: Test default launch permissions idempotence
134 amazon.aws.ec2_ami:
135 description: "{{ ec2_ami_description }}"
136 state: present
137 name: "{{ ec2_ami_name }}_ami"
138 tags:
139 Name: "{{ ec2_ami_name }}_ami"
140 root_device_name: "{{ ec2_ami_root_disk }}"
141 image_id: "{{ result.image_id }}"
142 launch_permissions:
143 user_ids: []
144 device_mapping:
145 - device_name: "{{ ec2_ami_root_disk }}"
146 volume_type: gp2
147 size: 8
148 delete_on_termination: true
149 snapshot_id: "{{ setup_snapshot.snapshot_id }}"
150 register: result
151
152 - name: Assert a new ami has not been created
153 ansible.builtin.assert:
154 that:
155 - not result.changed
156 - result.image_id.startswith('ami-')
157
158 # ============================================================
159
160 - name: Add a tag to the AMI
161 amazon.aws.ec2_ami:
162 state: present
163 description: "{{ ec2_ami_description }}"
164 image_id: "{{ result.image_id }}"
165 name: "{{ ec2_ami_name }}_ami"
166 tags:
167 New: Tag
168 purge_tags: false
169 register: result
170
171 - name: Assert a tag was added
172 ansible.builtin.assert:
173 that:
174 - "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'"
175 - "'New' in result.tags and result.tags.New == 'Tag'"
176
177 - name: Use purge_tags to remove a tag from the AMI
178 amazon.aws.ec2_ami:
179 state: present
180 description: "{{ ec2_ami_description }}"
181 image_id: "{{ result.image_id }}"
182 name: "{{ ec2_ami_name }}_ami"
183 tags:
184 New: Tag
185 register: result
186
187 - name: Assert a tag was removed
188 ansible.builtin.assert:
189 that:
190 - "'Name' not in result.tags"
191 - "'New' in result.tags and result.tags.New == 'Tag'"
192
193 # ============================================================
194
195 - name: Update AMI launch permissions (check mode)
196 amazon.aws.ec2_ami:
197 state: present
198 image_id: "{{ result.image_id }}"
199 description: "{{ ec2_ami_description }}"
200 tags:
201 Name: "{{ ec2_ami_name }}_ami"
202 launch_permissions:
203 group_names: [all]
204 check_mode: true
205 register: check_mode_result
206
207 - name: Assert that check_mode result is changed
208 ansible.builtin.assert:
209 that:
210 - check_mode_result is changed
211
212 - name: Update AMI launch permissions
213 amazon.aws.ec2_ami:
214 state: present
215 image_id: "{{ result.image_id }}"
216 description: "{{ ec2_ami_description }}"
217 tags:
218 Name: "{{ ec2_ami_name }}_ami"
219 launch_permissions:
220 group_names: [all]
221 register: result
222
223 - name: Assert launch permissions were updated
224 ansible.builtin.assert:
225 that:
226 - result.changed
227
228 # ============================================================
229
230 - name: Modify the AMI description (check mode)
231 amazon.aws.ec2_ami:
232 state: present
233 image_id: "{{ result.image_id }}"
234 name: "{{ ec2_ami_name }}_ami"
235 description: "{{ ec2_ami_description }}CHANGED"
236 tags:
237 Name: "{{ ec2_ami_name }}_ami"
238 launch_permissions:
239 group_names: [all]
240 check_mode: true
241 register: check_mode_result
242
243 - name: Assert that check_mode result is changed
244 ansible.builtin.assert:
245 that:
246 - check_mode_result is changed
247
248 - name: Modify the AMI description
249 amazon.aws.ec2_ami:
250 state: present
251 image_id: "{{ result.image_id }}"
252 name: "{{ ec2_ami_name }}_ami"
253 description: "{{ ec2_ami_description }}CHANGED"
254 tags:
255 Name: "{{ ec2_ami_name }}_ami"
256 launch_permissions:
257 group_names: [all]
258 register: result
259
260 - name: Assert the description changed
261 ansible.builtin.assert:
262 that:
263 - result.changed
264
265 # ============================================================
266
267 - name: Remove public launch permissions
268 amazon.aws.ec2_ami:
269 state: present
270 image_id: "{{ result.image_id }}"
271 name: "{{ ec2_ami_name }}_ami"
272 tags:
273 Name: "{{ ec2_ami_name }}_ami"
274 launch_permissions:
275 group_names: []
276 register: result
277
278 - name: Assert launch permissions were updated
279 ansible.builtin.assert:
280 that:
281 - result.changed
282
283 # ============================================================
284
285 - name: Delete ami without deleting the snapshot (default is not to delete)
286 amazon.aws.ec2_ami:
287 instance_id: "{{ ec2_instance_id }}"
288 state: absent
289 name: "{{ ec2_ami_name }}_ami"
290 image_id: "{{ ec2_ami_image_id }}"
291 tags:
292 Name: "{{ ec2_ami_name }}_ami"
293 wait: true
294 ignore_errors: true
295 register: result
296
297 - name: Assert that the image has been deleted
298 ansible.builtin.assert:
299 that:
300 - result.changed
301 - "'image_id' not in result"
302
303 - name: Ensure the snapshot still exists
304 amazon.aws.ec2_snapshot_info:
305 snapshot_ids:
306 - "{{ ec2_ami_snapshot }}"
307 register: snapshot_result
308
309 - name: Assert the snapshot wasn't deleted
310 ansible.builtin.assert:
311 that:
312 - snapshot_result.snapshots[0].snapshot_id == ec2_ami_snapshot
313
314 - name: Delete ami for a second time (check mode)
315 amazon.aws.ec2_ami:
316 instance_id: "{{ ec2_instance_id }}"
317 state: absent
318 name: "{{ ec2_ami_name }}_ami"
319 image_id: "{{ ec2_ami_image_id }}"
320 tags:
321 Name: "{{ ec2_ami_name }}_ami"
322 wait: true
323 check_mode: true
324 register: check_mode_result
325
326 - name: Assert that check_mode result is not changed
327 ansible.builtin.assert:
328 that:
329 - check_mode_result is not changed
330
331 - name: Delete ami for a second time
332 amazon.aws.ec2_ami:
333 instance_id: "{{ ec2_instance_id }}"
334 state: absent
335 name: "{{ ec2_ami_name }}_ami"
336 image_id: "{{ ec2_ami_image_id }}"
337 tags:
338 Name: "{{ ec2_ami_name }}_ami"
339 wait: true
340 register: result
341
342 - name: Assert that image does not exist
343 ansible.builtin.assert:
344 that:
345 - not result.changed
346 - not result.failed
347
348 always:
349 # ============================================================
350
351 # TEAR DOWN: snapshot, ec2 instance, ec2 key pair, security group, vpc
352 - name: Announce teardown start
353 ansible.builtin.debug:
354 msg: "***** TESTING COMPLETE. COMMENCE TEARDOWN *****"
355
356 - name: Remove setup ec2 instance
357 amazon.aws.ec2_instance:
358 state: absent
359 instance_ids:
360 - "{{ ec2_instance_id }}"
361 wait: true
362 ignore_errors: true
363
364 - name: Delete ami
365 amazon.aws.ec2_ami:
366 state: absent
367 image_id: "{{ ec2_ami_image_id }}"
368 name: "{{ ec2_ami_name }}_ami"
369 wait: true
370 ignore_errors: true
371
372 - name: Remove setup snapshot of ec2 instance
373 amazon.aws.ec2_snapshot:
374 state: absent
375 snapshot_id: "{{ setup_snapshot.snapshot_id }}"
376 ignore_errors: true
377
378 - name: Remove setup keypair
379 amazon.aws.ec2_key:
380 name: "{{ ec2_ami_name }}_setup"
381 state: absent
382 ignore_errors: true
383
384 - name: Remove setup security group
385 amazon.aws.ec2_security_group:
386 name: "{{ ec2_ami_name }}_setup"
387 description: created by Ansible integration tests
388 state: absent
389 vpc_id: "{{ setup_vpc.vpc.id }}"
390 ignore_errors: true
391
392 - name: Remove setup subnet
393 amazon.aws.ec2_vpc_subnet:
394 az: "{{ availability_zone }}"
395 tags: "{{ ec2_ami_name }}_setup"
396 vpc_id: "{{ setup_vpc.vpc.id }}"
397 cidr: "{{ subnet_cidr }}"
398 state: absent
399 resource_tags:
400 Name: "{{ ec2_ami_name }}_setup"
401 ignore_errors: true
402
403 - name: Remove setup VPC
404 amazon.aws.ec2_vpc_net:
405 cidr_block: "{{ vpc_cidr }}"
406 state: absent
407 name: "{{ ec2_ami_name }}_setup"
408 resource_tags:
409 Name: "{{ ec2_ami_name }}_setup"
410 ignore_errors: true