|
| 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 | +#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 | + |
| 8 | +$spec = @{ |
| 9 | + options = @{ |
| 10 | + name = @{ type = "str"; required = $true } |
| 11 | + site = @{ type = "str"; required = $true } |
| 12 | + state = @{ type = "str"; default = "present"; choices = "absent", "present" } |
| 13 | + physical_path = @{ type = "str"; aliases = @("path") } |
| 14 | + application_pool = @{ type = "str" } |
| 15 | + connect_as = @{ type = "str"; choices = "specific_user", "pass_through" } |
| 16 | + username = @{ type = "str" } |
| 17 | + password = @{ type = "str"; no_log = $true } |
| 18 | + } |
| 19 | + supports_check_mode = $true |
| 20 | +} |
| 21 | + |
| 22 | +$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 23 | +$name = $module.Params.name |
| 24 | +$site = $module.Params.site |
| 25 | +$state = $module.Params.state |
| 26 | +$physical_path = $module.Params.physical_path |
| 27 | +$application_pool = $module.Params.application_pool |
| 28 | +$connect_as = $module.Params.connect_as |
| 29 | +$username = $module.Params.username |
| 30 | +$password = $module.Params.password |
| 31 | +$check_mode = $module.CheckMode |
| 32 | + |
| 33 | +if ($connect_as -eq 'specific_user') { |
| 34 | + if (-not $username) { |
| 35 | + $module.FailJson("missing required arguments: username") |
| 36 | + } |
| 37 | + if (-not $password) { |
| 38 | + $module.FailJson("missing required arguments: password") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +# Ensure WebAdministration module is loaded |
| 43 | +if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) { |
| 44 | + Import-Module WebAdministration |
| 45 | +} |
| 46 | + |
| 47 | +# Application info |
| 48 | +$application = Get-WebApplication -Site $site -Name $name |
| 49 | +$website = Get-Website -Name $site |
| 50 | + |
| 51 | +# Set ApplicationPool to current if not specified |
| 52 | +if (!$application_pool) { |
| 53 | + $application_pool = $website.applicationPool |
| 54 | +} |
| 55 | + |
| 56 | +try { |
| 57 | + # Add application |
| 58 | + if (($state -eq 'present') -and (-not $application)) { |
| 59 | + if (-not $physical_path) { |
| 60 | + $module.FailJson("missing required arguments: physical_path") |
| 61 | + } |
| 62 | + if (-not (Test-Path -LiteralPath $physical_path)) { |
| 63 | + $module.FailJson("specified folder must already exist: '$physical_path'") |
| 64 | + } |
| 65 | + |
| 66 | + $application_parameters = @{ |
| 67 | + Name = $name |
| 68 | + PhysicalPath = $physical_path |
| 69 | + Site = $site |
| 70 | + ApplicationPool = $application_pool |
| 71 | + } |
| 72 | + |
| 73 | + if (-not $check_mode) { |
| 74 | + $application = New-WebApplication @application_parameters -Force |
| 75 | + } |
| 76 | + $module.Result.changed = $true |
| 77 | + } |
| 78 | + |
| 79 | + # Remove application |
| 80 | + if ($state -eq 'absent' -and $application) { |
| 81 | + $application = Remove-WebApplication -Site $site -Name $name -WhatIf:$check_mode |
| 82 | + $module.Result.changed = $true |
| 83 | + } |
| 84 | + |
| 85 | + $application = Get-WebApplication -Site $site -Name $name |
| 86 | + if ($application) { |
| 87 | + |
| 88 | + # Change Physical Path if needed |
| 89 | + if ($physical_path) { |
| 90 | + if (-not (Test-Path -LiteralPath $physical_path)) { |
| 91 | + $module.FailJson("specified folder must already exist: '$physical_path'") |
| 92 | + } |
| 93 | + |
| 94 | + $folder = Get-Item -LiteralPath $physical_path |
| 95 | + if ($folder.FullName -ne $application.PhysicalPath) { |
| 96 | + Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode |
| 97 | + $module.Result.changed = $true |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + # Change Application Pool if needed |
| 102 | + if ($application_pool) { |
| 103 | + if ($application_pool -ne $application.applicationPool) { |
| 104 | + Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode |
| 105 | + $module.Result.changed = $true |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + # Change username and password if needed |
| 110 | + $app_user = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' |
| 111 | + $app_pass = Get-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' |
| 112 | + if ($connect_as -eq 'pass_through') { |
| 113 | + if ($app_user -ne '') { |
| 114 | + Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -WhatIf:$check_mode |
| 115 | + $module.Result.changed = $true |
| 116 | + } |
| 117 | + if ($app_pass -ne '') { |
| 118 | + Clear-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -WhatIf:$check_mode |
| 119 | + $module.Result.changed = $true |
| 120 | + } |
| 121 | + } |
| 122 | + elseif ($connect_as -eq 'specific_user') { |
| 123 | + if ($app_user -ne $username) { |
| 124 | + Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'userName' -Value $username -WhatIf:$check_mode |
| 125 | + $module.Result.changed = $true |
| 126 | + } |
| 127 | + if ($app_pass -ne $password) { |
| 128 | + Set-ItemProperty -LiteralPath "IIS:\Sites\$($site)\$($name)" -Name 'password' -Value $password -WhatIf:$check_mode |
| 129 | + $module.Result.changed = $true |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +catch { |
| 135 | + $module.FailJson($_.Exception.Message, $_) |
| 136 | +} |
| 137 | + |
| 138 | +$module.ExitJson() |
0 commit comments