Debian Codegraph @

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

raw
1 ---
2 # Tests for iam_instance_profile and iam_instance_profile_info
3 #
4
5 - name: Setup AWS connection info
6 module_defaults:
7 group/aws:
8 access_key: "{{ aws_access_key }}"
9 secret_key: "{{ aws_secret_key }}"
10 session_token: "{{ security_token | default(omit) }}"
11 region: "{{ aws_region }}"
12 block:
13 # ===================================================================
14 # Prepare
15
16 - name: Prepare IAM Roles
17 amazon.aws.iam_role:
18 state: present
19 name: "{{ item }}"
20 path: "{{ test_path }}"
21 create_instance_profile: true
22 assume_role_policy_document: '{{ lookup("file", "deny-assume.json") }}'
23 managed_policies:
24 - "{{ safe_managed_policy }}"
25 wait: true
26 loop:
27 - "{{ test_role }}"
28 - "{{ test_role }}-2"
29
30 # ===================================================================
31 # Test
32
33 # ===================================================================
34
35 - name: Create minimal Instance Profile (CHECK)
36 amazon.aws.iam_instance_profile:
37 name: "{{ test_profile }}"
38 check_mode: true
39 register: profile_result
40
41 - ansible.builtin.assert:
42 that:
43 - profile_result is changed
44
45 - name: Create minimal Instance Profile
46 amazon.aws.iam_instance_profile:
47 name: "{{ test_profile }}"
48 register: profile_result
49
50 - ansible.builtin.assert:
51 that:
52 - profile_result is changed
53
54 - name: Create minimal Instance Profile - Idempotent (CHECK)
55 amazon.aws.iam_instance_profile:
56 name: "{{ test_profile }}"
57 check_mode: true
58 register: profile_result
59
60 - ansible.builtin.assert:
61 that:
62 - profile_result is not changed
63
64 - name: Create minimal Instance Profile - Idempotent
65 amazon.aws.iam_instance_profile:
66 name: "{{ test_profile }}"
67 register: profile_result
68
69 - ansible.builtin.assert:
70 that:
71 - profile_result is not changed
72
73 # ===================================================================
74
75 - ansible.builtin.include_tasks: tags.yml
76 - name: Add role to Instance Profile (CHECK)
77 amazon.aws.iam_instance_profile:
78 name: "{{ test_profile }}"
79 role: "{{ test_role }}"
80 check_mode: true
81 register: profile_result
82
83 - ansible.builtin.assert:
84 that:
85 - profile_result is changed
86
87 - name: Add role to Instance Profile
88 amazon.aws.iam_instance_profile:
89 name: "{{ test_profile }}"
90 role: "{{ test_role }}"
91 register: profile_result
92
93 - ansible.builtin.assert:
94 that:
95 - profile_result is changed
96
97 - name: Add role to Instance Profile - Idempotent (CHECK)
98 amazon.aws.iam_instance_profile:
99 name: "{{ test_profile }}"
100 role: "{{ test_role }}"
101 check_mode: true
102 register: profile_result
103
104 - ansible.builtin.assert:
105 that:
106 - profile_result is not changed
107
108 - name: Add role to Instance Profile - Idempotent
109 amazon.aws.iam_instance_profile:
110 name: "{{ test_profile }}"
111 role: "{{ test_role }}"
112 register: profile_result
113
114 - ansible.builtin.assert:
115 that:
116 - profile_result is not changed
117
118 # =====
119
120 - name: Replace role on Instance Profile (CHECK)
121 amazon.aws.iam_instance_profile:
122 name: "{{ test_profile }}"
123 role: "{{ test_role }}-2"
124 check_mode: true
125 register: profile_result
126
127 - ansible.builtin.assert:
128 that:
129 - profile_result is changed
130
131 - name: Replace role on Instance Profile
132 amazon.aws.iam_instance_profile:
133 name: "{{ test_profile }}"
134 role: "{{ test_role }}-2"
135 register: profile_result
136
137 - ansible.builtin.assert:
138 that:
139 - profile_result is changed
140
141 - name: Replace role on Instance Profile - Idempotent (CHECK)
142 amazon.aws.iam_instance_profile:
143 name: "{{ test_profile }}"
144 role: "{{ test_role }}-2"
145 check_mode: true
146 register: profile_result
147
148 - ansible.builtin.assert:
149 that:
150 - profile_result is not changed
151
152 - name: Replace role on Instance Profile - Idempotent
153 amazon.aws.iam_instance_profile:
154 name: "{{ test_profile }}"
155 role: "{{ test_role }}-2"
156 register: profile_result
157
158 - ansible.builtin.assert:
159 that:
160 - profile_result is not changed
161
162 # =====
163
164 - name: Remove role from Instance Profile (CHECK)
165 amazon.aws.iam_instance_profile:
166 name: "{{ test_profile }}"
167 role: ""
168 check_mode: true
169 register: profile_result
170
171 - ansible.builtin.assert:
172 that:
173 - profile_result is changed
174
175 - name: Remove role from Instance Profile
176 amazon.aws.iam_instance_profile:
177 name: "{{ test_profile }}"
178 role: ""
179 register: profile_result
180
181 - ansible.builtin.assert:
182 that:
183 - profile_result is changed
184
185 - name: Remove role from Instance Profile - Idempotent (CHECK)
186 amazon.aws.iam_instance_profile:
187 name: "{{ test_profile }}"
188 role: ""
189 check_mode: true
190 register: profile_result
191
192 - ansible.builtin.assert:
193 that:
194 - profile_result is not changed
195
196 - name: Remove role from Instance Profile - Idempotent
197 amazon.aws.iam_instance_profile:
198 name: "{{ test_profile }}"
199 role: ""
200 register: profile_result
201
202 - ansible.builtin.assert:
203 that:
204 - profile_result is not changed
205
206 # ===================================================================
207
208 - name: Create complex Instance Profile (CHECK)
209 amazon.aws.iam_instance_profile:
210 name: "{{ test_profile_complex }}"
211 role: "{{ test_role }}-2"
212 path: "{{ test_path }}"
213 tags: "{{ test_tags }}"
214 check_mode: true
215 register: profile_result
216
217 - ansible.builtin.assert:
218 that:
219 - profile_result is changed
220
221 - name: Create complex Instance Profile
222 amazon.aws.iam_instance_profile:
223 name: "{{ test_profile_complex }}"
224 role: "{{ test_role }}-2"
225 path: "{{ test_path }}"
226 tags: "{{ test_tags }}"
227 register: profile_result
228
229 - ansible.builtin.assert:
230 that:
231 - profile_result is changed
232
233 - name: Create complex Instance Profile - Idempotent (CHECK)
234 amazon.aws.iam_instance_profile:
235 name: "{{ test_profile_complex }}"
236 role: "{{ test_role }}-2"
237 path: "{{ test_path }}"
238 tags: "{{ test_tags }}"
239 check_mode: true
240 register: profile_result
241
242 - ansible.builtin.assert:
243 that:
244 - profile_result is not changed
245
246 - name: Create complex Instance Profile - Idempotent
247 amazon.aws.iam_instance_profile:
248 name: "{{ test_profile_complex }}"
249 role: "{{ test_role }}-2"
250 path: "{{ test_path }}"
251 tags: "{{ test_tags }}"
252 register: profile_result
253
254 - ansible.builtin.assert:
255 that:
256 - profile_result is not changed
257
258 # ===================================================================
259
260 - name: Update path for complex Instance Profile - no change can be made
261 amazon.aws.iam_instance_profile:
262 name: "{{ test_profile_complex }}"
263 role: "{{ test_role }}-2"
264 path: "{{ test_path }}subpath/"
265 register: profile_result
266
267 - ansible.builtin.assert:
268 that:
269 - profile_result is not changed
270
271 # ===================================================================
272
273 - name: List all Instance Profiles (no filter)
274 amazon.aws.iam_instance_profile_info:
275 register: profile_info
276
277 - ansible.builtin.assert:
278 that:
279 - profile_info.iam_instance_profiles | length >= 4
280 - test_role in profile_names
281 - test_role+"-2" in profile_names
282 - test_profile in profile_names
283 - test_profile_complex in profile_names
284
285 - '"arn" in complex_profile'
286 - '"create_date" in complex_profile'
287 - '"instance_profile_id" in complex_profile'
288 - '"instance_profile_name" in complex_profile'
289 - complex_profile.instance_profile_name == test_profile_complex
290 - '"path" in complex_profile'
291 - complex_profile.path == test_path
292 - '"roles" in complex_profile'
293 - complex_profile.roles | length == 1
294 - '"arn" in complex_profile.roles[0]'
295 - '"assume_role_policy_document" in complex_profile.roles[0]'
296 - '"create_date" in complex_profile.roles[0]'
297 - '"path" in complex_profile.roles[0]'
298 - complex_profile.roles[0].path == test_path
299 - '"role_id" in complex_profile.roles[0]'
300 - '"role_name" in complex_profile.roles[0]'
301 - complex_profile.roles[0].role_name == test_role+"-2"
302 vars:
303 profile_names: '{{ profile_info.iam_instance_profiles | map(attribute="instance_profile_name") }}'
304 complex_profile: '{{ profile_info.iam_instance_profiles | selectattr("instance_profile_name", "match", test_profile_complex) | first }}'
305
306 - name: List all Instance Profiles (filter by path)
307 amazon.aws.iam_instance_profile_info:
308 path: "{{ test_path }}"
309 register: profile_info
310
311 - ansible.builtin.assert:
312 that:
313 - profile_info.iam_instance_profiles | length == 3
314 - test_role in profile_names
315 - test_role+"-2" in profile_names
316 - test_profile_complex in profile_names
317
318 - '"arn" in complex_profile'
319 - '"create_date" in complex_profile'
320 - '"instance_profile_id" in complex_profile'
321 - '"instance_profile_name" in complex_profile'
322 - complex_profile.instance_profile_name == test_profile_complex
323 - '"path" in complex_profile'
324 - complex_profile.path == test_path
325 - '"roles" in complex_profile'
326 - complex_profile.roles | length == 1
327 - '"arn" in complex_profile.roles[0]'
328 - '"assume_role_policy_document" in complex_profile.roles[0]'
329 - '"create_date" in complex_profile.roles[0]'
330 - '"path" in complex_profile.roles[0]'
331 - complex_profile.roles[0].path == test_path
332 - '"role_id" in complex_profile.roles[0]'
333 - '"role_name" in complex_profile.roles[0]'
334 - complex_profile.roles[0].role_name == test_role+"-2"
335 vars:
336 profile_names: '{{ profile_info.iam_instance_profiles | map(attribute="instance_profile_name") }}'
337 complex_profile: '{{ profile_info.iam_instance_profiles | selectattr("instance_profile_name", "match", test_profile_complex) | first }}'
338
339 - name: List all Instance Profiles (filter by name - complex)
340 amazon.aws.iam_instance_profile_info:
341 name: "{{ test_profile_complex }}"
342 register: profile_info
343
344 - ansible.builtin.assert:
345 that:
346 - profile_info.iam_instance_profiles | length == 1
347 - test_profile_complex in profile_names
348
349 - '"arn" in complex_profile'
350 - '"create_date" in complex_profile'
351 - '"instance_profile_id" in complex_profile'
352 - '"instance_profile_name" in complex_profile'
353 - complex_profile.instance_profile_name == test_profile_complex
354 - '"path" in complex_profile'
355 - complex_profile.path == test_path
356 - '"tags" in complex_profile'
357 - complex_profile.tags == test_tags
358 - '"roles" in complex_profile'
359 - complex_profile.roles | length == 1
360 - '"arn" in complex_profile.roles[0]'
361 - '"assume_role_policy_document" in complex_profile.roles[0]'
362 - '"create_date" in complex_profile.roles[0]'
363 - '"path" in complex_profile.roles[0]'
364 - complex_profile.roles[0].path == test_path
365 - '"role_id" in complex_profile.roles[0]'
366 - '"role_name" in complex_profile.roles[0]'
367 - complex_profile.roles[0].role_name == test_role+"-2"
368 - '"tags" in complex_profile.roles[0]'
369 - complex_profile.roles[0].tags == {}
370 vars:
371 profile_names: '{{ profile_info.iam_instance_profiles | map(attribute="instance_profile_name") }}'
372 complex_profile: '{{ profile_info.iam_instance_profiles | selectattr("instance_profile_name", "match", test_profile_complex) | first }}'
373
374 - name: List an Instance Profile (filter by name)
375 amazon.aws.iam_instance_profile_info:
376 name: "{{ test_profile }}"
377 register: profile_info
378
379 - ansible.builtin.assert:
380 that:
381 - profile_info.iam_instance_profiles | length == 1
382 - '"arn" in simple_profile'
383 - '"create_date" in simple_profile'
384 - '"instance_profile_id" in simple_profile'
385 - '"instance_profile_name" in simple_profile'
386 - simple_profile.instance_profile_name == test_profile
387 - '"path" in simple_profile'
388 - simple_profile.path == "/"
389 - '"tags" in simple_profile'
390 - simple_profile.tags == {}
391 - '"roles" in simple_profile'
392 - simple_profile.roles | length == 0
393 vars:
394 simple_profile: "{{ profile_info.iam_instance_profiles[0] }}"
395
396 # ===================================================================
397
398 - name: Delete minimal Instance Profile (CHECK)
399 amazon.aws.iam_instance_profile:
400 state: absent
401 name: "{{ test_profile }}"
402 check_mode: true
403 register: profile_result
404
405 - ansible.builtin.assert:
406 that:
407 - profile_result is changed
408
409 - name: Delete minimal Instance Profile
410 amazon.aws.iam_instance_profile:
411 state: absent
412 name: "{{ test_profile }}"
413 register: profile_result
414
415 - ansible.builtin.assert:
416 that:
417 - profile_result is changed
418
419 - name: Delete minimal Instance Profile - Idempotent (CHECK)
420 amazon.aws.iam_instance_profile:
421 state: absent
422 name: "{{ test_profile }}"
423 check_mode: true
424 register: profile_result
425
426 - ansible.builtin.assert:
427 that:
428 - profile_result is not changed
429
430 - name: Delete minimal Instance Profile - Idempotent
431 amazon.aws.iam_instance_profile:
432 state: absent
433 name: "{{ test_profile }}"
434 register: profile_result
435
436 - ansible.builtin.assert:
437 that:
438 - profile_result is not changed
439
440 # ===================================================================
441
442 - name: Delete complex Instance Profile (CHECK)
443 amazon.aws.iam_instance_profile:
444 state: absent
445 name: "{{ test_profile_complex }}"
446 check_mode: true
447 register: profile_result
448
449 - ansible.builtin.assert:
450 that:
451 - profile_result is changed
452
453 - name: Delete complex Instance Profile
454 amazon.aws.iam_instance_profile:
455 state: absent
456 name: "{{ test_profile_complex }}"
457 register: profile_result
458
459 - ansible.builtin.assert:
460 that:
461 - profile_result is changed
462
463 - name: Delete complex Instance Profile - Idempotent (CHECK)
464 amazon.aws.iam_instance_profile:
465 state: absent
466 name: "{{ test_profile_complex }}"
467 check_mode: true
468 register: profile_result
469
470 - ansible.builtin.assert:
471 that:
472 - profile_result is not changed
473
474 - name: Delete complex Instance Profile - Idempotent
475 amazon.aws.iam_instance_profile:
476 state: absent
477 name: "{{ test_profile_complex }}"
478 register: profile_result
479
480 - ansible.builtin.assert:
481 that:
482 - profile_result is not changed
483
484 always:
485 # ===================================================================
486 # Cleanup
487
488 # - name: "iam_instance_profile_info after Role deletion"
489 # amazon.aws.iam_instance_profile_info:
490 # ignore_errors: true
491
492 - name: Delete Instance Profiles
493 amazon.aws.iam_instance_profile:
494 state: absent
495 name: "{{ item }}"
496 ignore_errors: true
497 loop:
498 - "{{ test_profile }}"
499 - "{{ test_profile_complex }}"
500 - "{{ test_role }}"
501 - "{{ test_role }}-2"
502
503 - name: Remove IAM Roles
504 amazon.aws.iam_role:
505 state: absent
506 name: "{{ item }}"
507 path: "{{ test_path }}"
508 delete_instance_profile: true
509 ignore_errors: true
510 loop:
511 - "{{ test_role }}"
512 - "{{ test_role }}-2"
513
514 # - name: "iam_role_info after Role deletion"
515 # amazon.aws.iam_role_info:
516 # path: "{{ test_path }}"
517 # ignore_errors: true