2015-04-24 08:48:02 +00:00
|
|
|
#!powershell
|
|
|
|
|
2018-07-17 21:29:05 +00:00
|
|
|
# Copyright: (c) 2015, Henrik Wallström <henrik@wallstroms.nu>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
2015-04-24 08:48:02 +00:00
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
$params = Parse-Args $args -supports_check_mode $true
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
2015-04-24 08:48:02 +00:00
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
|
|
|
$site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true
|
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
|
|
|
|
$physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" -aliases "path"
|
|
|
|
$application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str"
|
2015-04-24 08:48:02 +00:00
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
$result = @{
|
|
|
|
application_pool = $application_pool
|
|
|
|
changed = $false
|
|
|
|
physical_path = $physical_path
|
2015-04-24 08:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Ensure WebAdministration module is loaded
|
|
|
|
if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) {
|
|
|
|
Import-Module WebAdministration
|
|
|
|
}
|
|
|
|
|
|
|
|
# Application info
|
|
|
|
$application = Get-WebApplication -Site $site -Name $name
|
|
|
|
|
|
|
|
try {
|
|
|
|
# Add application
|
2017-06-28 02:16:06 +00:00
|
|
|
if (($state -eq 'present') -and (-not $application)) {
|
|
|
|
if (-not $physical_path) {
|
|
|
|
Fail-Json $result "missing required arguments: path"
|
2015-04-24 08:48:02 +00:00
|
|
|
}
|
2017-06-28 02:16:06 +00:00
|
|
|
if (-not (Test-Path -Path $physical_path)) {
|
|
|
|
Fail-Json $result "specified folder must already exist: path"
|
2015-04-24 08:48:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
$application_parameters = @{
|
2015-04-24 08:48:02 +00:00
|
|
|
Name = $name
|
|
|
|
PhysicalPath = $physical_path
|
2017-06-28 02:16:06 +00:00
|
|
|
Site = $site
|
|
|
|
}
|
2015-04-24 08:48:02 +00:00
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($application_pool) {
|
2015-04-24 08:48:02 +00:00
|
|
|
$application_parameters.ApplicationPool = $application_pool
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
if (-not $check_mode) {
|
|
|
|
$application = New-WebApplication @application_parameters -Force
|
|
|
|
}
|
2015-04-24 08:48:02 +00:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove application
|
|
|
|
if ($state -eq 'absent' -and $application) {
|
2017-07-28 16:24:08 +00:00
|
|
|
$application = Remove-WebApplication -Site $site -Name $name -WhatIf:$check_mode
|
2015-04-24 08:48:02 +00:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
|
|
|
|
$application = Get-WebApplication -Site $site -Name $name
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($application) {
|
2015-04-24 08:48:02 +00:00
|
|
|
|
|
|
|
# Change Physical Path if needed
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($physical_path) {
|
|
|
|
if (-not (Test-Path -Path $physical_path)) {
|
|
|
|
Fail-Json $result "specified folder must already exist: path"
|
2015-04-24 08:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$app_folder = Get-Item $application.PhysicalPath
|
|
|
|
$folder = Get-Item $physical_path
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($folder.FullName -ne $app_folder.FullName) {
|
|
|
|
Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode
|
2015-04-24 08:48:02 +00:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Change Application Pool if needed
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($application_pool) {
|
|
|
|
if ($application_pool -ne $application.applicationPool) {
|
|
|
|
Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode
|
2015-04-24 08:48:02 +00:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
Fail-Json $result $_.Exception.Message
|
|
|
|
}
|
|
|
|
|
2017-06-28 02:16:06 +00:00
|
|
|
# When in check-mode or on removal, this may fail
|
2015-04-24 08:48:02 +00:00
|
|
|
$application = Get-WebApplication -Site $site -Name $name
|
2017-06-28 02:16:06 +00:00
|
|
|
if ($application) {
|
|
|
|
$result.physical_path = $application.PhysicalPath
|
|
|
|
$result.application_pool = $application.ApplicationPool
|
2015-04-24 08:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Exit-Json $result
|