Debian Codegraph @

ansible_collections/microsoft/iis/plugins/modules/website.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2025, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7
8 # Define Bindings Options
9 $binding_options = @{
10 type = 'list'
11 elements = 'dict'
12 options = @{
13 ip = @{ type = 'str' }
14 port = @{ type = 'int' }
15 hostname = @{ type = 'str' }
16 protocol = @{ type = 'str' ; default = 'http' ; choices = @('http', 'https') }
17 use_sni = @{ type = 'bool' }
18 use_ccs = @{ type = 'bool' }
19 certificate_hash = @{ type = 'str' }
20 certificate_store_name = @{ type = 'str'; default = "my" }
21 }
22 }
23
24 $spec = @{
25 options = @{
26 name = @{
27 required = $true
28 type = "str"
29 }
30 state = @{
31 type = "str"
32 default = "started"
33 choices = @("absent", "restarted", "started", "stopped")
34 }
35 site_id = @{
36 type = "str"
37 }
38 application_pool = @{
39 type = "str"
40 }
41 physical_path = @{
42 type = "str"
43 }
44 bindings = @{
45 default = @{}
46 type = 'dict'
47 options = @{
48 add = $binding_options
49 set = $binding_options
50 remove = @{
51 type = 'list'
52 elements = 'dict'
53 options = @{
54 ip = @{ type = 'str' }
55 port = @{ type = 'int' }
56 hostname = @{ type = 'str' }
57 }
58 }
59 }
60 mutually_exclusive = @(
61 , @('set', 'add')
62 , @('set', 'remove')
63 )
64 }
65 }
66 supports_check_mode = $true
67 }
68 $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
69
70 $name = $module.Params.name
71 $state = $module.Params.state
72 $site_id = $module.Params.site_id
73 $application_pool = $module.Params.application_pool
74 $physical_path = $module.Params.physical_path
75 $bindings = $module.Params.bindings
76
77 $check_mode = $module.CheckMode
78 $module.Result.changed = $false
79
80 function Get-SSLFlagFromBinding {
81 param (
82 [Parameter(Mandatory = $true)]
83 $BindingInfo
84 )
85 $ssl_flags = 0
86 # Check for use_sni
87 if ($BindingInfo.use_sni) {
88 If ($BindingInfo.protocol -ne 'https') {
89 $module.FailJson("use_sni can only be set for https protocol")
90 }
91 If (-Not $BindingInfo.hostname) {
92 $module.FailJson("must specify hostname value when use_sni is set.")
93 }
94 $ssl_flags += 1
95 }
96 # Check for use_ccs
97 if ($BindingInfo.use_ccs) {
98 If ($BindingInfo.protocol -ne 'https') {
99 $module.FailJson("use_ccs can only be set for https protocol")
100 }
101 If (-Not $BindingInfo.hostname) {
102 $module.FailJson("must specify hostname value when use_ccs is set.")
103 }
104 If ($BindingInfo.certificate_hash) {
105 $module.FailJson("You set use_ccs to $($BindingInfo.use_ccs).
106 This indicates you wish to use the Central Certificate Store feature.
107 This cannot be used in combination with certficiate_hash and certificate_store_name. When using the Central Certificate Store feature,
108 the certificate is automatically retrieved from the store rather than manually assigned to the binding.")
109 }
110 $ssl_flags += 2
111 }
112 # Validate protocol is https and either use_ccs or certificate_hash is set
113 If ($BindingInfo.protocol -eq 'https') {
114 if (-Not $BindingInfo.use_ccs -and -Not $BindingInfo.certificate_hash) {
115 $module.FailJson("must either specify a certificate_hash or use_ccs.")
116 }
117 }
118 # Validate certificate_hash
119 If ($BindingInfo.certificate_hash) {
120 If ($BindingInfo.protocol -ne 'https') {
121 $module.FailJson("You can only provide a certificate thumbprint when protocol is set to https")
122 }
123 # Validate cert path
124 $cert_path = "cert:\LocalMachine\$($BindingInfo.certificate_store_name)\$($BindingInfo.certificate_hash)"
125 If (-Not (Test-Path -LiteralPath $cert_path)) {
126 $module.FailJson("Unable to locate certificate at $cert_path")
127 }
128 }
129 return $ssl_flags
130 }
131
132 # Ensure WebAdministration module is loaded
133 if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
134 Import-Module WebAdministration
135 }
136
137 # Site info
138 $site = Get-Website | Where-Object { $_.Name -eq $name }
139
140 Try {
141 # Add site
142 If (($state -ne 'absent') -and (-not $site)) {
143 If (-not $physical_path) {
144 $module.FailJson("missing required arguments: physical_path")
145 }
146 ElseIf (-not (Test-Path -LiteralPath $physical_path)) {
147 $module.FailJson("specified folder must already exist: physical_path")
148 }
149 $site_parameters = @{
150 Name = $name
151 PhysicalPath = $physical_path
152 }
153 If ($application_pool) {
154 $site_parameters.ApplicationPool = $application_pool
155 }
156 If ($site_id) {
157 $site_parameters.ID = $site_id
158 }
159 # Fix for error "New-Item : Index was outside the bounds of the array."
160 # This is a bug in the New-WebSite commandlet. Apparently there must be at least one site configured in IIS otherwise New-WebSite crashes.
161 # For more details, see http://stackoverflow.com/questions/3573889/ps-c-new-website-blah-throws-index-was-outside-the-bounds-of-the-array
162 $sites_list = Get-ChildItem -LiteralPath IIS:\sites
163 if ($null -eq $sites_list) {
164 if ($site_id) {
165 $site_parameters.ID = $site_id
166 }
167 else {
168 $site_parameters.ID = 1
169 }
170 }
171 if ( -not $check_mode) {
172 $site = New-Website @site_parameters -Force
173 }
174 # Verify that initial site has no binding
175 Get-WebBinding -Name $site.Name | Remove-WebBinding -WhatIf:$check_mode
176 $module.Result.changed = $true
177 }
178 # Remove site
179 If ($state -eq 'absent' -and $site) {
180 $site = Remove-Website -Name $name -WhatIf:$check_mode
181 $module.Result.changed = $true
182 }
183 $site = Get-Website | Where-Object { $_.Name -eq $name }
184 If ($site) {
185 # Change Physical Path if needed
186 if ($physical_path) {
187 If (-not (Test-Path -LiteralPath $physical_path)) {
188 $module.FailJson("specified folder must already exist: physical_path")
189 }
190 $folder = Get-Item -LiteralPath $physical_path
191 If ($folder.FullName -ne $site.PhysicalPath) {
192 Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -name physicalPath -value $folder.FullName -WhatIf:$check_mode
193 $module.Result.changed = $true
194 }
195 }
196 # Change Application Pool if needed
197 if ($application_pool) {
198 If ($application_pool -ne $site.applicationPool) {
199 Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -name applicationPool -value $application_pool -WhatIf:$check_mode
200 $module.Result.changed = $true
201 }
202 }
203 # Add Remove or Set bindings if needed
204 if ( $null -ne $bindings.set -or $bindings.add.Count -gt 0 -or $bindings.remove.Count -gt 0 ) {
205 $site_bindings = (Get-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)").Bindings.Collection
206 $toAdd = @()
207 $toEdit = @()
208 $toRemove = @()
209 if ($null -ne $bindings.set) {
210 $toAdd = $bindings.set | Where-Object { -not ($site_bindings.bindingInformation -contains "$($_.ip):$($_.port):$($_.hostname)") }
211 $toEdit = $bindings.set | Where-Object { ($site_bindings.bindingInformation -contains "$($_.ip):$($_.port):$($_.hostname)") }
212 $user_bindings = $bindings.set | ForEach-Object { "$($_.ip):$($_.port):$($_.hostname)" }
213 if ($null -ne $site_bindings.bindingInformation) {
214 $toRemove = $site_bindings.bindingInformation | Where-Object { $_ -notin $user_bindings }
215 }
216 }
217 else {
218 if ($bindings.add) {
219 $toAdd = $bindings.add | Where-Object { -not ($site_bindings.bindingInformation -contains "$($_.ip):$($_.port):$($_.hostname)") }
220 $toEdit = $bindings.add | Where-Object { ($site_bindings.bindingInformation -contains "$($_.ip):$($_.port):$($_.hostname)") }
221 }
222 if ($bindings.remove) {
223 $user_bindings = $bindings.remove | ForEach-Object { "$($_.ip):$($_.port):$($_.hostname)" }
224 $toRemove = $site_bindings.bindingInformation | Where-Object { $_ -in $user_bindings }
225 }
226 }
227 $toAdd | ForEach-Object {
228 $ssl_flags = Get-SSLFlagFromBinding -BindingInfo $_
229 if (-not $check_mode) {
230 New-WebBinding -Name $site.Name -IPAddress $_.ip -Port $_.port -HostHeader $_.hostname -Protocol $_.protocol -SslFlags $ssl_flags
231 If ($_.certificate_hash) {
232 $new_binding = Get-WebBinding -Name $site.Name -IPAddress $_.ip -Port $_.port -HostHeader $_.hostname
233 $new_binding.AddSslCertificate($_.certificate_hash, $_.certificate_store_name)
234 }
235 }
236 $module.Result.changed = $true
237 }
238 $toEdit | ForEach-Object {
239 $user_edit = $_
240 $site_edit = $site_bindings | Where-Object { $_.bindingInformation -eq "$($user_edit.ip):$($user_edit.port):$($user_edit.hostname)" }
241 $binding_index = $site_bindings.IndexOf($site_edit)
242 # Get existing use_sni value from site if null
243 if ($null -eq $user_edit.use_sni) {
244 $user_edit.use_sni = $site_edit.sslFlags % 2
245 }
246 # Get existing use_ccs value from site if null
247 if ($null -eq $user_edit.use_ccs) {
248 $user_edit.use_ccs = [math]::Floor($site_edit.sslFlags / 2)
249 }
250 $ssl_flags = Get-SSLFlagFromBinding -BindingInfo $user_edit
251 if ($site_edit.protocol -ne $user_edit.protocol) {
252 Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -Name "Bindings.Collection[$binding_index].protocol"`
253 -value $user_edit.protocol -WhatIf:$check_mode
254 $module.Result.changed = $true
255 }
256 if ($site_edit.sslFlags -ne $ssl_flags) {
257 Set-ItemProperty -LiteralPath "IIS:\Sites\$($site.Name)" -Name "Bindings.Collection[$binding_index].sslFlags"`
258 -value $ssl_flags -WhatIf:$check_mode
259 $module.Result.changed = $true
260 }
261 If ($user_edit.certificate_hash) {
262 $edit_binding = Get-WebBinding -Name $site.Name -IPAddress $user_edit.ip -Port $user_edit.port -HostHeader $user_edit.hostname
263 $edit_binding.AddSslCertificate($user_edit.certificate_hash, $user_edit.certificate_store_name)
264 }
265 }
266 $toRemove | ForEach-Object {
267 $remove_binding = $_ -split ':'
268 Get-WebBinding -Name $site.Name -IPAddress $remove_binding[0] -Port $remove_binding[1]`
269 -HostHeader $remove_binding[2] | Remove-WebBinding -WhatIf:$check_mode
270 $module.Result.changed = $true
271 }
272 }
273 # Set run state
274 if ((($state -eq 'stopped') -or ($state -eq 'restarted')) -and ($site.State -eq 'Started')) {
275 if (-not $check_mode) {
276 Stop-Website -Name $name -ErrorAction Stop
277 }
278 $module.Result.changed = $true
279 }
280 if ((($state -eq 'started') -and ($site.State -eq 'Stopped')) -or ($state -eq 'restarted')) {
281 if (-not $check_mode) {
282 Start-Website -Name $name -ErrorAction Stop
283 }
284 $module.Result.changed = $true
285 }
286 }
287 }
288 Catch {
289 $module.FailJson("$($module.Result) - $($_.Exception.Message)", $_)
290 }
291
292 $module.ExitJson()