Debian Codegraph @

ansible_collections/community/windows/tests/integration/targets/win_firewall_rule/tasks/main.yml

raw
1 - name: Remove potentially leftover firewall rule
2 win_firewall_rule:
3 name: http
4 state: absent
5 action: allow
6 direction: in
7
8 - name: Add firewall rule
9 win_firewall_rule:
10 name: http
11 enabled: yes
12 state: present
13 localport: 80
14 action: allow
15 direction: in
16 protocol: tcp
17 register: add_firewall_rule
18
19 - name: Check that creating new firewall rule succeeds with a change
20 assert:
21 that:
22 - add_firewall_rule.changed == true
23
24 - name: Add same firewall rule (again)
25 win_firewall_rule:
26 name: http
27 enabled: yes
28 state: present
29 localport: 80
30 action: allow
31 direction: in
32 protocol: tcp
33 register: add_firewall_rule_again
34
35 - name: Check that creating same firewall rule succeeds without a change
36 assert:
37 that:
38 - add_firewall_rule_again.changed == false
39
40 - name: Remove firewall rule
41 win_firewall_rule:
42 name: http
43 enabled: yes
44 state: absent
45 localport: 80
46 action: allow
47 direction: in
48 protocol: tcp
49 register: remove_firewall_rule
50
51 - name: Check that removing existing firewall rule succeeds with a change
52 assert:
53 that:
54 - remove_firewall_rule.changed == true
55
56 - name: Remove absent firewall rule
57 win_firewall_rule:
58 name: http
59 enabled: yes
60 state: absent
61 localport: 80
62 action: allow
63 direction: in
64 protocol: tcp
65 register: remove_absent_firewall_rule
66
67 - name: Check that removing non existing firewall rule succeeds without a change
68 assert:
69 that:
70 - remove_absent_firewall_rule.changed == false
71
72 - name: Add firewall rule
73 win_firewall_rule:
74 name: http
75 enabled: no
76 state: present
77 group: application
78 localport: 80
79 action: allow
80 direction: in
81 protocol: tcp
82 register: add_firewall_rule
83
84 - name: Check that creating new firewall rule succeeds with a change
85 assert:
86 that:
87 - add_firewall_rule.changed == true
88
89 - name: Enable all the rules in application group
90 win_firewall_rule:
91 group: application
92 enabled: yes
93 register: change_firewall_rule
94
95 - name: Check if the rules are enabled in application group
96 assert:
97 that:
98 - change_firewall_rule.changed == true
99
100 - name: Enable all the rules in application group (again)
101 win_firewall_rule:
102 group: application
103 enabled: yes
104 register: change_firewall_rule
105
106 - name: Check if the rules are enabled without a change
107 assert:
108 that:
109 - change_firewall_rule.changed == false
110
111 - name: Disable all the rules in application group
112 win_firewall_rule:
113 group: application
114 enabled: no
115 register: change_firewall_rule
116
117 - name: Check if the rules are disabled
118 assert:
119 that:
120 - change_firewall_rule.changed == true
121
122 - name: Disable all the rules in application group (again)
123 win_firewall_rule:
124 group: application
125 enabled: no
126 register: change_firewall_rule
127
128 - name: Check if the rules are disabled without a change
129 assert:
130 that:
131 - change_firewall_rule.changed == false
132
133 - name: Remove firewall rule
134 win_firewall_rule:
135 name: http
136 enabled: no
137 state: absent
138 localport: 80
139 action: allow
140 group: application
141 direction: in
142 protocol: tcp
143 register: remove_firewall_rule
144
145 - name: Check that removing existing firewall rule succeeds with a change
146 assert:
147 that:
148 - remove_firewall_rule.changed == true
149
150 - name: Add firewall rule
151 win_firewall_rule:
152 name: http
153 enabled: yes
154 state: present
155 localport: 80
156 action: allow
157 direction: in
158 protocol: tcp
159
160 - name: Change firewall rule
161 win_firewall_rule:
162 name: http
163 enabled: yes
164 state: present
165 localport: 80
166 action: block
167 direction: in
168 protocol: tcp
169 register: change_firewall_rule
170
171 - name: Check that changing firewall rule succeeds
172 assert:
173 that:
174 - change_firewall_rule.changed == true
175
176 - name: Disable firewall rule
177 win_firewall_rule:
178 name: http
179 enabled: no
180
181 - name: Get the actual values from the changed firewall rule
182 ansible.windows.win_shell: '(New-Object -ComObject HNetCfg.FwPolicy2).Rules | Where-Object { $_.Name -eq "http" } | Foreach-Object { $_.LocalPorts; $_.Enabled; $_.Action; $_.Direction; $_.Protocol }'
183 register: firewall_rule_actual
184
185 - name: Ensure that disabling the rule did not change the previous values
186 assert:
187 that:
188 - "firewall_rule_actual.stdout_lines[0] == '80'" # LocalPorts = 80
189 - "firewall_rule_actual.stdout_lines[1] == 'False'" # Enabled = False
190 - "firewall_rule_actual.stdout_lines[2] == '0'" # Action = block
191 - "firewall_rule_actual.stdout_lines[3] == '1'" # Direction = in
192 - "firewall_rule_actual.stdout_lines[4] == '6'" # Protocol = tcp
193
194 - name: Add firewall rule when remoteip is range
195 win_firewall_rule:
196 name: http
197 enabled: yes
198 state: present
199 localport: 80
200 remoteip: 192.168.0.1-192.168.0.5
201 action: allow
202 direction: in
203 protocol: tcp
204
205 - name: Add same firewall rule when remoteip is range (again)
206 win_firewall_rule:
207 name: http
208 enabled: yes
209 state: present
210 localport: 80
211 remoteip: 192.168.0.1-192.168.0.5
212 action: allow
213 direction: in
214 protocol: tcp
215 register: add_firewall_rule_with_range_remoteip_again
216
217 - name: Check that creating same firewall rule when remoteip is range succeeds without a change
218 assert:
219 that:
220 - add_firewall_rule_with_range_remoteip_again.changed == false
221
222 - name: Add firewall rule when remoteip in CIDR notation
223 win_firewall_rule:
224 name: http
225 enabled: yes
226 state: present
227 localport: 80
228 remoteip: 192.168.0.0/24
229 action: allow
230 direction: in
231 protocol: tcp
232
233 - name: Add same firewall rule when remoteip in CIDR notation (again)
234 win_firewall_rule:
235 name: http
236 enabled: yes
237 state: present
238 localport: 80
239 remoteip: 192.168.0.0/24
240 action: allow
241 direction: in
242 protocol: tcp
243 register: add_firewall_rule_with_cidr_remoteip_again
244
245 - name: Check that creating same firewall rule succeeds without a change when remoteip in CIDR notation
246 assert:
247 that:
248 - add_firewall_rule_with_cidr_remoteip_again.changed == false
249
250 - name: Add firewall rule when remoteip contains a netmask
251 win_firewall_rule:
252 name: http
253 enabled: yes
254 state: present
255 localport: 80
256 remoteip: 192.168.1.0/255.255.255.0
257 action: allow
258 direction: in
259 protocol: tcp
260
261 - name: Add same firewall rule when remoteip contains a netmask (again)
262 win_firewall_rule:
263 name: http
264 enabled: yes
265 state: present
266 localport: 80
267 remoteip: 192.168.1.0/255.255.255.0
268 action: allow
269 direction: in
270 protocol: tcp
271 register: add_firewall_rule_remoteip_contains_netmask_again
272
273 - name: Check that creating same firewall rule succeeds without a change when remoteip contains a netmask
274 assert:
275 that:
276 - add_firewall_rule_remoteip_contains_netmask_again.changed == false
277
278 - name: Add firewall rule when remoteip is IPv4
279 win_firewall_rule:
280 name: http
281 enabled: yes
282 state: present
283 localport: 80
284 remoteip: 192.168.0.1
285 action: allow
286 direction: in
287 protocol: tcp
288
289 - name: Add same firewall rule when remoteip is IPv4 (again)
290 win_firewall_rule:
291 name: http
292 enabled: yes
293 state: present
294 localport: 80
295 remoteip: 192.168.0.1
296 action: allow
297 direction: in
298 protocol: tcp
299 register: add_firewall_rule_with_ipv4_remoteip_again
300
301 - name: Check that creating same firewall rule when remoteip is IPv4 succeeds without a change
302 assert:
303 that:
304 - add_firewall_rule_with_ipv4_remoteip_again.changed == false
305
306 - name: Add firewall rule when remoteip contains a netmask
307 win_firewall_rule:
308 name: http
309 enabled: yes
310 state: present
311 localport: 80
312 remoteip: 192.168.2.0/255.255.255.0
313 action: allow
314 direction: in
315 protocol: tcp
316
317 - name: Add same firewall rule when remoteip in CIDR notation
318 win_firewall_rule:
319 name: http
320 enabled: yes
321 state: present
322 localport: 80
323 remoteip: 192.168.2.0/24
324 action: allow
325 direction: in
326 protocol: tcp
327 register: add_same_firewall_rule_with_cidr_remoteip
328
329 - name: Check that creating same firewall rule succeeds without a change when remoteip contains a netmask or CIDR
330 assert:
331 that:
332 - add_same_firewall_rule_with_cidr_remoteip.changed == false
333
334 - name: Add firewall rule with multiple ports
335 win_firewall_rule:
336 name: http
337 enabled: yes
338 state: present
339 localport: '80,81'
340 action: allow
341 direction: in
342 protocol: tcp
343 register: add_firewall_rule_with_multiple_ports
344
345 - name: Check that creating firewall rule with multiple ports succeeds with a change
346 assert:
347 that:
348 - add_firewall_rule_with_multiple_ports.changed == true
349
350 - name: Add firewall rule with interface types in string format
351 win_firewall_rule:
352 name: http
353 enabled: yes
354 state: present
355 localport: 80
356 action: allow
357 direction: in
358 protocol: tcp
359 interfacetypes: 'ras,lan,wireless'
360 register: add_firewall_rule_with_string_interface_types
361
362 - name: Check that creating firewall rule with interface types in string format succeeds with a change
363 assert:
364 that:
365 - add_firewall_rule_with_string_interface_types.changed == true
366
367 - name: Add firewall rule with interface types in list format
368 win_firewall_rule:
369 name: http
370 enabled: yes
371 state: present
372 localport: 80
373 action: allow
374 direction: in
375 protocol: tcp
376 interfacetypes: [ras, lan]
377 register: add_firewall_rule_with_list_interface_types
378
379 - name: Check that creating firewall rule with interface types in list format succeeds with a change
380 assert:
381 that:
382 - add_firewall_rule_with_list_interface_types.changed == true
383
384 - name: Add firewall rule with interface type 'any'
385 win_firewall_rule:
386 name: http
387 enabled: yes
388 state: present
389 localport: 80
390 action: allow
391 direction: in
392 protocol: tcp
393 interfacetypes: any
394 register: add_firewall_rule_with_interface_type_any
395
396 - name: Check that creating firewall rule with interface type 'any' succeeds with a change
397 assert:
398 that:
399 - add_firewall_rule_with_interface_type_any.changed == true
400
401 - name: Add firewall rule with edge traversal option 'deferapp'
402 win_firewall_rule:
403 name: http
404 enabled: yes
405 state: present
406 localport: 80
407 action: allow
408 direction: in
409 protocol: tcp
410 edge: deferapp
411 register: add_firewall_rule_with_edge_traversal
412
413 # Setup action creates ansible_distribution_version variable
414 - ansible.windows.setup:
415
416 - name: Check that creating firewall rule with enge traversal option 'deferapp' succeeds with a change
417 assert:
418 that:
419 - add_firewall_rule_with_edge_traversal.changed == true
420 # Works on windows >= Windows 7/Windows Server 2008 R2
421 when: ansible_distribution_version is version('6.1', '>=')
422
423 - name: Add firewall rule with 'authenticate' secure flag
424 win_firewall_rule:
425 name: http
426 enabled: yes
427 state: present
428 localport: 80
429 action: allow
430 direction: in
431 protocol: tcp
432 security: authenticate
433 register: add_firewall_rule_with_secure_flags
434
435 - name: Check that creating firewall rule with secure flag 'authenticate' succeeds with a change
436 assert:
437 that:
438 - add_firewall_rule_with_secure_flags.changed == true
439 # Works on windows >= Windows 8/Windows Server 2012
440 when: ansible_distribution_version is version('6.2', '>=')
441
442 - name: Add firewall rule with profiles in string format
443 win_firewall_rule:
444 name: http
445 enabled: yes
446 state: present
447 localport: 80
448 action: allow
449 direction: in
450 protocol: tcp
451 profiles: 'domain,public'
452 register: add_firewall_rule_with_string_profiles
453
454 - name: Check that creating firewall rule with profiles in string format succeeds with a change
455 assert:
456 that:
457 - add_firewall_rule_with_string_profiles.changed == true
458
459 - name: Set firewall rule profile back to 'all'
460 win_firewall_rule:
461 name: http
462 enabled: yes
463 state: present
464 localport: 80
465 action: allow
466 direction: in
467 protocol: tcp
468 profiles: [Domain, Public, Private]
469 register: add_firewall_rule_with_string_profiles
470
471 - name: Check that setting firewall rule profile back to 'all' succeeds with a change
472 assert:
473 that:
474 - add_firewall_rule_with_string_profiles.changed == true
475
476 - name: Add firewall rule with profiles in list format
477 win_firewall_rule:
478 name: http
479 enabled: yes
480 state: present
481 localport: 80
482 action: allow
483 direction: in
484 protocol: tcp
485 profiles: [Domain, Private]
486 register: add_firewall_rule_with_list_profiles
487
488 - name: Check that creating firewall rule with profiles in list format succeeds with a change
489 assert:
490 that:
491 - add_firewall_rule_with_list_profiles.changed == true
492
493 # Test for variable expansion in the path
494 - name: Add rule with path that needs to be expanded
495 win_firewall_rule:
496 name: VarExpansionTest
497 enabled: yes
498 state: present
499 action: allow
500 direction: in
501 protocol: tcp
502 program: '%SystemRoot%\system32\svchost.exe'
503
504 - name: Add same rule with path that needs to be expanded
505 win_firewall_rule:
506 name: VarExpansionTest
507 enabled: yes
508 state: present
509 action: allow
510 direction: in
511 protocol: tcp
512 program: '%SystemRoot%\system32\svchost.exe'
513 register: add_firewall_rule_with_var_expand_path
514
515 - name: Check that creating same firewall rule with expanded vars identified
516 assert:
517 that:
518 - add_firewall_rule_with_var_expand_path.changed == false
519
520 - name: Add firewall rule for application group
521 win_firewall_rule:
522 name: Rule for application group
523 enabled: yes
524 state: present
525 localport: 80
526 action: allow
527 direction: in
528 protocol: tcp
529 group: application
530 register: add_firewall_rule_with_group
531
532 - name: Check that creating firewall rule for application group succeeds with a change
533 assert:
534 that:
535 - add_firewall_rule_with_group.changed == true
536
537 # Test icmptypecode
538 - name: Add rule with icmptypecode
539 win_firewall_rule:
540 name: icmptest
541 enabled: yes
542 state: present
543 action: allow
544 direction: in
545 protocol: icmpv4
546 icmp_type_code: '8:*'
547 register: add_firewall_rule_with_icmptypecode
548
549 - name: Check that creating same firewall rule with expanded vars identified
550 assert:
551 that:
552 - add_firewall_rule_with_icmptypecode.changed == true
553
554 - name: Remove rule with icmptypecode
555 win_firewall_rule:
556 name: icmptest
557 enabled: yes
558 state: absent
559 action: allow
560 direction: in
561 protocol: icmpv4
562 icmp_type_code: '8:*'
563 register: remove_firewall_rule_with_icmptypecode
564
565 - name: Check that removing same firewall rule with expanded vars identified
566 assert:
567 that:
568 - remove_firewall_rule_with_icmptypecode.changed == true
569
570 # test for application name / program changes to any (and if they null the property)
571 # -----------------------------------------------------------------------------
572 - name: Add rule with an accociated program
573 win_firewall_rule:
574 name: ApplicationToAnyTest
575 enabled: true
576 state: present
577 action: allow
578 direction: in
579 protocol: tcp
580 program: '%SystemRoot%\system32\svchost.exe'
581 register: add_firewall_rule_with_program
582
583 - name: Check that creating firewall rule succeeds with a change
584 ansible.builtin.assert:
585 that:
586 - add_firewall_rule_with_program.changed == true
587
588 - name: Add same rule with program set to any
589 win_firewall_rule:
590 name: ApplicationToAnyTest
591 enabled: true
592 state: present
593 action: allow
594 direction: in
595 protocol: tcp
596 program: any
597 register: change_firewall_rule_with_program
598
599 - name: Check that changing firewall rule succeeds with a change
600 ansible.builtin.assert:
601 that:
602 - change_firewall_rule_with_program.changed == true
603
604 - name: Get the actual values from the changed firewall rule and check if ApplicationName is null
605 ansible.windows.win_shell: >-
606 ((New-Object -ComObject HNetCfg.FwPolicy2).Rules | Where-Object { $_.Name -eq "ApplicationToAnyTest" } | Foreach-Object { $_.ApplicationName }) -eq $null
607 register: firewall_rule_actual
608 failed_when: 'firewall_rule_actual.stdout_lines[0] != "True"'
609 # -----------------------------------------------------------------------------