| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2015, Henrik Wallström <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
|
| 8 |
$params = Parse-Args $args -supports_check_mode $true |
| 9 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 10 |
|
| 11 |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true |
| 12 |
$site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true |
| 13 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent", "present" |
| 14 |
$physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" -aliases "path" |
| 15 |
$application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str" |
| 16 |
$connect_as = Get-AnsibleParam -obj $params -name 'connect_as' -type 'str' -validateset 'specific_user', 'pass_through' |
| 17 |
$username = Get-AnsibleParam -obj $params -name "username" -type "str" -failifempty ($connect_as -eq 'specific_user') |
| 18 |
$password = Get-AnsibleParam -obj $params -name "password" -type "str" -failifempty ($connect_as -eq 'specific_user') |
| 19 |
|
| 20 |
$result = @{ |
| 21 |
application_pool = $application_pool |
| 22 |
changed = $false |
| 23 |
physical_path = $physical_path |
| 24 |
} |
| 25 |
|
| 26 |
# Ensure WebAdministration module is loaded |
| 27 |
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 28 |
Import-Module WebAdministration |
| 29 |
} |
| 30 |
|
| 31 |
# Application info |
| 32 |
$application = Get-WebApplication -Site $site -Name $name |
| 33 |
$website = Get-Website -Name $site |
| 34 |
|
| 35 |
# Set ApplicationPool to current if not specified |
| 36 |
if (!$application_pool) { |
| 37 |
$application_pool = $website.applicationPool |
| 38 |
} |
| 39 |
|
| 40 |
try { |
| 41 |
# Add application |
| 42 |
if (($state -eq 'present') -and (-not $application)) { |
| 43 |
if (-not $physical_path) { |
| 44 |
Fail-Json $result "missing required arguments: path" |
| 45 |
} |
| 46 |
if (-not (Test-Path -LiteralPath $physical_path)) { |
| 47 |
Fail-Json $result "specified folder must already exist: path" |
| 48 |
} |
| 49 |
|
| 50 |
$application_parameters = @{ |
| 51 |
Name = $name |
| 52 |
PhysicalPath = $physical_path |
| 53 |
Site = $site |
| 54 |
} |
| 55 |
|
| 56 |
if ($application_pool) { |
| 57 |
$application_parameters.ApplicationPool = $application_pool |
| 58 |
} |
| 59 |
|
| 60 |
if (-not $check_mode) { |
| 61 |
$application = New-WebApplication @application_parameters -Force |
| 62 |
} |
| 63 |
$result.changed = $true |
| 64 |
} |
| 65 |
|
| 66 |
# Remove application |
| 67 |
if ($state -eq 'absent' -and $application) { |
| 68 |
$application = Remove-WebApplication -Site $site -Name $name -WhatIf:$check_mode |
| 69 |
$result.changed = $true |
| 70 |
} |
| 71 |
|
| 72 |
$application = Get-WebApplication -Site $site -Name $name |
| 73 |
if ($application) { |
| 74 |
|
| 75 |
# Change Physical Path if needed |
| 76 |
if ($physical_path) { |
| 77 |
if (-not (Test-Path -LiteralPath $physical_path)) { |
| 78 |
Fail-Json $result "specified folder must already exist: path" |
| 79 |
} |
| 80 |
|
| 81 |
$folder = Get-Item -LiteralPath $physical_path |
| 82 |
if ($folder.FullName -ne $application.PhysicalPath) { |
| 83 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode |
| 84 |
$result.changed = $true |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
# Change Application Pool if needed |
| 89 |
if ($application_pool) { |
| 90 |
if ($application_pool -ne $application.applicationPool) { |
| 91 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode |
| 92 |
$result.changed = $true |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
# Change username and password if needed |
| 97 |
$app_user = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' |
| 98 |
$app_pass = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' |
| 99 |
if ($connect_as -eq 'pass_through') { |
| 100 |
if ($app_user -ne '') { |
| 101 |
Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -WhatIf:$check_mode |
| 102 |
$result.changed = $true |
| 103 |
} |
| 104 |
if ($app_pass -ne '') { |
| 105 |
Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -WhatIf:$check_mode |
| 106 |
$result.changed = $true |
| 107 |
} |
| 108 |
} |
| 109 |
elseif ($connect_as -eq 'specific_user') { |
| 110 |
if ($app_user -ne $username) { |
| 111 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -Value $username -WhatIf:$check_mode |
| 112 |
$result.changed = $true |
| 113 |
} |
| 114 |
if ($app_pass -ne $password) { |
| 115 |
Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -Value $password -WhatIf:$check_mode |
| 116 |
$result.changed = $true |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
catch { |
| 122 |
Fail-Json $result $_.Exception.Message |
| 123 |
} |
| 124 |
|
| 125 |
# When in check-mode or on removal, this may fail |
| 126 |
$application = Get-WebApplication -Site $site -Name $name |
| 127 |
if ($application) { |
| 128 |
$app_user = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' |
| 129 |
if ($app_user -eq '') { |
| 130 |
$result.connect_as = 'pass_through' |
| 131 |
} |
| 132 |
else { |
| 133 |
$result.connect_as = 'specific_user' |
| 134 |
} |
| 135 |
|
| 136 |
$result.physical_path = $application.PhysicalPath |
| 137 |
$result.application_pool = $application.ApplicationPool |
| 138 |
} |
| 139 |
|
| 140 |
Exit-Json $result |