1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

win_iis_webapplication: Parameter fixes and check-mode (#26082)

This PR includes:
- General cleanup of parameters
- Added check-mode support
- Cleanup of return values
This commit is contained in:
Dag Wieers 2017-06-28 04:16:06 +02:00 committed by Jordan Borean
parent 58b348ddf5
commit b1e608811b
2 changed files with 63 additions and 81 deletions

View file

@ -20,100 +20,82 @@
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
$params = Parse-Args $args; $params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
# Name parameter $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
$name = Get-Attr $params "name" $FALSE; $site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true
If ($name -eq $FALSE) { $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
Fail-Json (New-Object psobject) "missing required argument: name"; $physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str" -aliases "path"
$application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str"
$result = @{
application_pool = $application_pool
changed = $false
physical_path = $physical_path
} }
# Site
$site = Get-Attr $params "site" $FALSE;
If ($site -eq $FALSE) {
Fail-Json (New-Object psobject) "missing required argument: site";
}
# State parameter
$state = Get-Attr $params "state" "present";
$state.ToString().ToLower();
If (($state -ne 'present') -and ($state -ne 'absent')) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
}
# Path parameter
$physical_path = Get-Attr $params "physical_path" $FALSE;
# Application Pool Parameter
$application_pool = Get-Attr $params "application_pool" $FALSE;
# Ensure WebAdministration module is loaded # Ensure WebAdministration module is loaded
if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) { if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) {
Import-Module WebAdministration Import-Module WebAdministration
} }
# Result
$result = New-Object psobject @{
application = New-Object psobject
changed = $false
};
# Application info # Application info
$application = Get-WebApplication -Site $site -Name $name $application = Get-WebApplication -Site $site -Name $name
try { try {
# Add application # Add application
If(($state -eq 'present') -and (-not $application)) { if (($state -eq 'present') -and (-not $application)) {
If ($physical_path -eq $FALSE) { if (-not $physical_path) {
Fail-Json (New-Object psobject) "missing required arguments: physical_path" Fail-Json $result "missing required arguments: path"
} }
If (-not (Test-Path $physical_path)) { if (-not (Test-Path -Path $physical_path)) {
Fail-Json (New-Object psobject) "specified folder must already exist: physical_path" Fail-Json $result "specified folder must already exist: path"
} }
$application_parameters = New-Object psobject @{ $application_parameters = @{
Site = $site
Name = $name Name = $name
PhysicalPath = $physical_path PhysicalPath = $physical_path
}; Site = $site
}
If ($application_pool) { if ($application_pool) {
$application_parameters.ApplicationPool = $application_pool $application_parameters.ApplicationPool = $application_pool
} }
$application = New-WebApplication @application_parameters -Force if (-not $check_mode) {
$application = New-WebApplication @application_parameters -Force
}
$result.changed = $true $result.changed = $true
} }
# Remove application # Remove application
if ($state -eq 'absent' -and $application) { if ($state -eq 'absent' -and $application) {
$application = Remove-WebApplication -Site $site -Name $name $application = Remove-WebApplication -Site $site -Name $name $WhatIf:$check_mode
$result.changed = $true $result.changed = $true
} }
$application = Get-WebApplication -Site $site -Name $name $application = Get-WebApplication -Site $site -Name $name
If($application) { if ($application) {
# Change Physical Path if needed # Change Physical Path if needed
if($physical_path) { if ($physical_path) {
If (-not (Test-Path $physical_path)) { if (-not (Test-Path -Path $physical_path)) {
Fail-Json (New-Object psobject) "specified folder must already exist: physical_path" Fail-Json $result "specified folder must already exist: path"
} }
$app_folder = Get-Item $application.PhysicalPath $app_folder = Get-Item $application.PhysicalPath
$folder = Get-Item $physical_path $folder = Get-Item $physical_path
If($folder.FullName -ne $app_folder.FullName) { if ($folder.FullName -ne $app_folder.FullName) {
Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name physicalPath -value $physical_path -WhatIf:$check_mode
$result.changed = $true $result.changed = $true
} }
} }
# Change Application Pool if needed # Change Application Pool if needed
if($application_pool) { if ($application_pool) {
If($application_pool -ne $application.applicationPool) { if ($application_pool -ne $application.applicationPool) {
Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool Set-ItemProperty "IIS:\Sites\$($site)\$($name)" -name applicationPool -value $application_pool -WhatIf:$check_mode
$result.changed = $true $result.changed = $true
} }
} }
@ -122,11 +104,11 @@ try {
Fail-Json $result $_.Exception.Message Fail-Json $result $_.Exception.Message
} }
# Result # When in check-mode or on removal, this may fail
$application = Get-WebApplication -Site $site -Name $name $application = Get-WebApplication -Site $site -Name $name
$result.application = New-Object psobject @{ if ($application) {
PhysicalPath = $application.PhysicalPath $result.physical_path = $application.PhysicalPath
ApplicationPool = $application.applicationPool $result.application_pool = $application.ApplicationPool
} }
Exit-Json $result Exit-Json $result

View file

@ -22,49 +22,36 @@ ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = r''' DOCUMENTATION = r'''
--- ---
module: win_iis_webapplication module: win_iis_webapplication
version_added: "2.0" version_added: "2.0"
short_description: Configures IIS web applications. short_description: Configures IIS web applications
description: description:
- Creates, removes, and configures IIS web applications. - Creates, removes, and configures IIS web applications.
options: options:
name: name:
description: description:
- Name of the web application. - Name of the web application.
required: true required: true
default: null
aliases: []
site: site:
description: description:
- Name of the site on which the application is created. - Name of the site on which the application is created.
required: true required: true
default: null
aliases: []
state: state:
description: description:
- State of the web application. - State of the web application.
choices: choices: [ absent, present ]
- present default: present
- absent
required: false
default: null
aliases: []
physical_path: physical_path:
description: description:
- The physical path on the remote host to use for the new application. The specified folder must already exist. - The physical path on the remote host to use for the new application.
required: false - The specified folder must already exist.
default: null
aliases: []
application_pool: application_pool:
description: description:
- The application pool in which the new site executes. - The application pool in which the new site executes.
required: false author:
default: null - Henrik Wallström
aliases: []
author: Henrik Wallström
''' '''
EXAMPLES = r''' EXAMPLES = r'''
@ -75,3 +62,16 @@ EXAMPLES = r'''
state: present state: present
physical_path: C:\apps\acme\api physical_path: C:\apps\acme\api
''' '''
RETURN = r'''
application_pool:
description: The used/implemented application_pool value
returned: success
type: string
sample: DefaultAppPool
physical_path:
description: The used/implemented physical_path value
returned: success
type: string
sample: C:\apps\acme\api
'''