| 1 |
--- |
| 2 |
- name: Wrap up all tests and setup AWS credentials |
| 3 |
module_defaults: |
| 4 |
group/aws: |
| 5 |
access_key: "{{ aws_access_key }}" |
| 6 |
secret_key: "{{ aws_secret_key }}" |
| 7 |
session_token: "{{ security_token | default(omit) }}" |
| 8 |
region: "{{ aws_region }}" |
| 9 |
|
| 10 |
block: |
| 11 |
# ==== Env setup ========================================================== |
| 12 |
|
| 13 |
- name: Create a test VPC |
| 14 |
amazon.aws.ec2_vpc_net: |
| 15 |
name: "{{ vpc_name }}" |
| 16 |
cidr_block: "{{ vpc_cidr }}" |
| 17 |
tags: |
| 18 |
Name: Cloudformation testing |
| 19 |
register: testing_vpc |
| 20 |
|
| 21 |
- name: Create a test subnet |
| 22 |
amazon.aws.ec2_vpc_subnet: |
| 23 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 24 |
cidr: "{{ subnet_cidr }}" |
| 25 |
az: "{{ availability_zone }}" |
| 26 |
register: testing_subnet |
| 27 |
|
| 28 |
# ==== Cloudformation tests with disable_rollback ==================== |
| 29 |
|
| 30 |
- name: Run tests for testing update stack termination protection |
| 31 |
ansible.builtin.import_tasks: test_update_termination_protection.yml |
| 32 |
|
| 33 |
- name: Run tests for testing stack disable rollback |
| 34 |
ansible.builtin.import_tasks: test_disable_rollback.yml |
| 35 |
|
| 36 |
- name: Create a cloudformation stack (Check mode) |
| 37 |
amazon.aws.cloudformation: |
| 38 |
stack_name: "{{ stack_name }}" |
| 39 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 40 |
template_parameters: |
| 41 |
InstanceType: t3.nano |
| 42 |
ImageId: "{{ ec2_ami_id }}" |
| 43 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 44 |
tags: |
| 45 |
Stack: "{{ stack_name }}" |
| 46 |
test: "{{ resource_prefix }}" |
| 47 |
register: cf_stack |
| 48 |
check_mode: true |
| 49 |
|
| 50 |
- name: Check task return attributes |
| 51 |
ansible.builtin.assert: |
| 52 |
that: |
| 53 |
- cf_stack.changed |
| 54 |
- "'msg' in cf_stack and 'New stack would be created' in cf_stack.msg" |
| 55 |
|
| 56 |
- name: Create a cloudformation stack |
| 57 |
amazon.aws.cloudformation: |
| 58 |
stack_name: "{{ stack_name }}" |
| 59 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 60 |
template_parameters: |
| 61 |
InstanceType: t3.nano |
| 62 |
ImageId: "{{ ec2_ami_id }}" |
| 63 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 64 |
tags: |
| 65 |
Stack: "{{ stack_name }}" |
| 66 |
test: "{{ resource_prefix }}" |
| 67 |
register: cf_stack |
| 68 |
|
| 69 |
- name: Check task return attributes |
| 70 |
ansible.builtin.assert: |
| 71 |
that: |
| 72 |
- cf_stack.changed |
| 73 |
- "'events' in cf_stack" |
| 74 |
- "'output' in cf_stack and 'Stack CREATE complete' in cf_stack.output" |
| 75 |
- "'stack_outputs' in cf_stack and 'InstanceId' in cf_stack.stack_outputs" |
| 76 |
- "'stack_resources' in cf_stack" |
| 77 |
|
| 78 |
- name: Create a cloudformation stack (Check mode) (idempotent) |
| 79 |
amazon.aws.cloudformation: |
| 80 |
stack_name: "{{ stack_name }}" |
| 81 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 82 |
template_parameters: |
| 83 |
InstanceType: t3.nano |
| 84 |
ImageId: "{{ ec2_ami_id }}" |
| 85 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 86 |
tags: |
| 87 |
Stack: "{{ stack_name }}" |
| 88 |
test: "{{ resource_prefix }}" |
| 89 |
register: cf_stack |
| 90 |
check_mode: true |
| 91 |
|
| 92 |
- name: Check task return attributes |
| 93 |
ansible.builtin.assert: |
| 94 |
that: |
| 95 |
- not cf_stack.changed |
| 96 |
|
| 97 |
- name: Create a cloudformation stack (idempotent) |
| 98 |
amazon.aws.cloudformation: |
| 99 |
stack_name: "{{ stack_name }}" |
| 100 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 101 |
template_parameters: |
| 102 |
InstanceType: t3.nano |
| 103 |
ImageId: "{{ ec2_ami_id }}" |
| 104 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 105 |
tags: |
| 106 |
Stack: "{{ stack_name }}" |
| 107 |
test: "{{ resource_prefix }}" |
| 108 |
register: cf_stack |
| 109 |
|
| 110 |
- name: Check task return attributes |
| 111 |
ansible.builtin.assert: |
| 112 |
that: |
| 113 |
- not cf_stack.changed |
| 114 |
- "'output' in cf_stack and 'Stack is already up-to-date.' in cf_stack.output" |
| 115 |
- "'stack_outputs' in cf_stack and 'InstanceId' in cf_stack.stack_outputs" |
| 116 |
- "'stack_resources' in cf_stack" |
| 117 |
|
| 118 |
- name: Get all stacks details |
| 119 |
amazon.aws.cloudformation_info: |
| 120 |
register: all_stacks_info |
| 121 |
|
| 122 |
- name: Assert all stacks info |
| 123 |
ansible.builtin.assert: |
| 124 |
that: |
| 125 |
- all_stacks_info | length > 0 |
| 126 |
|
| 127 |
- name: Get stack details |
| 128 |
amazon.aws.cloudformation_info: |
| 129 |
stack_name: "{{ stack_name }}" |
| 130 |
register: stack_info |
| 131 |
|
| 132 |
- name: Assert stack info |
| 133 |
ansible.builtin.assert: |
| 134 |
that: |
| 135 |
- "'cloudformation' in stack_info" |
| 136 |
- stack_info.cloudformation | length == 1 |
| 137 |
- stack_name in stack_info.cloudformation |
| 138 |
- "'stack_description' in stack_info.cloudformation[stack_name]" |
| 139 |
- "'stack_outputs' in stack_info.cloudformation[stack_name]" |
| 140 |
- "'stack_parameters' in stack_info.cloudformation[stack_name]" |
| 141 |
- "'stack_tags' in stack_info.cloudformation[stack_name]" |
| 142 |
- stack_info.cloudformation[stack_name].stack_tags.Stack == stack_name |
| 143 |
|
| 144 |
- name: Get stack details (checkmode) |
| 145 |
amazon.aws.cloudformation_info: |
| 146 |
stack_name: "{{ stack_name }}" |
| 147 |
register: stack_info |
| 148 |
check_mode: true |
| 149 |
|
| 150 |
- name: Assert stack info |
| 151 |
ansible.builtin.assert: |
| 152 |
that: |
| 153 |
- "'cloudformation' in stack_info" |
| 154 |
- stack_info.cloudformation | length == 1 |
| 155 |
- stack_name in stack_info.cloudformation |
| 156 |
- "'stack_description' in stack_info.cloudformation[stack_name]" |
| 157 |
- "'stack_outputs' in stack_info.cloudformation[stack_name]" |
| 158 |
- "'stack_parameters' in stack_info.cloudformation[stack_name]" |
| 159 |
- "'stack_tags' in stack_info.cloudformation[stack_name]" |
| 160 |
- stack_info.cloudformation[stack_name].stack_tags.Stack == stack_name |
| 161 |
|
| 162 |
- name: Get stack details (all_facts) |
| 163 |
amazon.aws.cloudformation_info: |
| 164 |
stack_name: "{{ stack_name }}" |
| 165 |
all_facts: true |
| 166 |
register: stack_info |
| 167 |
|
| 168 |
- name: Assert stack info |
| 169 |
ansible.builtin.assert: |
| 170 |
that: |
| 171 |
- "'stack_events' in stack_info.cloudformation[stack_name]" |
| 172 |
- "'stack_policy' in stack_info.cloudformation[stack_name]" |
| 173 |
- "'stack_resource_list' in stack_info.cloudformation[stack_name]" |
| 174 |
- "'stack_resources' in stack_info.cloudformation[stack_name]" |
| 175 |
- "'stack_template' in stack_info.cloudformation[stack_name]" |
| 176 |
|
| 177 |
- name: Get stack details (all_facts) (checkmode) |
| 178 |
amazon.aws.cloudformation_info: |
| 179 |
stack_name: "{{ stack_name }}" |
| 180 |
all_facts: true |
| 181 |
register: stack_info |
| 182 |
check_mode: true |
| 183 |
|
| 184 |
- name: Assert stack info |
| 185 |
ansible.builtin.assert: |
| 186 |
that: |
| 187 |
- "'stack_events' in stack_info.cloudformation[stack_name]" |
| 188 |
- "'stack_policy' in stack_info.cloudformation[stack_name]" |
| 189 |
- "'stack_resource_list' in stack_info.cloudformation[stack_name]" |
| 190 |
- "'stack_resources' in stack_info.cloudformation[stack_name]" |
| 191 |
- "'stack_template' in stack_info.cloudformation[stack_name]" |
| 192 |
|
| 193 |
# ==== Cloudformation tests (Create changeset) ============================ |
| 194 |
|
| 195 |
# try to Create a changeset by changing instance type |
| 196 |
- name: Create a changeset |
| 197 |
amazon.aws.cloudformation: |
| 198 |
stack_name: "{{ stack_name }}" |
| 199 |
create_changeset: true |
| 200 |
changeset_name: test-changeset |
| 201 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 202 |
template_parameters: |
| 203 |
InstanceType: t3.micro |
| 204 |
ImageId: "{{ ec2_ami_id }}" |
| 205 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 206 |
tags: |
| 207 |
Stack: "{{ stack_name }}" |
| 208 |
test: "{{ resource_prefix }}" |
| 209 |
register: create_changeset_result |
| 210 |
|
| 211 |
- name: Assert changeset created |
| 212 |
ansible.builtin.assert: |
| 213 |
that: |
| 214 |
- create_changeset_result.changed |
| 215 |
- "'change_set_id' in create_changeset_result" |
| 216 |
- "'Stack CREATE_CHANGESET complete' in create_changeset_result.output" |
| 217 |
|
| 218 |
- name: Get stack details with changesets |
| 219 |
amazon.aws.cloudformation_info: |
| 220 |
stack_name: "{{ stack_name }}" |
| 221 |
stack_change_sets: true |
| 222 |
register: stack_info |
| 223 |
|
| 224 |
- name: Assert changesets in info |
| 225 |
ansible.builtin.assert: |
| 226 |
that: |
| 227 |
- "'stack_change_sets' in stack_info.cloudformation[stack_name]" |
| 228 |
|
| 229 |
- name: Get stack details with changesets (checkmode) |
| 230 |
amazon.aws.cloudformation_info: |
| 231 |
stack_name: "{{ stack_name }}" |
| 232 |
stack_change_sets: true |
| 233 |
register: stack_info |
| 234 |
check_mode: true |
| 235 |
|
| 236 |
- name: Assert changesets in info |
| 237 |
ansible.builtin.assert: |
| 238 |
that: |
| 239 |
- "'stack_change_sets' in stack_info.cloudformation[stack_name]" |
| 240 |
|
| 241 |
# try to Create an empty changeset by passing in unchanged template |
| 242 |
- name: Create a changeset |
| 243 |
amazon.aws.cloudformation: |
| 244 |
stack_name: "{{ stack_name }}" |
| 245 |
create_changeset: true |
| 246 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 247 |
template_parameters: |
| 248 |
InstanceType: t3.nano |
| 249 |
ImageId: "{{ ec2_ami_id }}" |
| 250 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 251 |
tags: |
| 252 |
Stack: "{{ stack_name }}" |
| 253 |
test: "{{ resource_prefix }}" |
| 254 |
register: create_changeset_result |
| 255 |
|
| 256 |
- name: Assert changeset created |
| 257 |
ansible.builtin.assert: |
| 258 |
that: |
| 259 |
- not create_changeset_result.changed |
| 260 |
- "'The created Change Set did not contain any changes to this stack and was deleted.' in create_changeset_result.output" |
| 261 |
|
| 262 |
# ==== Cloudformation tests (termination_protection) ====================== |
| 263 |
|
| 264 |
- name: Set termination protection to true |
| 265 |
amazon.aws.cloudformation: |
| 266 |
stack_name: "{{ stack_name }}" |
| 267 |
termination_protection: true |
| 268 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 269 |
template_parameters: |
| 270 |
InstanceType: t3.nano |
| 271 |
ImageId: "{{ ec2_ami_id }}" |
| 272 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 273 |
tags: |
| 274 |
Stack: "{{ stack_name }}" |
| 275 |
test: "{{ resource_prefix }}" |
| 276 |
register: cf_stack |
| 277 |
|
| 278 |
# This fails - #65592 |
| 279 |
# - name: check task return attributes |
| 280 |
# assert: |
| 281 |
# that: |
| 282 |
# - cf_stack.changed |
| 283 |
|
| 284 |
- name: Get stack details |
| 285 |
amazon.aws.cloudformation_info: |
| 286 |
stack_name: "{{ stack_name }}" |
| 287 |
register: stack_info |
| 288 |
|
| 289 |
- name: Assert stack info |
| 290 |
ansible.builtin.assert: |
| 291 |
that: |
| 292 |
- stack_info.cloudformation[stack_name].stack_description.enable_termination_protection |
| 293 |
|
| 294 |
- name: Get stack details (checkmode) |
| 295 |
amazon.aws.cloudformation_info: |
| 296 |
stack_name: "{{ stack_name }}" |
| 297 |
register: stack_info |
| 298 |
check_mode: true |
| 299 |
|
| 300 |
- name: Assert stack info |
| 301 |
ansible.builtin.assert: |
| 302 |
that: |
| 303 |
- stack_info.cloudformation[stack_name].stack_description.enable_termination_protection |
| 304 |
|
| 305 |
- name: Set termination protection to false |
| 306 |
amazon.aws.cloudformation: |
| 307 |
stack_name: "{{ stack_name }}" |
| 308 |
termination_protection: false |
| 309 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 310 |
template_parameters: |
| 311 |
InstanceType: t3.nano |
| 312 |
ImageId: "{{ ec2_ami_id }}" |
| 313 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 314 |
tags: |
| 315 |
Stack: "{{ stack_name }}" |
| 316 |
test: "{{ resource_prefix }}" |
| 317 |
register: cf_stack |
| 318 |
|
| 319 |
# This fails - #65592 |
| 320 |
# - name: check task return attributes |
| 321 |
# assert: |
| 322 |
# that: |
| 323 |
# - cf_stack.changed |
| 324 |
|
| 325 |
- name: Get stack details |
| 326 |
amazon.aws.cloudformation_info: |
| 327 |
stack_name: "{{ stack_name }}" |
| 328 |
register: stack_info |
| 329 |
|
| 330 |
- name: Assert stack info |
| 331 |
ansible.builtin.assert: |
| 332 |
that: |
| 333 |
- not stack_info.cloudformation[stack_name].stack_description.enable_termination_protection |
| 334 |
|
| 335 |
- name: Get stack details (checkmode) |
| 336 |
amazon.aws.cloudformation_info: |
| 337 |
stack_name: "{{ stack_name }}" |
| 338 |
register: stack_info |
| 339 |
check_mode: true |
| 340 |
|
| 341 |
- name: Assert stack info |
| 342 |
ansible.builtin.assert: |
| 343 |
that: |
| 344 |
- not stack_info.cloudformation[stack_name].stack_description.enable_termination_protection |
| 345 |
|
| 346 |
# ==== Cloudformation tests (update_policy) ====================== |
| 347 |
|
| 348 |
- name: Setting an stack policy with json body |
| 349 |
amazon.aws.cloudformation: |
| 350 |
stack_name: "{{ stack_name }}" |
| 351 |
stack_policy_body: "{{ lookup('file', 'update_policy.json') }}" |
| 352 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 353 |
template_parameters: |
| 354 |
InstanceType: t3.nano |
| 355 |
ImageId: "{{ ec2_ami_id }}" |
| 356 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 357 |
tags: |
| 358 |
Stack: "{{ stack_name }}" |
| 359 |
test: "{{ resource_prefix }}" |
| 360 |
register: cf_stack |
| 361 |
|
| 362 |
- name: Get stack details |
| 363 |
amazon.aws.cloudformation_info: |
| 364 |
stack_name: "{{ stack_name }}" |
| 365 |
register: stack_info |
| 366 |
|
| 367 |
- name: Setting an stack policy on update |
| 368 |
amazon.aws.cloudformation: |
| 369 |
stack_name: "{{ stack_name }}" |
| 370 |
stack_policy_on_update_body: "{{ lookup('file', 'update_policy.json') }}" |
| 371 |
template_body: "{{ lookup('file', 'cf_template.json') }}" |
| 372 |
template_parameters: |
| 373 |
InstanceType: t3.nano |
| 374 |
ImageId: "{{ ec2_ami_id }}" |
| 375 |
SubnetId: "{{ testing_subnet.subnet.id }}" |
| 376 |
tags: |
| 377 |
Stack: "{{ stack_name }}" |
| 378 |
test: "{{ resource_prefix }}" |
| 379 |
register: cf_stack |
| 380 |
|
| 381 |
- name: Get stack details |
| 382 |
amazon.aws.cloudformation_info: |
| 383 |
stack_name: "{{ stack_name }}" |
| 384 |
register: stack_info |
| 385 |
|
| 386 |
# ==== Cloudformation tests (delete stack tests) ========================== |
| 387 |
|
| 388 |
- name: Delete cloudformation stack (Check mode) |
| 389 |
amazon.aws.cloudformation: |
| 390 |
stack_name: "{{ stack_name }}" |
| 391 |
state: absent |
| 392 |
check_mode: true |
| 393 |
register: cf_stack |
| 394 |
|
| 395 |
- name: Check task return attributes |
| 396 |
ansible.builtin.assert: |
| 397 |
that: |
| 398 |
- cf_stack.changed |
| 399 |
- "'msg' in cf_stack and 'Stack would be deleted' in cf_stack.msg" |
| 400 |
|
| 401 |
- name: Delete cloudformation stack |
| 402 |
amazon.aws.cloudformation: |
| 403 |
stack_name: "{{ stack_name }}" |
| 404 |
state: absent |
| 405 |
register: cf_stack |
| 406 |
|
| 407 |
- name: Check task return attributes |
| 408 |
ansible.builtin.assert: |
| 409 |
that: |
| 410 |
- cf_stack.changed |
| 411 |
- "'output' in cf_stack and 'Stack Deleted' in cf_stack.output" |
| 412 |
|
| 413 |
- name: Delete cloudformation stack (Check mode) (idempotent) |
| 414 |
amazon.aws.cloudformation: |
| 415 |
stack_name: "{{ stack_name }}" |
| 416 |
state: absent |
| 417 |
check_mode: true |
| 418 |
register: cf_stack |
| 419 |
|
| 420 |
- name: Check task return attributes |
| 421 |
ansible.builtin.assert: |
| 422 |
that: |
| 423 |
- not cf_stack.changed |
| 424 |
- "'msg' in cf_stack" |
| 425 |
- >- |
| 426 |
"Stack doesn't exist" in cf_stack.msg |
| 427 |
|
| 428 |
- name: Delete cloudformation stack (idempotent) |
| 429 |
amazon.aws.cloudformation: |
| 430 |
stack_name: "{{ stack_name }}" |
| 431 |
state: absent |
| 432 |
register: cf_stack |
| 433 |
|
| 434 |
- name: Check task return attributes |
| 435 |
ansible.builtin.assert: |
| 436 |
that: |
| 437 |
- not cf_stack.changed |
| 438 |
- "'output' in cf_stack and 'Stack not found.' in cf_stack.output" |
| 439 |
|
| 440 |
- name: Get stack details |
| 441 |
amazon.aws.cloudformation_info: |
| 442 |
stack_name: "{{ stack_name }}" |
| 443 |
register: stack_info |
| 444 |
|
| 445 |
- name: Assert stack info |
| 446 |
ansible.builtin.assert: |
| 447 |
that: |
| 448 |
- not stack_info.cloudformation |
| 449 |
|
| 450 |
- name: Get stack details (checkmode) |
| 451 |
amazon.aws.cloudformation_info: |
| 452 |
stack_name: "{{ stack_name }}" |
| 453 |
register: stack_info |
| 454 |
check_mode: true |
| 455 |
|
| 456 |
- name: Assert stack info |
| 457 |
ansible.builtin.assert: |
| 458 |
that: |
| 459 |
- not stack_info.cloudformation |
| 460 |
|
| 461 |
# ==== Cleanup ============================================================ |
| 462 |
|
| 463 |
always: |
| 464 |
- name: Delete stack |
| 465 |
amazon.aws.cloudformation: |
| 466 |
stack_name: "{{ stack_name }}" |
| 467 |
state: absent |
| 468 |
ignore_errors: true |
| 469 |
|
| 470 |
- name: Delete test subnet |
| 471 |
amazon.aws.ec2_vpc_subnet: |
| 472 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 473 |
cidr: "{{ subnet_cidr }}" |
| 474 |
state: absent |
| 475 |
ignore_errors: true |
| 476 |
|
| 477 |
- name: Delete test VPC |
| 478 |
amazon.aws.ec2_vpc_net: |
| 479 |
name: "{{ vpc_name }}" |
| 480 |
cidr_block: "{{ vpc_cidr }}" |
| 481 |
state: absent |
| 482 |
ignore_errors: true |