1---
2# Cannot use win_feature to install RDS on Server 2008
3- name: check if feature is availble
4  win_shell: if (Get-Command -Name Add-WindowsFeature -ErrorAction SilentlyContinue) { $true } else { $false }
5  changed_when: False
6  register: module_available
7
8- name: install Remote Desktop Gateway features
9  when: module_available.stdout | trim | bool
10  block:
11  - name: ensure Remote Desktop Gateway services are installed
12    win_feature:
13      name:
14      - RDS-Gateway
15      - RDS-Licensing
16      - RDS-RD-Server
17      state: present
18    register: rds_install
19
20  - name: reboot server if needed
21    win_reboot:
22    when: rds_install.reboot_required
23
24  # After a reboot Windows is still configuring the feature, this is a hack to wait until that is finished
25  - name: wait for component servicing to be finished
26    win_shell: |
27      $start = Get-Date
28      $path = "HKLM:\SYSTEM\CurrentControlSet\Control\Winlogon\Notifications\Components\TrustedInstaller"
29      $tries = 0
30      while ((Get-ItemProperty -Path $path -Name Events).Events.Contains("CreateSession")) {
31          $tries += 1
32          Start-Sleep -Seconds 5
33          if (((Get-Date) - $start).TotalSeconds -gt 180) {
34              break
35          }
36      }
37      $tries
38    changed_when: False
39
40  - name: run win_rds_cap integration tests
41    include_tasks: win_rds_cap.yml
42
43  - name: run win_rds_rap integration tests
44    include_tasks: win_rds_rap.yml
45
46  - name: run win_rds_settings integration tests
47    include_tasks: win_rds_settings.yml
48
49  always:
50  # Server 2008 R2 requires us to remove this first before the other features
51  - name: remove the RDS-Gateway feature
52    win_feature:
53      name: RDS-Gateway
54      state: absent
55    register: rds_uninstall
56
57  - name: reboot after removing RDS-Gateway feature
58    win_reboot:
59    when: rds_uninstall.reboot_required
60
61  # Now remove the remaining features
62  - name: remove installed RDS feature
63    win_feature:
64      name:
65      - RDS-Licensing
66      - RDS-RD-Server
67      - Web-Server  # not part of the initial feature install but RDS-Gateway requires this and it breaks httptester
68      state: absent
69    register: rds_uninstall2
70
71  - name: reboot after feature removal
72    win_reboot:
73    when: rds_uninstall2.reboot_required
74