| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Henrik Wallström <[email protected]> |
| 4 |
# Copyright: (c) 2017, Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 8 |
|
| 9 |
$spec = @{ |
| 10 |
options = @{ |
| 11 |
name = @{ |
| 12 |
required = $true |
| 13 |
type = "str" |
| 14 |
} |
| 15 |
state = @{ |
| 16 |
type = "str" |
| 17 |
default = "present" |
| 18 |
choices = @("absent", "present", "restarted", "started", "stopped") |
| 19 |
} |
| 20 |
attributes = @{ |
| 21 |
type = "dict" |
| 22 |
# default empty dictionary to ensure it's always a dictionary |
| 23 |
default = @{} |
| 24 |
} |
| 25 |
} |
| 26 |
supports_check_mode = $true |
| 27 |
} |
| 28 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 29 |
|
| 30 |
$name = $module.Params.name |
| 31 |
$state = $module.Params.state |
| 32 |
$check_mode = $module.CheckMode |
| 33 |
$module.Result.changed = $false |
| 34 |
|
| 35 |
# Stores the free form attributes for the module |
| 36 |
$attributes = $module.Params.attributes |
| 37 |
|
| 38 |
Function Get-DotNetClassForAttribute($attribute_parent) { |
| 39 |
switch ($attribute_parent) { |
| 40 |
"attributes" { [Microsoft.Web.Administration.ApplicationPool] } |
| 41 |
"cpu" { [Microsoft.Web.Administration.ApplicationPoolCpu] } |
| 42 |
"failure" { [Microsoft.Web.Administration.ApplicationPoolFailure] } |
| 43 |
"processModel" { [Microsoft.Web.Administration.ApplicationPoolProcessModel] } |
| 44 |
"recycling" { [Microsoft.Web.Administration.ApplicationPoolRecycling] } |
| 45 |
default { [Microsoft.Web.Administration.ApplicationPool] } |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
Function Convert-CollectionToList($collection) { |
| 50 |
$list = @() |
| 51 |
|
| 52 |
if ($collection -is [String]) { |
| 53 |
$raw_list = $collection -split "," |
| 54 |
foreach ($entry in $raw_list) { |
| 55 |
$list += $entry.Trim() |
| 56 |
} |
| 57 |
} |
| 58 |
elseif ($collection -is [Microsoft.IIs.PowerShell.Framework.ConfigurationElement]) { |
| 59 |
# the collection is the value from IIS itself, we need to conver accordingly |
| 60 |
foreach ($entry in $collection.Collection) { |
| 61 |
$list += $entry.Value.ToString() |
| 62 |
} |
| 63 |
} |
| 64 |
elseif ($collection -isnot [Array]) { |
| 65 |
$list += $collection |
| 66 |
} |
| 67 |
else { |
| 68 |
$list = $collection |
| 69 |
} |
| 70 |
|
| 71 |
return , $list |
| 72 |
} |
| 73 |
|
| 74 |
Function Compare-Value($current, $new) { |
| 75 |
if ($null -eq $current) { |
| 76 |
return $true |
| 77 |
} |
| 78 |
|
| 79 |
if ($current -is [Array]) { |
| 80 |
if ($new -isnot [Array]) { |
| 81 |
return $true |
| 82 |
} |
| 83 |
|
| 84 |
if ($current.Count -ne $new.Count) { |
| 85 |
return $true |
| 86 |
} |
| 87 |
for ($i = 0; $i -lt $current.Count; $i++) { |
| 88 |
if ($current[$i] -ne $new[$i]) { |
| 89 |
return $true |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
else { |
| 94 |
if ($current -ne $new) { |
| 95 |
return $true |
| 96 |
} |
| 97 |
} |
| 98 |
return $false |
| 99 |
} |
| 100 |
|
| 101 |
Function Convert-ToPropertyValue($pool, $attribute_key, $attribute_value) { |
| 102 |
# Will convert the new value to the enum value expected and cast accordingly to the type |
| 103 |
if ([bool]($attribute_value.PSobject.Properties -match "Value")) { |
| 104 |
$attribute_value = $attribute_value.Value |
| 105 |
} |
| 106 |
$attribute_key_split = $attribute_key -split "\." |
| 107 |
if ($attribute_key_split.Length -eq 1) { |
| 108 |
$attribute_parent = "attributes" |
| 109 |
$attribute_child = $attribute_key |
| 110 |
$attribute_meta = $pool.Attributes | Where-Object { $_.Name -eq $attribute_child } |
| 111 |
} |
| 112 |
elseif ($attribute_key_split.Length -gt 1) { |
| 113 |
$attribute_parent = $attribute_key_split[0] |
| 114 |
$attribute_key_split = $attribute_key_split[1..$($attribute_key_split.Length - 1)] |
| 115 |
$parent = $pool.$attribute_parent |
| 116 |
|
| 117 |
foreach ($key in $attribute_key_split) { |
| 118 |
$attribute_meta = $parent.Attributes | Where-Object { $_.Name -eq $key } |
| 119 |
$parent = $parent.$key |
| 120 |
if ($null -eq $attribute_meta) { |
| 121 |
$attribute_meta = $parent |
| 122 |
} |
| 123 |
} |
| 124 |
$attribute_child = $attribute_key_split[-1] |
| 125 |
} |
| 126 |
|
| 127 |
if ($attribute_meta) { |
| 128 |
if (($attribute_meta.PSObject.Properties.Name -eq "Collection").Count -gt 0) { |
| 129 |
return , (Convert-CollectionToList -collection $attribute_value) |
| 130 |
} |
| 131 |
$type = $attribute_meta.Schema.Type |
| 132 |
$value = $attribute_value |
| 133 |
if ($type -eq "enum") { |
| 134 |
# Attempt to convert the value from human friendly to enum value - use existing value if we fail |
| 135 |
$dot_net_class = Get-DotNetClassForAttribute -attribute_parent $attribute_parent |
| 136 |
$enum_attribute_name = $attribute_child.Substring(0, 1).ToUpper() + $attribute_child.Substring(1) |
| 137 |
$enum = $dot_net_class.GetProperty($enum_attribute_name).PropertyType.FullName |
| 138 |
if ($enum) { |
| 139 |
$enum_values = [Enum]::GetValues($enum) |
| 140 |
foreach ($enum_value in $enum_values) { |
| 141 |
if ($attribute_value.GetType() -is $enum_value.GetType()) { |
| 142 |
if ($enum_value -eq $attribute_value) { |
| 143 |
$value = $enum_value |
| 144 |
break |
| 145 |
} |
| 146 |
} |
| 147 |
else { |
| 148 |
if ([System.String]$enum_value -eq [System.String]$attribute_value) { |
| 149 |
$value = $enum_value |
| 150 |
break |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
# Try and cast the variable using the chosen type, revert to the default if it fails |
| 157 |
Set-Variable -Name casted_value -Value ($value -as ([type] $attribute_meta.TypeName)) |
| 158 |
if ($null -eq $casted_value) { |
| 159 |
$value |
| 160 |
} |
| 161 |
else { |
| 162 |
$casted_value |
| 163 |
} |
| 164 |
} |
| 165 |
else { |
| 166 |
$attribute_value |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
# Ensure WebAdministration module is loaded |
| 171 |
if ($null -eq (Get-Module -Name "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 172 |
Import-Module WebAdministration |
| 173 |
$web_admin_dll_path = Join-Path $env:SystemRoot system32\inetsrv\Microsoft.Web.Administration.dll |
| 174 |
Add-Type -LiteralPath $web_admin_dll_path |
| 175 |
} |
| 176 |
|
| 177 |
$pool = Get-Item -LiteralPath IIS:\AppPools\$name -ErrorAction SilentlyContinue |
| 178 |
if ($state -eq "absent") { |
| 179 |
# Remove pool if present |
| 180 |
if ($pool) { |
| 181 |
try { |
| 182 |
Remove-WebAppPool -Name $name -WhatIf:$check_mode |
| 183 |
} |
| 184 |
catch { |
| 185 |
$module.FailJson("Failed to remove Web App pool $($name): $($_.Exception.Message)", $_) |
| 186 |
} |
| 187 |
$module.Result.changed = $true |
| 188 |
} |
| 189 |
} |
| 190 |
else { |
| 191 |
# Add pool if absent |
| 192 |
if (-not $pool) { |
| 193 |
if (-not $check_mode) { |
| 194 |
try { |
| 195 |
New-WebAppPool -Name $name > $null |
| 196 |
} |
| 197 |
catch { |
| 198 |
$module.FailJson("Failed to create new Web App Pool $($name): $($_.Exception.Message)", $_) |
| 199 |
} |
| 200 |
} |
| 201 |
$module.Result.changed = $true |
| 202 |
# If in check mode this pool won't actually exists so skip it |
| 203 |
if (-not $check_mode) { |
| 204 |
$pool = Get-Item -LiteralPath IIS:\AppPools\$name |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
# Cannot run the below in check mode if the pool did not always exist |
| 209 |
if ($pool) { |
| 210 |
# Modify pool based on parameters |
| 211 |
foreach ($attribute in $attributes.GetEnumerator()) { |
| 212 |
$attribute_key = $attribute.Key |
| 213 |
$new_raw_value = $attribute.Value |
| 214 |
$new_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $new_raw_value |
| 215 |
|
| 216 |
$current_raw_value = Get-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -ErrorAction SilentlyContinue |
| 217 |
$current_value = Convert-ToPropertyValue -pool $pool -attribute_key $attribute_key -attribute_value $current_raw_value |
| 218 |
|
| 219 |
$changed = Compare-Value -current $current_value -new $new_value |
| 220 |
if ($changed -eq $true) { |
| 221 |
if ($new_value -is [Array]) { |
| 222 |
try { |
| 223 |
Clear-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -WhatIf:$check_mode |
| 224 |
} |
| 225 |
catch { |
| 226 |
$msg = -join @( |
| 227 |
"Failed to clear attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 228 |
"Exception: $($_.Exception.Message)" |
| 229 |
) |
| 230 |
$module.FailJson($msg, $_) |
| 231 |
} |
| 232 |
foreach ($value in $new_value) { |
| 233 |
try { |
| 234 |
New-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -Value @{value = $value } -WhatIf:$check_mode > $null |
| 235 |
} |
| 236 |
catch { |
| 237 |
$msg = -join @( |
| 238 |
"Failed to add new attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 239 |
"Value: $value, Exception: $($_.Exception.Message)" |
| 240 |
) |
| 241 |
$module.FailJson($msg, $_) |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
else { |
| 246 |
try { |
| 247 |
Set-ItemProperty -LiteralPath IIS:\AppPools\$name -Name $attribute_key -Value $new_value -WhatIf:$check_mode |
| 248 |
} |
| 249 |
catch { |
| 250 |
$msg = -join @( |
| 251 |
"Failed to set attribute to Web App Pool $name. Attribute: $attribute_key, " |
| 252 |
"Value: $new_value, Exception: $($_.Exception.Message)" |
| 253 |
) |
| 254 |
$module.FailJson($msg, $_) |
| 255 |
} |
| 256 |
} |
| 257 |
$module.Result.changed = $true |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
# Set the state of the pool |
| 262 |
if ($pool.State -eq "Stopped") { |
| 263 |
if ($state -eq "started" -or $state -eq "restarted") { |
| 264 |
if (-not $check_mode) { |
| 265 |
try { |
| 266 |
Start-WebAppPool -Name $name > $null |
| 267 |
} |
| 268 |
catch { |
| 269 |
$module.FailJson("Failed to start Web App Pool $($name): $($_.Exception.Message)", $_) |
| 270 |
} |
| 271 |
} |
| 272 |
$module.Result.changed = $true |
| 273 |
} |
| 274 |
} |
| 275 |
else { |
| 276 |
if ($state -eq "stopped") { |
| 277 |
if (-not $check_mode) { |
| 278 |
try { |
| 279 |
Stop-WebAppPool -Name $name > $null |
| 280 |
} |
| 281 |
catch { |
| 282 |
$module.FailJson("Failed to stop Web App Pool $($name): $($_.Exception.Message)", $_) |
| 283 |
} |
| 284 |
} |
| 285 |
$module.Result.changed = $true |
| 286 |
} |
| 287 |
elseif ($state -eq "restarted") { |
| 288 |
if (-not $check_mode) { |
| 289 |
try { |
| 290 |
Restart-WebAppPool -Name $name > $null |
| 291 |
} |
| 292 |
catch { |
| 293 |
$module.FailJson("Failed to restart Web App Pool $($name): $($_.Exception.Message)", $_) |
| 294 |
} |
| 295 |
} |
| 296 |
$module.Result.changed = $true |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
$module.ExitJson() |