| 1 |
--- |
| 2 |
- ansible.builtin.set_fact: |
| 3 |
# lookup plugins don't have access to module_defaults |
| 4 |
connection_args: |
| 5 |
region: "{{ aws_region }}" |
| 6 |
access_key: "{{ aws_access_key }}" |
| 7 |
secret_key: "{{ aws_secret_key }}" |
| 8 |
session_token: "{{ security_token | default(omit) }}" |
| 9 |
no_log: true |
| 10 |
|
| 11 |
# ============================================================ |
| 12 |
- name: Run all tests |
| 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 |
block: |
| 20 |
- name: Determine if there is a default VPC |
| 21 |
ansible.builtin.set_fact: |
| 22 |
defaultvpc: "{{ lookup('amazon.aws.aws_account_attribute', attribute='default-vpc', **connection_args) }}" |
| 23 |
register: default_vpc |
| 24 |
|
| 25 |
- name: Create a VPC |
| 26 |
amazon.aws.ec2_vpc_net: |
| 27 |
name: "{{ resource_prefix }}-vpc" |
| 28 |
state: present |
| 29 |
cidr_block: "{{ vpc_cidr }}" |
| 30 |
tags: |
| 31 |
Name: "{{ resource_prefix }}-vpc" |
| 32 |
Description: Created by ansible-test |
| 33 |
register: vpc_result |
| 34 |
## TODO(ryansb): Update CI for VPC peering permissions |
| 35 |
# - include_tasks: ./multi_account.yml |
| 36 |
- ansible.builtin.include_tasks: ./diff_mode.yml |
| 37 |
- ansible.builtin.include_tasks: ./numeric_protos.yml |
| 38 |
- ansible.builtin.include_tasks: ./rule_group_create.yml |
| 39 |
- ansible.builtin.include_tasks: ./egress_tests.yml |
| 40 |
- ansible.builtin.include_tasks: ./icmp_verbs.yml |
| 41 |
- ansible.builtin.include_tasks: ./data_validation.yml |
| 42 |
- ansible.builtin.include_tasks: ./multi_nested_target.yml |
| 43 |
- ansible.builtin.include_tasks: ./group_info.yml |
| 44 |
- name: Test state=absent (CHECK MODE) |
| 45 |
amazon.aws.ec2_security_group: |
| 46 |
name: "{{ ec2_group_name }}" |
| 47 |
description: "{{ ec2_group_description }}" |
| 48 |
state: absent |
| 49 |
check_mode: true |
| 50 |
register: result |
| 51 |
|
| 52 |
- name: Assert no changes would be made |
| 53 |
ansible.builtin.assert: |
| 54 |
that: |
| 55 |
- not result.changed |
| 56 |
|
| 57 |
# =========================================================== |
| 58 |
- name: Test state=absent |
| 59 |
amazon.aws.ec2_security_group: |
| 60 |
name: "{{ ec2_group_name }}" |
| 61 |
description: "{{ ec2_group_description }}" |
| 62 |
state: absent |
| 63 |
register: result |
| 64 |
|
| 65 |
# ============================================================ |
| 66 |
- name: Test state=present (expected changed=true) (CHECK MODE) |
| 67 |
amazon.aws.ec2_security_group: |
| 68 |
name: "{{ ec2_group_name }}" |
| 69 |
description: "{{ ec2_group_description }}" |
| 70 |
state: present |
| 71 |
check_mode: true |
| 72 |
register: result |
| 73 |
|
| 74 |
- name: Assert state=present (expected changed=true) |
| 75 |
ansible.builtin.assert: |
| 76 |
that: |
| 77 |
- result.changed |
| 78 |
|
| 79 |
# ============================================================ |
| 80 |
- name: Test state=present (expected changed=true) |
| 81 |
amazon.aws.ec2_security_group: |
| 82 |
name: "{{ ec2_group_name }}" |
| 83 |
description: "{{ ec2_group_description }}" |
| 84 |
state: present |
| 85 |
register: result |
| 86 |
|
| 87 |
- name: Assert state=present (expected changed=true) |
| 88 |
ansible.builtin.assert: |
| 89 |
that: |
| 90 |
- result.changed |
| 91 |
- result.group_id.startswith("sg-") |
| 92 |
|
| 93 |
# ============================================================ |
| 94 |
- name: Test state=present different description (expected changed=false) (CHECK MODE) |
| 95 |
amazon.aws.ec2_security_group: |
| 96 |
name: "{{ ec2_group_name }}" |
| 97 |
description: "{{ ec2_group_description }}CHANGED" |
| 98 |
state: present |
| 99 |
check_mode: true |
| 100 |
register: result |
| 101 |
|
| 102 |
- name: Assert state=present (expected changed=false) |
| 103 |
ansible.builtin.assert: |
| 104 |
that: |
| 105 |
- not result.changed |
| 106 |
|
| 107 |
# ============================================================ |
| 108 |
- name: Test state=present different description (expected changed=false) |
| 109 |
amazon.aws.ec2_security_group: |
| 110 |
name: "{{ ec2_group_name }}" |
| 111 |
description: "{{ ec2_group_description }}CHANGED" |
| 112 |
state: present |
| 113 |
ignore_errors: true |
| 114 |
register: result |
| 115 |
|
| 116 |
- name: Assert state=present (expected changed=false) |
| 117 |
ansible.builtin.assert: |
| 118 |
that: |
| 119 |
- not result.changed |
| 120 |
- result.group_id.startswith("sg-") |
| 121 |
|
| 122 |
# ============================================================ |
| 123 |
- name: Test state=present (expected changed=false) |
| 124 |
amazon.aws.ec2_security_group: |
| 125 |
name: "{{ ec2_group_name }}" |
| 126 |
description: "{{ ec2_group_description }}" |
| 127 |
state: present |
| 128 |
register: result |
| 129 |
|
| 130 |
- name: Assert state=present (expected changed=false) |
| 131 |
ansible.builtin.assert: |
| 132 |
that: |
| 133 |
- not result.changed |
| 134 |
- result.group_id.startswith("sg-") |
| 135 |
|
| 136 |
# ============================================================ |
| 137 |
- name: Tests IPv6 with the default VPC |
| 138 |
ansible.builtin.include_tasks: ./ipv6_default_tests.yml |
| 139 |
when: default_vpc != "" |
| 140 |
|
| 141 |
- name: Test IPv6 with a specified VPC |
| 142 |
block: |
| 143 |
# ============================================================ |
| 144 |
- name: Test state=present (expected changed=true) (CHECK MODE) |
| 145 |
amazon.aws.ec2_security_group: |
| 146 |
name: "{{ ec2_group_name }}-2" |
| 147 |
description: "{{ ec2_group_description }}-2" |
| 148 |
state: present |
| 149 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 150 |
check_mode: true |
| 151 |
register: result |
| 152 |
|
| 153 |
- name: Assert state=present (expected changed=true) |
| 154 |
ansible.builtin.assert: |
| 155 |
that: |
| 156 |
- result.changed |
| 157 |
|
| 158 |
# ============================================================ |
| 159 |
- name: Test state=present (expected changed=true) |
| 160 |
amazon.aws.ec2_security_group: |
| 161 |
name: "{{ ec2_group_name }}-2" |
| 162 |
description: "{{ ec2_group_description }}-2" |
| 163 |
state: present |
| 164 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 165 |
register: result |
| 166 |
|
| 167 |
- name: Assert state=present (expected changed=true) |
| 168 |
ansible.builtin.assert: |
| 169 |
that: |
| 170 |
- result.changed |
| 171 |
- result.group_id.startswith("sg-") |
| 172 |
|
| 173 |
# ============================================================ |
| 174 |
- name: Test state=present for ipv6 (expected changed=true) (CHECK MODE) |
| 175 |
amazon.aws.ec2_security_group: |
| 176 |
name: "{{ ec2_group_name }}-2" |
| 177 |
description: "{{ ec2_group_description }}-2" |
| 178 |
state: present |
| 179 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 180 |
rules: |
| 181 |
- proto: tcp |
| 182 |
from_port: 8182 |
| 183 |
to_port: 8182 |
| 184 |
cidr_ipv6: 64:ff9b::/96 |
| 185 |
check_mode: true |
| 186 |
register: result |
| 187 |
|
| 188 |
- name: Assert state=present (expected changed=true) |
| 189 |
ansible.builtin.assert: |
| 190 |
that: |
| 191 |
- result.changed |
| 192 |
|
| 193 |
# ============================================================ |
| 194 |
- name: Test state=present for ipv6 (expected changed=true) |
| 195 |
amazon.aws.ec2_security_group: |
| 196 |
name: "{{ ec2_group_name }}-2" |
| 197 |
description: "{{ ec2_group_description }}-2" |
| 198 |
state: present |
| 199 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 200 |
rules: |
| 201 |
- proto: tcp |
| 202 |
from_port: 8182 |
| 203 |
to_port: 8182 |
| 204 |
cidr_ipv6: 64:ff9b::/96 |
| 205 |
register: result |
| 206 |
|
| 207 |
- name: Assert state=present (expected changed=true) |
| 208 |
ansible.builtin.assert: |
| 209 |
that: |
| 210 |
- result.changed |
| 211 |
- result.group_id.startswith("sg-") |
| 212 |
|
| 213 |
# ============================================================ |
| 214 |
- name: Test state=present for ipv6 (expected changed=false) (CHECK MODE) |
| 215 |
amazon.aws.ec2_security_group: |
| 216 |
name: "{{ ec2_group_name }}-2" |
| 217 |
description: "{{ ec2_group_description }}-2" |
| 218 |
state: present |
| 219 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 220 |
rules: |
| 221 |
- proto: tcp |
| 222 |
from_port: 8182 |
| 223 |
to_port: 8182 |
| 224 |
cidr_ipv6: 64:ff9b::/96 |
| 225 |
check_mode: true |
| 226 |
register: result |
| 227 |
|
| 228 |
- name: Assert nothing changed |
| 229 |
ansible.builtin.assert: |
| 230 |
that: |
| 231 |
- not result.changed |
| 232 |
|
| 233 |
# ============================================================ |
| 234 |
- name: Test state=present for ipv6 (expected changed=false) |
| 235 |
amazon.aws.ec2_security_group: |
| 236 |
name: "{{ ec2_group_name }}-2" |
| 237 |
description: "{{ ec2_group_description }}-2" |
| 238 |
state: present |
| 239 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 240 |
rules: |
| 241 |
- proto: tcp |
| 242 |
from_port: 8182 |
| 243 |
to_port: 8182 |
| 244 |
cidr_ipv6: 64:ff9b::/96 |
| 245 |
register: result |
| 246 |
|
| 247 |
- name: Assert nothing changed |
| 248 |
ansible.builtin.assert: |
| 249 |
that: |
| 250 |
- not result.changed |
| 251 |
|
| 252 |
# ============================================================ |
| 253 |
- name: Test rules_egress state=present for ipv6 (expected changed=true) (CHECK MODE) |
| 254 |
amazon.aws.ec2_security_group: |
| 255 |
name: "{{ ec2_group_name }}-2" |
| 256 |
description: "{{ ec2_group_description }}-2" |
| 257 |
state: present |
| 258 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 259 |
rules: |
| 260 |
- proto: tcp |
| 261 |
from_port: 8182 |
| 262 |
to_port: 8182 |
| 263 |
cidr_ipv6: 64:ff9b::/96 |
| 264 |
rules_egress: |
| 265 |
- proto: tcp |
| 266 |
from_port: 8181 |
| 267 |
to_port: 8181 |
| 268 |
cidr_ipv6: 64:ff9b::/96 |
| 269 |
check_mode: true |
| 270 |
diff: true |
| 271 |
register: result |
| 272 |
|
| 273 |
- name: Assert state=present (expected changed=true) |
| 274 |
ansible.builtin.assert: |
| 275 |
that: |
| 276 |
- result.changed |
| 277 |
- result.diff.0.before.ip_permissions == result.diff.0.after.ip_permissions |
| 278 |
- result.diff.0.before.ip_permissions_egress != result.diff.0.after.ip_permissions_egress |
| 279 |
|
| 280 |
# ============================================================ |
| 281 |
- name: Test rules_egress state=present for ipv6 (expected changed=true) |
| 282 |
amazon.aws.ec2_security_group: |
| 283 |
name: "{{ ec2_group_name }}-2" |
| 284 |
description: "{{ ec2_group_description }}-2" |
| 285 |
state: present |
| 286 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 287 |
rules: |
| 288 |
- proto: tcp |
| 289 |
from_port: 8182 |
| 290 |
to_port: 8182 |
| 291 |
cidr_ipv6: 64:ff9b::/96 |
| 292 |
rules_egress: |
| 293 |
- proto: tcp |
| 294 |
from_port: 8181 |
| 295 |
to_port: 8181 |
| 296 |
cidr_ipv6: 64:ff9b::/96 |
| 297 |
register: result |
| 298 |
|
| 299 |
- name: Assert state=present (expected changed=true) |
| 300 |
ansible.builtin.assert: |
| 301 |
that: |
| 302 |
- result.changed |
| 303 |
- result.group_id.startswith("sg-") |
| 304 |
|
| 305 |
# ============================================================ |
| 306 |
- name: Test state=absent (expected changed=true) (CHECK MODE) |
| 307 |
amazon.aws.ec2_security_group: |
| 308 |
name: "{{ ec2_group_name }}-2" |
| 309 |
description: "{{ ec2_group_description }}-2" |
| 310 |
state: absent |
| 311 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 312 |
check_mode: true |
| 313 |
diff: true |
| 314 |
register: result |
| 315 |
|
| 316 |
- name: Assert group was removed |
| 317 |
ansible.builtin.assert: |
| 318 |
that: |
| 319 |
- result.changed |
| 320 |
- not result.diff.0.after |
| 321 |
|
| 322 |
# ============================================================ |
| 323 |
- name: Test state=absent (expected changed=true) |
| 324 |
amazon.aws.ec2_security_group: |
| 325 |
name: "{{ ec2_group_name }}-2" |
| 326 |
description: "{{ ec2_group_description }}-2" |
| 327 |
state: absent |
| 328 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 329 |
register: result |
| 330 |
|
| 331 |
- name: Assert group was removed |
| 332 |
ansible.builtin.assert: |
| 333 |
that: |
| 334 |
- result.changed |
| 335 |
|
| 336 |
# ============================================================ |
| 337 |
- name: Test state=present for ipv4 (expected changed=true) (CHECK MODE) |
| 338 |
amazon.aws.ec2_security_group: |
| 339 |
name: "{{ ec2_group_name }}" |
| 340 |
description: "{{ ec2_group_description }}" |
| 341 |
rules: |
| 342 |
- proto: tcp |
| 343 |
from_port: 8182 |
| 344 |
to_port: 8182 |
| 345 |
cidr_ip: 10.1.1.1/32 |
| 346 |
check_mode: true |
| 347 |
register: result |
| 348 |
|
| 349 |
- name: Assert state=present (expected changed=true) |
| 350 |
ansible.builtin.assert: |
| 351 |
that: |
| 352 |
- result.changed |
| 353 |
|
| 354 |
# ============================================================ |
| 355 |
- name: Test state=present for ipv4 (expected changed=true) |
| 356 |
amazon.aws.ec2_security_group: |
| 357 |
name: "{{ ec2_group_name }}" |
| 358 |
description: "{{ ec2_group_description }}" |
| 359 |
rules: |
| 360 |
- proto: tcp |
| 361 |
from_port: 8182 |
| 362 |
to_port: 8182 |
| 363 |
cidr_ip: 10.1.1.1/32 |
| 364 |
register: result |
| 365 |
|
| 366 |
- name: Assert state=present (expected changed=true) |
| 367 |
ansible.builtin.assert: |
| 368 |
that: |
| 369 |
- result.changed |
| 370 |
- result.group_id.startswith("sg-") |
| 371 |
- result.ip_permissions|length == 1 |
| 372 |
- result.ip_permissions_egress|length == 1 |
| 373 |
|
| 374 |
# ============================================================ |
| 375 |
- name: Add same rule to the existing group (expected changed=false) (CHECK MODE) |
| 376 |
amazon.aws.ec2_security_group: |
| 377 |
name: "{{ ec2_group_name }}" |
| 378 |
description: "{{ ec2_group_description }}" |
| 379 |
state: present |
| 380 |
rules: |
| 381 |
- proto: tcp |
| 382 |
from_port: 8182 |
| 383 |
to_port: 8182 |
| 384 |
cidr_ip: 10.1.1.1/32 |
| 385 |
check_mode: true |
| 386 |
diff: true |
| 387 |
register: check_result |
| 388 |
|
| 389 |
- ansible.builtin.assert: |
| 390 |
that: |
| 391 |
- not check_result.changed |
| 392 |
- check_result.diff.0.before.ip_permissions.0 == check_result.diff.0.after.ip_permissions.0 |
| 393 |
|
| 394 |
# ============================================================ |
| 395 |
- name: Add same rule to the existing group (expected changed=false) |
| 396 |
amazon.aws.ec2_security_group: |
| 397 |
name: "{{ ec2_group_name }}" |
| 398 |
description: "{{ ec2_group_description }}" |
| 399 |
state: present |
| 400 |
rules: |
| 401 |
- proto: tcp |
| 402 |
from_port: 8182 |
| 403 |
to_port: 8182 |
| 404 |
cidr_ip: 10.1.1.1/32 |
| 405 |
register: result |
| 406 |
|
| 407 |
- name: Assert state=present (expected changed=false) |
| 408 |
ansible.builtin.assert: |
| 409 |
that: |
| 410 |
- not result.changed |
| 411 |
- result.group_id.startswith("sg-") |
| 412 |
|
| 413 |
- name: Assert state=present (expected changed=false) |
| 414 |
ansible.builtin.assert: |
| 415 |
that: |
| 416 |
- not check_result.changed |
| 417 |
|
| 418 |
# ============================================================ |
| 419 |
- name: Add a rule that auto creates another security group (CHECK MODE) |
| 420 |
amazon.aws.ec2_security_group: |
| 421 |
name: "{{ ec2_group_name }}" |
| 422 |
description: "{{ ec2_group_description }}" |
| 423 |
state: present |
| 424 |
purge_rules: false |
| 425 |
rules: |
| 426 |
- proto: tcp |
| 427 |
group_name: "{{ resource_prefix }} - Another security group" |
| 428 |
group_desc: Another security group |
| 429 |
ports: 7171 |
| 430 |
check_mode: true |
| 431 |
register: result |
| 432 |
|
| 433 |
- name: Check that there are now two rules |
| 434 |
ansible.builtin.assert: |
| 435 |
that: |
| 436 |
- result.changed |
| 437 |
|
| 438 |
# ============================================================ |
| 439 |
- name: Add a rule that auto creates another security group |
| 440 |
amazon.aws.ec2_security_group: |
| 441 |
name: "{{ ec2_group_name }}" |
| 442 |
description: "{{ ec2_group_description }}" |
| 443 |
state: present |
| 444 |
purge_rules: false |
| 445 |
rules: |
| 446 |
- proto: tcp |
| 447 |
group_name: "{{ resource_prefix }} - Another security group" |
| 448 |
group_desc: Another security group |
| 449 |
ports: 7171 |
| 450 |
register: result |
| 451 |
|
| 452 |
- name: Check that there are now two rules |
| 453 |
ansible.builtin.assert: |
| 454 |
that: |
| 455 |
- result.changed |
| 456 |
- result.warning is not defined |
| 457 |
- result.ip_permissions|length == 2 |
| 458 |
- (result.ip_permissions[0].user_id_group_pairs | length > 0) or (result.ip_permissions[1].user_id_group_pairs | length > 0) |
| 459 |
- result.ip_permissions_egress[0].ip_protocol == "-1" |
| 460 |
|
| 461 |
# ============================================================ |
| 462 |
- name: Test ip rules convert port numbers from string to int (expected changed=true) (CHECK MODE) |
| 463 |
amazon.aws.ec2_security_group: |
| 464 |
name: "{{ ec2_group_name }}" |
| 465 |
description: "{{ ec2_group_description }}" |
| 466 |
state: present |
| 467 |
rules: |
| 468 |
- proto: tcp |
| 469 |
from_port: "8183" |
| 470 |
to_port: "8183" |
| 471 |
cidr_ip: 10.1.1.1/32 |
| 472 |
rules_egress: |
| 473 |
- proto: tcp |
| 474 |
from_port: "8184" |
| 475 |
to_port: "8184" |
| 476 |
cidr_ip: 10.1.1.1/32 |
| 477 |
check_mode: true |
| 478 |
register: result |
| 479 |
|
| 480 |
- name: Assert state=present (expected changed=true) |
| 481 |
ansible.builtin.assert: |
| 482 |
that: |
| 483 |
- result.changed |
| 484 |
|
| 485 |
# ============================================================ |
| 486 |
- name: Test ip rules convert port numbers from string to int (expected changed=true) |
| 487 |
amazon.aws.ec2_security_group: |
| 488 |
name: "{{ ec2_group_name }}" |
| 489 |
description: "{{ ec2_group_description }}" |
| 490 |
state: present |
| 491 |
rules: |
| 492 |
- proto: tcp |
| 493 |
from_port: "8183" |
| 494 |
to_port: "8183" |
| 495 |
cidr_ip: 10.1.1.1/32 |
| 496 |
rules_egress: |
| 497 |
- proto: tcp |
| 498 |
from_port: "8184" |
| 499 |
to_port: "8184" |
| 500 |
cidr_ip: 10.1.1.1/32 |
| 501 |
register: result |
| 502 |
|
| 503 |
- name: Assert state=present (expected changed=true) |
| 504 |
ansible.builtin.assert: |
| 505 |
that: |
| 506 |
- result.changed |
| 507 |
- result.group_id.startswith("sg-") |
| 508 |
- result.ip_permissions|length == 1 |
| 509 |
- result.ip_permissions_egress[0].ip_protocol == "tcp" |
| 510 |
|
| 511 |
# ============================================================ |
| 512 |
- name: Test group rules convert port numbers from string to int (expected changed=true) (CHECK MODE) |
| 513 |
amazon.aws.ec2_security_group: |
| 514 |
name: "{{ ec2_group_name }}" |
| 515 |
description: "{{ ec2_group_description }}" |
| 516 |
state: present |
| 517 |
rules: |
| 518 |
- proto: tcp |
| 519 |
from_port: "8185" |
| 520 |
to_port: "8185" |
| 521 |
group_id: "{{ result.group_id }}" |
| 522 |
rules_egress: |
| 523 |
- proto: tcp |
| 524 |
from_port: "8186" |
| 525 |
to_port: "8186" |
| 526 |
group_id: "{{ result.group_id }}" |
| 527 |
check_mode: true |
| 528 |
register: result |
| 529 |
|
| 530 |
- name: Assert state=present (expected changed=true) |
| 531 |
ansible.builtin.assert: |
| 532 |
that: |
| 533 |
- result.changed |
| 534 |
|
| 535 |
# ============================================================ |
| 536 |
- name: Test group rules convert port numbers from string to int (expected changed=true) |
| 537 |
amazon.aws.ec2_security_group: |
| 538 |
name: "{{ ec2_group_name }}" |
| 539 |
description: "{{ ec2_group_description }}" |
| 540 |
state: present |
| 541 |
rules: |
| 542 |
- proto: tcp |
| 543 |
from_port: "8185" |
| 544 |
to_port: "8185" |
| 545 |
group_id: "{{ result.group_id }}" |
| 546 |
rules_egress: |
| 547 |
- proto: tcp |
| 548 |
from_port: "8186" |
| 549 |
to_port: "8186" |
| 550 |
group_id: "{{ result.group_id }}" |
| 551 |
register: result |
| 552 |
|
| 553 |
- name: Assert state=present (expected changed=true) |
| 554 |
ansible.builtin.assert: |
| 555 |
that: |
| 556 |
- result.changed |
| 557 |
- result.group_id.startswith("sg-") |
| 558 |
- result.warning is not defined |
| 559 |
|
| 560 |
# ============================================================ |
| 561 |
- name: Test adding a range of ports and ports given as strings (expected changed=true) (CHECK MODE) |
| 562 |
amazon.aws.ec2_security_group: |
| 563 |
name: "{{ ec2_group_name }}" |
| 564 |
description: "{{ ec2_group_description }}" |
| 565 |
state: present |
| 566 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 567 |
purge_rules: false |
| 568 |
rules: |
| 569 |
- proto: tcp |
| 570 |
ports: |
| 571 |
- 8183-8190 |
| 572 |
- "8192" |
| 573 |
cidr_ip: 10.1.1.1/32 |
| 574 |
check_mode: true |
| 575 |
register: result |
| 576 |
|
| 577 |
- name: Assert state=present (expected changed=true) |
| 578 |
ansible.builtin.assert: |
| 579 |
that: |
| 580 |
- result.changed |
| 581 |
|
| 582 |
# ============================================================ |
| 583 |
- name: Test adding a range of ports and ports given as strings (expected changed=true) |
| 584 |
amazon.aws.ec2_security_group: |
| 585 |
name: "{{ ec2_group_name }}" |
| 586 |
description: "{{ ec2_group_description }}" |
| 587 |
state: present |
| 588 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 589 |
purge_rules: false |
| 590 |
rules: |
| 591 |
- proto: tcp |
| 592 |
ports: |
| 593 |
- 8183-8190 |
| 594 |
- "8192" |
| 595 |
cidr_ip: 10.1.1.1/32 |
| 596 |
register: result |
| 597 |
|
| 598 |
- name: Assert state=present (expected changed=true) |
| 599 |
ansible.builtin.assert: |
| 600 |
that: |
| 601 |
- result.changed |
| 602 |
- result.group_id.startswith("sg-") |
| 603 |
|
| 604 |
# ============================================================ |
| 605 |
- name: Test adding a rule with a IPv4 CIDR with host bits set (expected changed=true) (CHECK MODE) |
| 606 |
amazon.aws.ec2_security_group: |
| 607 |
name: "{{ ec2_group_name }}" |
| 608 |
description: "{{ ec2_group_description }}" |
| 609 |
state: present |
| 610 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 611 |
purge_rules: false |
| 612 |
rules: |
| 613 |
- proto: tcp |
| 614 |
ports: |
| 615 |
- 8195 |
| 616 |
cidr_ip: 10.0.0.1/8 |
| 617 |
check_mode: true |
| 618 |
register: result |
| 619 |
|
| 620 |
- name: Assert state=present (expected changed=true) |
| 621 |
ansible.builtin.assert: |
| 622 |
that: |
| 623 |
- result.changed |
| 624 |
|
| 625 |
# ============================================================ |
| 626 |
- name: Test adding a rule with a IPv4 CIDR with host bits set (expected changed=true) |
| 627 |
amazon.aws.ec2_security_group: |
| 628 |
name: "{{ ec2_group_name }}" |
| 629 |
description: "{{ ec2_group_description }}" |
| 630 |
state: present |
| 631 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 632 |
purge_rules: false |
| 633 |
rules: |
| 634 |
- proto: tcp |
| 635 |
ports: |
| 636 |
- 8195 |
| 637 |
cidr_ip: 10.0.0.1/8 |
| 638 |
register: result |
| 639 |
|
| 640 |
- name: Assert state=present (expected changed=true) |
| 641 |
ansible.builtin.assert: |
| 642 |
that: |
| 643 |
- result.changed |
| 644 |
- result.group_id.startswith("sg-") |
| 645 |
|
| 646 |
# ============================================================ |
| 647 |
- name: Test adding the same rule with a IPv4 CIDR with host bits set (expected changed=false) (CHECK MODE) |
| 648 |
amazon.aws.ec2_security_group: |
| 649 |
name: "{{ ec2_group_name }}" |
| 650 |
description: "{{ ec2_group_description }}" |
| 651 |
state: present |
| 652 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 653 |
purge_rules: false |
| 654 |
rules: |
| 655 |
- proto: tcp |
| 656 |
ports: |
| 657 |
- 8195 |
| 658 |
cidr_ip: 10.0.0.1/8 |
| 659 |
check_mode: true |
| 660 |
register: check_result |
| 661 |
|
| 662 |
# ============================================================ |
| 663 |
- name: Test adding the same rule with a IPv4 CIDR with host bits set (expected changed=false and a warning) |
| 664 |
amazon.aws.ec2_security_group: |
| 665 |
name: "{{ ec2_group_name }}" |
| 666 |
description: "{{ ec2_group_description }}" |
| 667 |
state: present |
| 668 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 669 |
purge_rules: false |
| 670 |
rules: |
| 671 |
- proto: tcp |
| 672 |
ports: |
| 673 |
- 8195 |
| 674 |
cidr_ip: 10.0.0.1/8 |
| 675 |
register: result |
| 676 |
|
| 677 |
- name: Assert state=present (expected changed=false and a warning) |
| 678 |
ansible.builtin.assert: |
| 679 |
that: |
| 680 |
- not check_result.changed |
| 681 |
|
| 682 |
- name: Assert state=present (expected changed=false and a warning) |
| 683 |
ansible.builtin.assert: |
| 684 |
that: |
| 685 |
# No way to assert for warnings? |
| 686 |
- not result.changed |
| 687 |
- result.group_id.startswith("sg-") |
| 688 |
|
| 689 |
# ============================================================ |
| 690 |
- name: Test using the default VPC |
| 691 |
when: default_vpc != "" |
| 692 |
|
| 693 |
# ============================================================ |
| 694 |
block: |
| 695 |
- name: Test adding a rule with a IPv6 CIDR with host bits set (expected changed=true) (CHECK MODE) |
| 696 |
amazon.aws.ec2_security_group: |
| 697 |
name: "{{ ec2_group_name }}" |
| 698 |
description: "{{ ec2_group_description }}" |
| 699 |
state: present |
| 700 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 701 |
purge_rules: false |
| 702 |
rules: |
| 703 |
- proto: tcp |
| 704 |
ports: |
| 705 |
- 8196 |
| 706 |
cidr_ipv6: 2001:db00::1/24 |
| 707 |
check_mode: true |
| 708 |
register: result |
| 709 |
|
| 710 |
- name: Assert state=present (expected changed=true) |
| 711 |
ansible.builtin.assert: |
| 712 |
that: |
| 713 |
- result.changed |
| 714 |
|
| 715 |
# ============================================================ |
| 716 |
- name: Test adding a rule with a IPv6 CIDR with host bits set (expected changed=true) |
| 717 |
amazon.aws.ec2_security_group: |
| 718 |
name: "{{ ec2_group_name }}" |
| 719 |
description: "{{ ec2_group_description }}" |
| 720 |
state: present |
| 721 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 722 |
purge_rules: false |
| 723 |
rules: |
| 724 |
- proto: tcp |
| 725 |
ports: |
| 726 |
- 8196 |
| 727 |
cidr_ipv6: 2001:db00::1/24 |
| 728 |
register: result |
| 729 |
|
| 730 |
- name: Assert state=present (expected changed=true) |
| 731 |
ansible.builtin.assert: |
| 732 |
that: |
| 733 |
- result.changed |
| 734 |
- result.group_id.startswith("sg-") |
| 735 |
|
| 736 |
# ============================================================ |
| 737 |
|
| 738 |
- name: Test adding a rule again with a IPv6 CIDR with host bits set (expected changed=false and a warning) |
| 739 |
amazon.aws.ec2_security_group: |
| 740 |
name: "{{ ec2_group_name }}" |
| 741 |
description: "{{ ec2_group_description }}" |
| 742 |
state: present |
| 743 |
# set purge_rules to false so we don't get a false positive from previously added rules |
| 744 |
purge_rules: false |
| 745 |
rules: |
| 746 |
- proto: tcp |
| 747 |
ports: |
| 748 |
- 8196 |
| 749 |
cidr_ipv6: 2001:db00::1/24 |
| 750 |
register: result |
| 751 |
|
| 752 |
- name: Assert state=present (expected changed=false and a warning) |
| 753 |
ansible.builtin.assert: |
| 754 |
that: |
| 755 |
# No way to assert for warnings? |
| 756 |
- not result.changed |
| 757 |
- result.group_id.startswith("sg-") |
| 758 |
|
| 759 |
- name: Test state=absent (expected changed=true) (CHECK MODE) |
| 760 |
amazon.aws.ec2_security_group: |
| 761 |
name: "{{ ec2_group_name }}" |
| 762 |
state: absent |
| 763 |
check_mode: true |
| 764 |
register: result |
| 765 |
|
| 766 |
- name: Assert state=absent (expected changed=true) |
| 767 |
ansible.builtin.assert: |
| 768 |
that: |
| 769 |
- result.changed |
| 770 |
|
| 771 |
# ============================================================ |
| 772 |
- name: Test state=absent (expected changed=true) |
| 773 |
amazon.aws.ec2_security_group: |
| 774 |
name: "{{ ec2_group_name }}" |
| 775 |
state: absent |
| 776 |
register: result |
| 777 |
|
| 778 |
- name: Assert state=absent (expected changed=true) |
| 779 |
ansible.builtin.assert: |
| 780 |
that: |
| 781 |
- result.changed |
| 782 |
- not result.group_id |
| 783 |
|
| 784 |
# ============================================================ |
| 785 |
- name: Create security group in the VPC (CHECK MODE) |
| 786 |
amazon.aws.ec2_security_group: |
| 787 |
name: "{{ ec2_group_name }}" |
| 788 |
description: "{{ ec2_group_description }}" |
| 789 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 790 |
state: present |
| 791 |
rules: |
| 792 |
- proto: tcp |
| 793 |
from_port: 8182 |
| 794 |
to_port: 8182 |
| 795 |
cidr_ip: 10.1.1.1/32 |
| 796 |
check_mode: true |
| 797 |
register: result |
| 798 |
|
| 799 |
- name: Assert state=present (expected changed=true) |
| 800 |
ansible.builtin.assert: |
| 801 |
that: |
| 802 |
- result.changed |
| 803 |
|
| 804 |
# ============================================================ |
| 805 |
- name: Create security group in the VPC |
| 806 |
amazon.aws.ec2_security_group: |
| 807 |
name: "{{ ec2_group_name }}" |
| 808 |
description: "{{ ec2_group_description }}" |
| 809 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 810 |
state: present |
| 811 |
rules: |
| 812 |
- proto: tcp |
| 813 |
from_port: 8182 |
| 814 |
to_port: 8182 |
| 815 |
cidr_ip: 10.1.1.1/32 |
| 816 |
register: result |
| 817 |
|
| 818 |
- name: Assert state=present (expected changed=true) |
| 819 |
ansible.builtin.assert: |
| 820 |
that: |
| 821 |
- result.changed |
| 822 |
- result.vpc_id == vpc_result.vpc.id |
| 823 |
- result.group_id.startswith("sg-") |
| 824 |
|
| 825 |
# ============================================================ |
| 826 |
- name: Test adding tags (expected changed=true) (CHECK MODE) |
| 827 |
amazon.aws.ec2_security_group: |
| 828 |
name: "{{ ec2_group_name }}" |
| 829 |
description: "{{ ec2_group_description }}" |
| 830 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 831 |
state: present |
| 832 |
rules: |
| 833 |
- proto: tcp |
| 834 |
from_port: 8182 |
| 835 |
to_port: 8182 |
| 836 |
cidr_ip: 10.1.1.1/32 |
| 837 |
tags: |
| 838 |
tag1: test1 |
| 839 |
tag2: test2 |
| 840 |
check_mode: true |
| 841 |
diff: true |
| 842 |
register: result |
| 843 |
|
| 844 |
- name: Assert that tags were added (expected changed=true) |
| 845 |
ansible.builtin.assert: |
| 846 |
that: |
| 847 |
- result.changed |
| 848 |
- not result.diff.0.before.tags |
| 849 |
- result.diff.0.after.tags.tag1 == "test1" |
| 850 |
- result.diff.0.after.tags.tag2 == "test2" |
| 851 |
|
| 852 |
# ============================================================ |
| 853 |
- name: Test adding tags (expected changed=true) |
| 854 |
amazon.aws.ec2_security_group: |
| 855 |
name: "{{ ec2_group_name }}" |
| 856 |
description: "{{ ec2_group_description }}" |
| 857 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 858 |
state: present |
| 859 |
rules: |
| 860 |
- proto: tcp |
| 861 |
from_port: 8182 |
| 862 |
to_port: 8182 |
| 863 |
cidr_ip: 10.1.1.1/32 |
| 864 |
tags: |
| 865 |
tag1: test1 |
| 866 |
tag2: test2 |
| 867 |
register: result |
| 868 |
|
| 869 |
- name: Assert that tags were added (expected changed=true) |
| 870 |
ansible.builtin.assert: |
| 871 |
that: |
| 872 |
- result.changed |
| 873 |
- 'result.tags == {"tag1": "test1", "tag2": "test2"}' |
| 874 |
|
| 875 |
# ============================================================ |
| 876 |
- name: Test that tags are present (expected changed=False) (CHECK MODE) |
| 877 |
amazon.aws.ec2_security_group: |
| 878 |
name: "{{ ec2_group_name }}" |
| 879 |
description: "{{ ec2_group_description }}" |
| 880 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 881 |
state: present |
| 882 |
purge_rules_egress: false |
| 883 |
rules: |
| 884 |
- proto: tcp |
| 885 |
from_port: 8182 |
| 886 |
to_port: 8182 |
| 887 |
cidr_ip: 10.1.1.1/32 |
| 888 |
tags: |
| 889 |
tag1: test1 |
| 890 |
tag2: test2 |
| 891 |
check_mode: true |
| 892 |
register: result |
| 893 |
|
| 894 |
- name: Assert that tags were not changed (expected changed=False) |
| 895 |
ansible.builtin.assert: |
| 896 |
that: |
| 897 |
- not result.changed |
| 898 |
|
| 899 |
# ============================================================ |
| 900 |
- name: Test that tags are present (expected changed=False) |
| 901 |
amazon.aws.ec2_security_group: |
| 902 |
name: "{{ ec2_group_name }}" |
| 903 |
description: "{{ ec2_group_description }}" |
| 904 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 905 |
state: present |
| 906 |
purge_rules_egress: false |
| 907 |
rules: |
| 908 |
- proto: tcp |
| 909 |
from_port: 8182 |
| 910 |
to_port: 8182 |
| 911 |
cidr_ip: 10.1.1.1/32 |
| 912 |
tags: |
| 913 |
tag1: test1 |
| 914 |
tag2: test2 |
| 915 |
register: result |
| 916 |
|
| 917 |
- name: Assert that tags were not changed (expected changed=False) |
| 918 |
ansible.builtin.assert: |
| 919 |
that: |
| 920 |
- not result.changed |
| 921 |
- 'result.tags == {"tag1": "test1", "tag2": "test2"}' |
| 922 |
|
| 923 |
# ============================================================ |
| 924 |
- name: Test purging tags (expected changed=True) (CHECK MODE) |
| 925 |
amazon.aws.ec2_security_group: |
| 926 |
name: "{{ ec2_group_name }}" |
| 927 |
description: "{{ ec2_group_description }}" |
| 928 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 929 |
state: present |
| 930 |
rules: |
| 931 |
- proto: tcp |
| 932 |
from_port: 8182 |
| 933 |
to_port: 8182 |
| 934 |
cidr_ip: 10.1.1.1/32 |
| 935 |
tags: |
| 936 |
tag1: test1 |
| 937 |
check_mode: true |
| 938 |
register: result |
| 939 |
|
| 940 |
- name: Assert that tag2 was removed (expected changed=true) |
| 941 |
ansible.builtin.assert: |
| 942 |
that: |
| 943 |
- result.changed |
| 944 |
|
| 945 |
# ============================================================ |
| 946 |
- name: Test purging tags (expected changed=True) |
| 947 |
amazon.aws.ec2_security_group: |
| 948 |
name: "{{ ec2_group_name }}" |
| 949 |
description: "{{ ec2_group_description }}" |
| 950 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 951 |
state: present |
| 952 |
rules: |
| 953 |
- proto: tcp |
| 954 |
from_port: 8182 |
| 955 |
to_port: 8182 |
| 956 |
cidr_ip: 10.1.1.1/32 |
| 957 |
tags: |
| 958 |
tag1: test1 |
| 959 |
register: result |
| 960 |
|
| 961 |
- name: Assert that tag2 was removed (expected changed=true) |
| 962 |
ansible.builtin.assert: |
| 963 |
that: |
| 964 |
- result.changed |
| 965 |
- 'result.tags == {"tag1": "test1"}' |
| 966 |
|
| 967 |
# ============================================================ |
| 968 |
|
| 969 |
- name: Assert that tags are left as-is if not specified (expected changed=False) |
| 970 |
amazon.aws.ec2_security_group: |
| 971 |
name: "{{ ec2_group_name }}" |
| 972 |
description: "{{ ec2_group_description }}" |
| 973 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 974 |
state: present |
| 975 |
rules: |
| 976 |
- proto: tcp |
| 977 |
from_port: 8182 |
| 978 |
to_port: 8182 |
| 979 |
cidr_ip: 10.1.1.1/32 |
| 980 |
register: result |
| 981 |
|
| 982 |
- name: Assert that the tags stayed the same (expected changed=false) |
| 983 |
ansible.builtin.assert: |
| 984 |
that: |
| 985 |
- not result.changed |
| 986 |
- 'result.tags == {"tag1": "test1"}' |
| 987 |
|
| 988 |
# ============================================================ |
| 989 |
|
| 990 |
- name: Test purging all tags (expected changed=True) |
| 991 |
amazon.aws.ec2_security_group: |
| 992 |
name: "{{ ec2_group_name }}" |
| 993 |
description: "{{ ec2_group_description }}" |
| 994 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 995 |
state: present |
| 996 |
rules: |
| 997 |
- proto: tcp |
| 998 |
from_port: 8182 |
| 999 |
to_port: 8182 |
| 1000 |
cidr_ip: 10.1.1.1/32 |
| 1001 |
tags: {} |
| 1002 |
register: result |
| 1003 |
|
| 1004 |
- name: Assert that tag1 was removed (expected changed=true) |
| 1005 |
ansible.builtin.assert: |
| 1006 |
that: |
| 1007 |
- result.changed |
| 1008 |
- not result.tags |
| 1009 |
|
| 1010 |
# ============================================================ |
| 1011 |
- name: Test adding a rule and egress rule descriptions (expected changed=true) (CHECK MODE) |
| 1012 |
amazon.aws.ec2_security_group: |
| 1013 |
name: "{{ ec2_group_name }}" |
| 1014 |
description: "{{ ec2_group_description }}" |
| 1015 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1016 |
# purge the other rules so assertions work for the subsequent tests for rule descriptions |
| 1017 |
purge_rules_egress: true |
| 1018 |
purge_rules: true |
| 1019 |
state: present |
| 1020 |
rules: |
| 1021 |
- proto: tcp |
| 1022 |
ports: |
| 1023 |
- 8281 |
| 1024 |
cidr_ipv6: 1001:d00::/24 |
| 1025 |
rule_desc: ipv6 rule desc 1 |
| 1026 |
rules_egress: |
| 1027 |
- proto: tcp |
| 1028 |
ports: |
| 1029 |
- 8282 |
| 1030 |
cidr_ip: 10.2.2.2/32 |
| 1031 |
rule_desc: egress rule desc 1 |
| 1032 |
check_mode: true |
| 1033 |
register: result |
| 1034 |
|
| 1035 |
- name: Assert that rule descriptions are created (expected changed=true) |
| 1036 |
ansible.builtin.assert: |
| 1037 |
that: |
| 1038 |
- result.changed |
| 1039 |
|
| 1040 |
# ========================================================================================= |
| 1041 |
- name: Add rules without descriptions ready for adding descriptions to existing rules |
| 1042 |
amazon.aws.ec2_security_group: |
| 1043 |
name: "{{ ec2_group_name }}" |
| 1044 |
description: "{{ ec2_group_description }}" |
| 1045 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1046 |
# purge the other rules so assertions work for the subsequent tests for rule descriptions |
| 1047 |
purge_rules_egress: true |
| 1048 |
purge_rules: true |
| 1049 |
state: present |
| 1050 |
rules: |
| 1051 |
- proto: tcp |
| 1052 |
ports: |
| 1053 |
- 8281 |
| 1054 |
cidr_ipv6: 1001:d00::/24 |
| 1055 |
rules_egress: |
| 1056 |
- proto: tcp |
| 1057 |
ports: |
| 1058 |
- 8282 |
| 1059 |
cidr_ip: 10.2.2.2/32 |
| 1060 |
register: result |
| 1061 |
|
| 1062 |
# ============================================================ |
| 1063 |
- name: Test adding a rule and egress rule descriptions (expected changed=true) |
| 1064 |
amazon.aws.ec2_security_group: |
| 1065 |
name: "{{ ec2_group_name }}" |
| 1066 |
description: "{{ ec2_group_description }}" |
| 1067 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1068 |
# purge the other rules so assertions work for the subsequent tests for rule descriptions |
| 1069 |
purge_rules_egress: true |
| 1070 |
purge_rules: true |
| 1071 |
state: present |
| 1072 |
rules: |
| 1073 |
- proto: tcp |
| 1074 |
ports: |
| 1075 |
- 8281 |
| 1076 |
cidr_ipv6: 1001:d00::/24 |
| 1077 |
rule_desc: ipv6 rule desc 1 |
| 1078 |
rules_egress: |
| 1079 |
- proto: tcp |
| 1080 |
ports: |
| 1081 |
- 8282 |
| 1082 |
cidr_ip: 10.2.2.2/32 |
| 1083 |
rule_desc: egress rule desc 1 |
| 1084 |
register: result |
| 1085 |
|
| 1086 |
- name: Assert that rule descriptions are created (expected changed=true) |
| 1087 |
ansible.builtin.assert: |
| 1088 |
that: |
| 1089 |
- result.changed |
| 1090 |
- result.ip_permissions[0].ipv6_ranges[0].description == "ipv6 rule desc 1" |
| 1091 |
- result.ip_permissions_egress[0].ip_ranges[0].description == "egress rule desc 1" |
| 1092 |
|
| 1093 |
# ============================================================ |
| 1094 |
- name: Test modifying rule and egress rule descriptions (expected changed=true) (CHECK MODE) |
| 1095 |
amazon.aws.ec2_security_group: |
| 1096 |
name: "{{ ec2_group_name }}" |
| 1097 |
description: "{{ ec2_group_description }}" |
| 1098 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1099 |
purge_rules_egress: false |
| 1100 |
purge_rules: false |
| 1101 |
state: present |
| 1102 |
rules: |
| 1103 |
- proto: tcp |
| 1104 |
ports: |
| 1105 |
- 8281 |
| 1106 |
cidr_ipv6: 1001:d00::/24 |
| 1107 |
rule_desc: ipv6 rule desc 2 |
| 1108 |
rules_egress: |
| 1109 |
- proto: tcp |
| 1110 |
ports: |
| 1111 |
- 8282 |
| 1112 |
cidr_ip: 10.2.2.2/32 |
| 1113 |
rule_desc: egress rule desc 2 |
| 1114 |
check_mode: true |
| 1115 |
register: result |
| 1116 |
|
| 1117 |
- name: Assert that rule descriptions were modified (expected changed=true) |
| 1118 |
ansible.builtin.assert: |
| 1119 |
that: |
| 1120 |
- result.ip_permissions | length > 0 |
| 1121 |
- result.changed |
| 1122 |
|
| 1123 |
# ============================================================ |
| 1124 |
- name: Test modifying rule and egress rule descriptions (expected changed=true) |
| 1125 |
amazon.aws.ec2_security_group: |
| 1126 |
name: "{{ ec2_group_name }}" |
| 1127 |
description: "{{ ec2_group_description }}" |
| 1128 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1129 |
purge_rules_egress: false |
| 1130 |
purge_rules: false |
| 1131 |
state: present |
| 1132 |
rules: |
| 1133 |
- proto: tcp |
| 1134 |
ports: |
| 1135 |
- 8281 |
| 1136 |
cidr_ipv6: 1001:d00::/24 |
| 1137 |
rule_desc: ipv6 rule desc 2 |
| 1138 |
rules_egress: |
| 1139 |
- proto: tcp |
| 1140 |
ports: |
| 1141 |
- 8282 |
| 1142 |
cidr_ip: 10.2.2.2/32 |
| 1143 |
rule_desc: egress rule desc 2 |
| 1144 |
register: result |
| 1145 |
|
| 1146 |
- name: Assert that rule descriptions were modified (expected changed=true) |
| 1147 |
ansible.builtin.assert: |
| 1148 |
that: |
| 1149 |
- result.changed |
| 1150 |
- result.ip_permissions[0].ipv6_ranges[0].description == "ipv6 rule desc 2" |
| 1151 |
- result.ip_permissions_egress[0].ip_ranges[0].description == "egress rule desc 2" |
| 1152 |
|
| 1153 |
# ============================================================ |
| 1154 |
|
| 1155 |
- name: Test creating rule in default vpc with egress rule (expected changed=true) |
| 1156 |
amazon.aws.ec2_security_group: |
| 1157 |
name: "{{ ec2_group_name }}-default-vpc" |
| 1158 |
description: "{{ ec2_group_description }} default VPC" |
| 1159 |
purge_rules_egress: true |
| 1160 |
state: present |
| 1161 |
rules: |
| 1162 |
- proto: tcp |
| 1163 |
ports: |
| 1164 |
- 8281 |
| 1165 |
cidr_ip: 10.1.1.1/24 |
| 1166 |
rule_desc: ipv4 rule desc |
| 1167 |
rules_egress: |
| 1168 |
- proto: tcp |
| 1169 |
ports: |
| 1170 |
- 8282 |
| 1171 |
cidr_ip: 10.2.2.2/32 |
| 1172 |
rule_desc: egress rule desc 2 |
| 1173 |
register: result |
| 1174 |
|
| 1175 |
- name: Assert that rule descriptions were modified (expected changed=true) |
| 1176 |
ansible.builtin.assert: |
| 1177 |
that: |
| 1178 |
- result.changed |
| 1179 |
- result.ip_permissions_egress|length == 1 |
| 1180 |
|
| 1181 |
# ============================================================ |
| 1182 |
- name: Test that keeping the same rule descriptions (expected changed=false) (CHECK MODE) |
| 1183 |
amazon.aws.ec2_security_group: |
| 1184 |
name: "{{ ec2_group_name }}" |
| 1185 |
description: "{{ ec2_group_description }}" |
| 1186 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1187 |
purge_rules_egress: false |
| 1188 |
purge_rules: false |
| 1189 |
state: present |
| 1190 |
rules: |
| 1191 |
- proto: tcp |
| 1192 |
ports: |
| 1193 |
- 8281 |
| 1194 |
cidr_ipv6: 1001:d00::/24 |
| 1195 |
rule_desc: ipv6 rule desc 2 |
| 1196 |
rules_egress: |
| 1197 |
- proto: tcp |
| 1198 |
ports: |
| 1199 |
- 8282 |
| 1200 |
cidr_ip: 10.2.2.2/32 |
| 1201 |
rule_desc: egress rule desc 2 |
| 1202 |
check_mode: true |
| 1203 |
register: result |
| 1204 |
|
| 1205 |
- name: Assert that rule descriptions stayed the same (expected changed=false) |
| 1206 |
ansible.builtin.assert: |
| 1207 |
that: |
| 1208 |
- not result.changed |
| 1209 |
|
| 1210 |
# ============================================================ |
| 1211 |
- name: Test that keeping the same rule descriptions (expected changed=false) |
| 1212 |
amazon.aws.ec2_security_group: |
| 1213 |
name: "{{ ec2_group_name }}" |
| 1214 |
description: "{{ ec2_group_description }}" |
| 1215 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1216 |
purge_rules_egress: false |
| 1217 |
purge_rules: false |
| 1218 |
state: present |
| 1219 |
rules: |
| 1220 |
- proto: tcp |
| 1221 |
ports: |
| 1222 |
- 8281 |
| 1223 |
cidr_ipv6: 1001:d00::/24 |
| 1224 |
rule_desc: ipv6 rule desc 2 |
| 1225 |
rules_egress: |
| 1226 |
- proto: tcp |
| 1227 |
ports: |
| 1228 |
- 8282 |
| 1229 |
cidr_ip: 10.2.2.2/32 |
| 1230 |
rule_desc: egress rule desc 2 |
| 1231 |
register: result |
| 1232 |
|
| 1233 |
- name: Assert that rule descriptions stayed the same (expected changed=false) |
| 1234 |
ansible.builtin.assert: |
| 1235 |
that: |
| 1236 |
- not result.changed |
| 1237 |
- result.ip_permissions[0].ipv6_ranges[0].description == "ipv6 rule desc 2" |
| 1238 |
- result.ip_permissions_egress[0].ip_ranges[0].description == "egress rule desc 2" |
| 1239 |
|
| 1240 |
# ============================================================ |
| 1241 |
- name: Test removing rule descriptions (expected changed=true) (CHECK MODE) |
| 1242 |
amazon.aws.ec2_security_group: |
| 1243 |
name: "{{ ec2_group_name }}" |
| 1244 |
description: "{{ ec2_group_description }}" |
| 1245 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1246 |
purge_rules_egress: false |
| 1247 |
purge_rules: false |
| 1248 |
state: present |
| 1249 |
rules: |
| 1250 |
- proto: tcp |
| 1251 |
ports: |
| 1252 |
- 8281 |
| 1253 |
cidr_ipv6: 1001:d00::/24 |
| 1254 |
rule_desc: |
| 1255 |
rules_egress: |
| 1256 |
- proto: tcp |
| 1257 |
ports: |
| 1258 |
- 8282 |
| 1259 |
cidr_ip: 10.2.2.2/32 |
| 1260 |
rule_desc: |
| 1261 |
check_mode: true |
| 1262 |
register: result |
| 1263 |
|
| 1264 |
- name: Assert that rule descriptions were removed (expected changed=true) |
| 1265 |
ansible.builtin.assert: |
| 1266 |
that: |
| 1267 |
- result.changed |
| 1268 |
|
| 1269 |
# ============================================================ |
| 1270 |
- name: Test removing rule descriptions (expected changed=true) |
| 1271 |
amazon.aws.ec2_security_group: |
| 1272 |
name: "{{ ec2_group_name }}" |
| 1273 |
description: "{{ ec2_group_description }}" |
| 1274 |
vpc_id: "{{ vpc_result.vpc.id }}" |
| 1275 |
purge_rules_egress: false |
| 1276 |
purge_rules: false |
| 1277 |
state: present |
| 1278 |
rules: |
| 1279 |
- proto: tcp |
| 1280 |
ports: |
| 1281 |
- 8281 |
| 1282 |
cidr_ipv6: 1001:d00::/24 |
| 1283 |
rule_desc: |
| 1284 |
rules_egress: |
| 1285 |
- proto: tcp |
| 1286 |
ports: |
| 1287 |
- 8282 |
| 1288 |
cidr_ip: 10.2.2.2/32 |
| 1289 |
rule_desc: |
| 1290 |
register: result |
| 1291 |
ignore_errors: true |
| 1292 |
|
| 1293 |
- name: Assert that rule descriptions were removed |
| 1294 |
ansible.builtin.assert: |
| 1295 |
that: |
| 1296 |
- result.ip_permissions[0].ipv6_ranges[0].description is undefined |
| 1297 |
- result.ip_permissions_egress[0].ip_ranges[0].description is undefined |
| 1298 |
|
| 1299 |
# ============================================================ |
| 1300 |
|
| 1301 |
- name: Test state=absent (expected changed=true) |
| 1302 |
amazon.aws.ec2_security_group: |
| 1303 |
name: "{{ ec2_group_name }}" |
| 1304 |
state: absent |
| 1305 |
register: result |
| 1306 |
|
| 1307 |
- name: Assert state=absent (expected changed=true) |
| 1308 |
ansible.builtin.assert: |
| 1309 |
that: |
| 1310 |
- result.changed |
| 1311 |
- not result.group_id |
| 1312 |
always: |
| 1313 |
# ============================================================ |
| 1314 |
# Describe state of remaining resources |
| 1315 |
|
| 1316 |
- name: Retrieve security group info based on SG VPC |
| 1317 |
amazon.aws.ec2_security_group_info: |
| 1318 |
filters: |
| 1319 |
vpc-id: "{{ vpc_result.vpc.id }}" |
| 1320 |
register: remaining_groups |
| 1321 |
|
| 1322 |
- name: Retrieve subnet info based on SG VPC |
| 1323 |
amazon.aws.ec2_vpc_subnet_info: |
| 1324 |
filters: |
| 1325 |
vpc-id: "{{ vpc_result.vpc.id }}" |
| 1326 |
register: remaining_subnets |
| 1327 |
|
| 1328 |
- name: Retrieve VPC info based on SG VPC |
| 1329 |
amazon.aws.ec2_vpc_net_info: |
| 1330 |
vpc_ids: |
| 1331 |
- "{{ vpc_result.vpc.id }}" |
| 1332 |
register: remaining_vpc |
| 1333 |
|
| 1334 |
# ============================================================ |
| 1335 |
# Delete all remaining SGs |
| 1336 |
|
| 1337 |
- name: Delete rules from remaining SGs |
| 1338 |
amazon.aws.ec2_security_group: |
| 1339 |
name: "{{ item.group_name }}" |
| 1340 |
group_id: "{{ item.group_id }}" |
| 1341 |
description: "{{ item.description }}" |
| 1342 |
rules: [] |
| 1343 |
rules_egress: [] |
| 1344 |
loop: "{{ remaining_groups.security_groups }}" |
| 1345 |
ignore_errors: true |
| 1346 |
|
| 1347 |
- name: Delete remaining SGs |
| 1348 |
amazon.aws.ec2_security_group: |
| 1349 |
state: absent |
| 1350 |
group_id: "{{ item.group_id }}" |
| 1351 |
loop: "{{ remaining_groups.security_groups }}" |
| 1352 |
when: |
| 1353 |
- item.group_name != 'default' |
| 1354 |
ignore_errors: true |
| 1355 |
|
| 1356 |
# ============================================================ |
| 1357 |
|
| 1358 |
- name: Tidy up VPC |
| 1359 |
amazon.aws.ec2_vpc_net: |
| 1360 |
name: "{{ resource_prefix }}-vpc" |
| 1361 |
state: absent |
| 1362 |
cidr_block: "{{ vpc_cidr }}" |
| 1363 |
ignore_errors: true |