Skip to content

Commit

Permalink
Migrate win_iis_webapplication module to new microsoft.iis repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ronger4 committed Jan 22, 2025
1 parent 290cc06 commit b6de23e
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 1,073 deletions.
25 changes: 14 additions & 11 deletions plugins/modules/web_application.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ $spec = @{
state = @{ type = "str"; default = "present"; choices = "absent", "present" }
physical_path = @{ type = "str"; aliases = @("path") }
application_pool = @{ type = "str" }
connect_as = @{ type = "str"; default = "pass_through"; choices = "specific_user", "pass_through" }
connect_as = @{ type = "str"; choices = "specific_user", "pass_through" }
username = @{ type = "str" }
password = @{ type = "str"; no_log = $true }
}
required_if = @(
, @("connect_as", "specific_user", @("username", "password"))
)
supports_check_mode = $true
}

Expand All @@ -33,6 +30,15 @@ $username = $module.Params.username
$password = $module.Params.password
$check_mode = $module.CheckMode

if ($connect_as -eq 'specific_user') {
if (-not $username) {
$module.FailJson("missing required arguments: username")
}
if (-not $password) {
$module.FailJson("missing required arguments: password")
}
}

# Ensure WebAdministration module is loaded
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
Import-Module WebAdministration
Expand All @@ -51,20 +57,17 @@ try {
# Add application
if (($state -eq 'present') -and (-not $application)) {
if (-not $physical_path) {
$module.FailJson("missing required arguments: path")
$module.FailJson("missing required arguments: physical_path")
}
if (-not (Test-Path -LiteralPath $physical_path)) {
$module.FailJson("specified folder must already exist: path")
$module.FailJson("specified folder must already exist: '$physical_path'")
}

$application_parameters = @{
Name = $name
PhysicalPath = $physical_path
Site = $site
}

if ($application_pool) {
$application_parameters.ApplicationPool = $application_pool
ApplicationPool = $application_pool
}

if (-not $check_mode) {
Expand All @@ -85,7 +88,7 @@ try {
# Change Physical Path if needed
if ($physical_path) {
if (-not (Test-Path -LiteralPath $physical_path)) {
$module.FailJson("specified folder must already exist: path")
$module.FailJson("specified folder must already exist: '$physical_path'")
}

$folder = Get-Item -LiteralPath $physical_path
Expand Down
28 changes: 23 additions & 5 deletions plugins/modules/web_application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ DOCUMENTATION:
short_description: Configures IIS web applications
description: >
Creates, removes, and configures IIS web applications.
requirements:
- C(IISAdministration) PowerShell module
options:
name:
description:
Expand All @@ -21,6 +23,8 @@ DOCUMENTATION:
state:
description:
- State of the web application.
- If C(absent), the web application will be removed.
- If C(present), the web application will be created if not already present.
type: str
choices:
- absent
Expand All @@ -43,6 +47,7 @@ DOCUMENTATION:
- The type of authentication to use for this application. Either C(pass_through) or C(specific_user).
- If C(pass_through), IIS will use the identity of the user or application pool identity to access the file system or network.
- If C(specific_user), IIS will use the credentials provided in I(username) and I(password) to access the file system or network.
- If not specified, the default is C(pass_through) and an existing application will not be modified.
type: str
choices:
- pass_through
Expand All @@ -58,19 +63,32 @@ DOCUMENTATION:
- Required when I(connect_as) is set to C(specific_user).
type: str
seealso:
- module: microsoft.iis.win_iis_virtualdirectory
- module: microsoft.iis.win_iis_webapppool
- module: microsoft.iis.win_iis_webbinding
- module: microsoft.iis.win_iis_website
- module: microsoft.iis.web_app_pool
- module: microsoft.iis.website
- module: microsoft.iis.web_application_info
author:
- Henrik Wallström (@henrikwallstrom)

EXAMPLES: |
- name: Add ACME webapplication on IIS.
- name: Add ACME web application on IIS.
microsoft.iis.web_application:
name: api
site: acme
state: present
physical_path: C:\apps\acme\api
- name: Change connect_as to be specific user.
microsoft.iis.web_application:
name: api
site: acme
connect_as: specific_user
username: acmeuser
password: acmepassword
- name: Delete ACME web application on IIS.
microsoft.iis.web_application:
state: absent
name: api
site: acme
RETURN: {}
102 changes: 102 additions & 0 deletions plugins/modules/web_application_info.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!powershell

# Copyright: (c) 2024, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


#AnsibleRequires -CSharpUtil Ansible.Basic

function Get-ConnectAsInfo {
param (
[string] $site,
[string] $appName
)

# Construct the IIS path
$appPath = "IIS:\Sites\$($site)\$($appName)"

# Get the properties of the web application or virtual directory
$appProperties = Get-ItemProperty -LiteralPath $appPath

# Determine the Connect-As mode
if ($appProperties.userName -and $appProperties.userName -ne "") {
$connect_as = "specific_user"
$username = $appProperties.userName
}
else {
$connect_as = "pass_through"
$username = ""
}
return @{
connect_as = $connect_as
username = $username
}
}
$spec = @{
options = @{
name = @{ type = "str" }
site = @{ type = "str" }
}
supports_check_mode = $true
}

$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$name = $module.Params.name
$site = $module.Params.site

$module.Result.exists = $false
$module.Result.applications = @()

try {
# Ensure WebAdministration module is loaded
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
Import-Module WebAdministration
}
}
catch {
$module.FailJson("Failed to load WebAdministration module, Exception: $($_.Exception.Message)", $_)
}

try {
$getParams = @{}
if ($name) {
$getParams.Name = $name
}
if ($site) {
$getParams.Site = $site
}
$applications = Get-WebApplication @getParams
}
catch {
$module.FailJson("Failed to get web applications, Exception: $($_.Exception.Message)", $_)
}
if ($null -ne $applications) {
$module.Result.exists = $true
}

try {
$module.Result.applications = @(
foreach ($application in $applications) {
# Get site name from the application object
$site_name = $application.GetParentElement().Attributes["name"].Value
$app_name = $application.Path.TrimStart('/')

# Fetch Connect-As information once
$connectAsInfo = Get-ConnectAsInfo -site $site_name -appName $app_name
@{
name = $app_name
site = $site_name
connect_as = $connectAsInfo.connect_as
username = $connectAsInfo.username
application_pool = $application.ApplicationPool
physical_path = $application.PhysicalPath
enabled_protocols = $application.EnabledProtocols
}
}
)
}
catch {
$module.FailJson("Failed to get application details, Exception: $($_.Exception.Message)", $_)
}

$module.ExitJson()
120 changes: 120 additions & 0 deletions plugins/modules/web_application_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
# Copyright: (c) 2024, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

DOCUMENTATION:
module: web_application_info
short_description: Get information on IIS Web Applications
description:
- Returns information about IIS web applications.
requirements:
- C(IISAdministration) PowerShell module
options:
name:
description:
- Name of the web application.
- When not specified, information of all existing applications will be fetched or if site is specified, all applications under the site will be fetched.
type: str
site:
description:
- Name of the site on which the application is created.
- When not specified, information of all existing applications will be fetched unless name is specified.
- Can be used in conjunction with name to fetch information for a specific application when name is not unique.
type: str
seealso:
- module: microsoft.iis.web_app_pool
- module: microsoft.iis.website
- module: microsoft.iis.web_application
author:
- Ron Gershburg (@rgershbu)

EXAMPLES: |
- name: Fetch info for all applications under siteA
web_application_info:
site: SiteA
register: info
- name: Fetch info for web application MyApp
web_application_info:
name: MyApp
register: info
- name: Fetch info for web application MyApp using site and name - Useful when multiple sites have same app name
web_application_info:
name: MyApp
site: SiteA
register: info
- name: Fetch info for all web applications that present in the system
web_application_info:
register: info
RETURN:
exists:
description:
- Whether any applications were found.
returned: success
type: bool
sample: true
applications:
description:
- List of applications found.
returned: success
type: list
elements: dict
sample: '[
{
"application_pool": "testpool",
"enabled_protocols": "http",
"name": "TestAppA",
"physical_path": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\foldera",
"site": "Test Site One"
"connect_as": "pass_through"
"username": ""
},
{
"application_pool": "DefaultAppPool",
"enabled_protocols": "http",
"name": "TestAppB",
"physical_path": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\folderb",
"site": "Test Site Two"
"connect_as": "specific_user"
"username": "testuser"
}
]'
contains:
application_pool:
description:
- The application pool the application is associated with.
type: str
sample: testpool
enabled_protocols:
description:
- The enabled protocols for the application.
type: str
sample: http
name:
description:
- The name of the application.
type: str
sample: TestApp
physical_path:
description:
- The physical path of the application.
type: str
sample: C:\Users\Administrator\AppData\Local\Temp\AppFolder
site:
description:
- The site the application is associated with.
type: str
sample: Test Site One
connect_as:
description:
- The type of authentication to use for this application.
type: str
sample: pass_through
username:
description:
- The username of the account that can access configuration files and content for this application when I(connect_as) is set to C(specific_user).
type: str
sample: testuser
Loading

0 comments on commit b6de23e

Please sign in to comment.