Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/ec2_ami_instance/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 # ============================================================
68
69 - name: Test clean failure if not providing image_id or name with state=present
70 amazon.aws.ec2_ami:
71 instance_id: "{{ ec2_instance_id }}"
72 state: present
73 description: "{{ ec2_ami_description }}"
74 tags:
75 Name: "{{ ec2_ami_name }}_ami"
76 wait: true
77 root_device_name: "{{ ec2_ami_root_disk }}"
78 register: result
79 ignore_errors: true
80
81 - name: Assert error message is helpful
82 ansible.builtin.assert:
83 that:
84 - result.failed
85 - "result.msg == 'one of the following is required: name, image_id'"
86
87 # ============================================================
88
89 - name: Create an image from the instance (check mode)
90 amazon.aws.ec2_ami:
91 instance_id: "{{ ec2_instance_id }}"
92 state: present
93 name: "{{ ec2_ami_name }}_ami"
94 description: "{{ ec2_ami_description }}"
95 tags:
96 Name: "{{ ec2_ami_name }}_ami"
97 wait: true
98 root_device_name: "{{ ec2_ami_root_disk }}"
99 check_mode: true
100 register: check_mode_result
101
102 - name: Assert that check_mode result is changed
103 ansible.builtin.assert:
104 that:
105 - check_mode_result is changed
106
107 - name: Create an image from the instance
108 amazon.aws.ec2_ami:
109 instance_id: "{{ ec2_instance_id }}"
110 state: present
111 name: "{{ ec2_ami_name }}_ami"
112 description: "{{ ec2_ami_description }}"
113 tags:
114 Name: "{{ ec2_ami_name }}_ami"
115 wait: true
116 root_device_name: "{{ ec2_ami_root_disk }}"
117 register: result
118
119 - name: Set image id fact for deletion later
120 ansible.builtin.set_fact:
121 ec2_ami_image_id_simple: "{{ result.image_id }}"
122
123 - name: Assert that image has been created
124 ansible.builtin.assert:
125 that:
126 - result.changed
127 - result.image_id.startswith('ami-')
128 - "'Name' in result.tags and result.tags.Name == ec2_ami_name + '_ami'"
129
130 - name: Get related snapshot info and ensure the tags have been propagated
131 amazon.aws.ec2_snapshot_info:
132 snapshot_ids:
133 - "{{ result.block_device_mapping[ec2_ami_root_disk].snapshot_id }}"
134 register: snapshot_result
135
136 - name: Ensure the tags have been propagated to the snapshot
137 ansible.builtin.assert:
138 that:
139 - "'tags' in snapshot_result.snapshots[0]"
140 - "'Name' in snapshot_result.snapshots[0].tags and snapshot_result.snapshots[0].tags.Name == ec2_ami_name + '_ami'"
141
142 # ============================================================
143
144 - name: Create an image from the instance with attached devices with no_device true (check mode)
145 amazon.aws.ec2_ami:
146 name: "{{ ec2_ami_name }}_no_device_true_ami"
147 instance_id: "{{ ec2_instance_id }}"
148 device_mapping:
149 - device_name: /dev/sda1
150 volume_size: 10
151 delete_on_termination: true
152 volume_type: gp2
153 - device_name: /dev/sdf
154 no_device: true
155 state: present
156 wait: true
157 root_device_name: "{{ ec2_ami_root_disk }}"
158 check_mode: true
159 register: check_mode_result
160
161 - name: Assert that check_mode result is changed
162 ansible.builtin.assert:
163 that:
164 - check_mode_result is changed
165
166 - name: Create an image from the instance with attached devices with no_device true
167 amazon.aws.ec2_ami:
168 name: "{{ ec2_ami_name }}_no_device_true_ami"
169 instance_id: "{{ ec2_instance_id }}"
170 device_mapping:
171 - device_name: /dev/sda1
172 volume_size: 10
173 delete_on_termination: true
174 volume_type: gp2
175 - device_name: /dev/sdf
176 no_device: true
177 state: present
178 wait: true
179 root_device_name: "{{ ec2_ami_root_disk }}"
180 register: result_no_device_true
181
182 - name: Set image id fact for deletion later
183 ansible.builtin.set_fact:
184 ec2_ami_no_device_true_image_id: "{{ result_no_device_true.image_id }}"
185
186 - name: Assert that image with no_device option yes has been created
187 ansible.builtin.assert:
188 that:
189 - result_no_device_true.changed
190 - "'/dev/sdf' not in result_no_device_true.block_device_mapping"
191
192 - name: Create an image from the instance with attached devices with no_device false
193 amazon.aws.ec2_ami:
194 name: "{{ ec2_ami_name }}_no_device_false_ami"
195 instance_id: "{{ ec2_instance_id }}"
196 device_mapping:
197 - device_name: /dev/sda1
198 volume_size: 10
199 delete_on_termination: true
200 volume_type: gp2
201 no_device: false
202 state: present
203 wait: true
204 root_device_name: "{{ ec2_ami_root_disk }}"
205 register: result_no_device_false
206
207 - name: Set image id fact for deletion later
208 ansible.builtin.set_fact:
209 ec2_ami_no_device_false_image_id: "{{ result_no_device_false.image_id }}"
210
211 - name: Assert that image with no_device option no has been created
212 ansible.builtin.assert:
213 that:
214 - result_no_device_false.changed
215 - "'/dev/sda1' in result_no_device_false.block_device_mapping"
216
217 # ============================================================
218
219 - name: Gather facts about the image created
220 amazon.aws.ec2_ami_info:
221 image_ids: "{{ ec2_ami_image_id_simple }}"
222 register: ami_facts_result
223 ignore_errors: true
224
225 - name: Assert that the right image was found
226 ansible.builtin.assert:
227 that:
228 - ami_facts_result.images[0].image_id == ec2_ami_image_id_simple
229
230 # some ec2_ami_info tests to test if the filtering is working fine.
231 # ============================================================
232
233 - name: Gather info about the image
234 amazon.aws.ec2_ami_info:
235 image_ids: "{{ ec2_region_images[ec2_region] }}"
236 register: ami_info_result
237 ignore_errors: true
238
239 - name: Assert that the right image was found
240 ansible.builtin.assert:
241 that:
242 - ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
243
244 # ============================================================
245
246 - name: Gather info about the image using boolean filter
247 amazon.aws.ec2_ami_info:
248 image_ids: "{{ ec2_region_images[ec2_region] }}"
249 filters:
250 is-public: true
251 register: ami_info_result
252 ignore_errors: true
253
254 - name: Assert that the right image was found
255 ansible.builtin.assert:
256 that:
257 - ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
258
259 # ============================================================
260
261 - name: Gather info about the image using integer filter
262 amazon.aws.ec2_ami_info:
263 image_ids: "{{ ec2_region_images[ec2_region] }}"
264 filters:
265 # Amazon owned
266 owner-id: 137112412989
267 register: ami_info_result
268 ignore_errors: true
269
270 - name: Assert that the right image was found
271 ansible.builtin.assert:
272 that:
273 - ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
274
275 # ============================================================
276
277 - name: Gather info about the image using string filter
278 amazon.aws.ec2_ami_info:
279 image_ids: "{{ ec2_region_images[ec2_region] }}"
280 filters:
281 name: amzn-ami-hvm-2017.09.0.20170930-x86_64-gp2
282 register: ami_info_result
283 ignore_errors: true
284
285 - name: Assert that the right image was found
286 ansible.builtin.assert:
287 that:
288 - ami_info_result.images[0].image_id == ec2_region_images[ec2_region]
289
290 # ec2_ami_info filtering tests ends
291 # ============================================================
292
293 - name: Delete the image (check mode)
294 amazon.aws.ec2_ami:
295 instance_id: "{{ ec2_instance_id }}"
296 state: absent
297 delete_snapshot: true
298 name: "{{ ec2_ami_name }}_ami"
299 description: "{{ ec2_ami_description }}"
300 image_id: "{{ result.image_id }}"
301 tags:
302 Name: "{{ ec2_ami_name }}_ami"
303 wait: true
304 ignore_errors: true
305 check_mode: true
306 register: check_mode_result
307
308 - name: Assert that check_mode result is changed
309 ansible.builtin.assert:
310 that:
311 - check_mode_result is changed
312
313 - name: Delete the image
314 amazon.aws.ec2_ami:
315 instance_id: "{{ ec2_instance_id }}"
316 state: absent
317 delete_snapshot: true
318 name: "{{ ec2_ami_name }}_ami"
319 description: "{{ ec2_ami_description }}"
320 image_id: "{{ result.image_id }}"
321 tags:
322 Name: "{{ ec2_ami_name }}_ami"
323 wait: true
324 ignore_errors: true
325 register: result
326
327 - name: Assert that the image has been deleted
328 ansible.builtin.assert:
329 that:
330 - result.changed
331 - "'image_id' not in result"
332 - result.snapshots_deleted != []
333
334 # ==============================================================
335
336 - name: Test removing an ami if no image ID is provided (expected failed=true)
337 amazon.aws.ec2_ami:
338 state: absent
339 register: result
340 ignore_errors: true
341
342 - name: Assert that an image ID is required
343 ansible.builtin.assert:
344 that:
345 - result.failed
346 - "result.msg == 'state is absent but all of the following are missing: image_id'"
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: Remove setup security group
365 amazon.aws.ec2_security_group:
366 name: "{{ ec2_ami_name }}_setup"
367 description: created by Ansible integration tests
368 state: absent
369 vpc_id: "{{ setup_vpc.vpc.id }}"
370 ignore_errors: true
371
372 - name: Delete ami
373 amazon.aws.ec2_ami:
374 state: absent
375 image_id: "{{ ec2_ami_image_id_simple }}"
376 name: "{{ ec2_ami_name }}_ami"
377 wait: true
378 ignore_errors: true
379
380 - name: Delete ami
381 amazon.aws.ec2_ami:
382 state: absent
383 image_id: "{{ ec2_ami_no_device_true_image_id }}"
384 wait: true
385 ignore_errors: true
386
387 - name: Delete ami
388 amazon.aws.ec2_ami:
389 state: absent
390 image_id: "{{ ec2_ami_no_device_false_image_id }}"
391 wait: true
392 ignore_errors: true
393
394 - name: Remove setup keypair
395 amazon.aws.ec2_key:
396 name: "{{ ec2_ami_name }}_setup"
397 state: absent
398 ignore_errors: true
399
400 - name: Remove setup subnet
401 amazon.aws.ec2_vpc_subnet:
402 az: "{{ availability_zone }}"
403 tags: "{{ ec2_ami_name }}_setup"
404 vpc_id: "{{ setup_vpc.vpc.id }}"
405 cidr: "{{ subnet_cidr }}"
406 state: absent
407 resource_tags:
408 Name: "{{ ec2_ami_name }}_setup"
409 ignore_errors: true
410
411 - name: Remove setup VPC
412 amazon.aws.ec2_vpc_net:
413 cidr_block: "{{ vpc_cidr }}"
414 state: absent
415 name: "{{ ec2_ami_name }}_setup"
416 resource_tags:
417 Name: "{{ ec2_ami_name }}_setup"
418 ignore_errors: true