Skip to content

Commit c67e9a9

Browse files
ronger4jborean93colshine1AnsibleJosh
authored
Migrate win_iis_webapplication module to new microsoft.iis repository (#9)
* Migrate win_iis_webapplication module to new microsoft.iis repository * Initial commit * Fix up docs after migration (#95) * Fix up docs after migration * Fix up sanity errors * fix up sanity ignores (#97) * fix up sanity ignores * Bump ansible-windows dep * Fix bad change for win_region * Fix devel sanity checks - ci_complete (#331) * docs(win_iis_webapppool.ps1): update to include example on how to set No Managed Code for .Net compatibility (#556) * Upgrade ansible-lint and fix problems (#565) * Expand connection plugins used in CI (#577) Expands the testing matrix of the Windows connection plugins used in CI to cover all the supported connections of Windows. * fixing portion where building app pools with the word value fails. (#587) * fixing portion where building app pools with the word value fails. * Create 588-win_iis_webapppool.yaml * Migrate win_iis_webapplication module to new microsoft.iis repository --------- Co-authored-by: Jordan Borean <[email protected]> Co-authored-by: Irum Malik <[email protected]> Co-authored-by: AnsibleJosh <[email protected]>
1 parent 56f5a0b commit c67e9a9

File tree

14 files changed

+844
-0
lines changed

14 files changed

+844
-0
lines changed

plugins/modules/web_application.ps1

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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()

plugins/modules/web_application.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
# Copyright: (c) 2024, Ansible Project
3+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4+
5+
DOCUMENTATION:
6+
module: web_application
7+
short_description: Configures IIS web applications
8+
description: >
9+
Creates, removes, and configures IIS web applications.
10+
requirements:
11+
- C(IISAdministration) PowerShell module
12+
options:
13+
name:
14+
description:
15+
- Name of the web application.
16+
type: str
17+
required: true
18+
site:
19+
description:
20+
- Name of the site on which the application is created.
21+
type: str
22+
required: true
23+
state:
24+
description:
25+
- State of the web application.
26+
- If C(absent), the web application will be removed.
27+
- If C(present), the web application will be created if not already present.
28+
type: str
29+
choices:
30+
- absent
31+
- present
32+
default: present
33+
physical_path:
34+
description:
35+
- The physical path on the remote host to use for the new application.
36+
- The specified folder must already exist.
37+
type: str
38+
aliases:
39+
- path
40+
application_pool:
41+
description:
42+
- The application pool in which the new site executes.
43+
- If not specified, the application pool of the current website will be used.
44+
type: str
45+
connect_as:
46+
description:
47+
- The type of authentication to use for this application. Either C(pass_through) or C(specific_user).
48+
- If C(pass_through), IIS will use the identity of the user or application pool identity to access the file system or network.
49+
- If C(specific_user), IIS will use the credentials provided in I(username) and I(password) to access the file system or network.
50+
- If not specified, the default is C(pass_through) and an existing application will not be modified.
51+
type: str
52+
choices:
53+
- pass_through
54+
- specific_user
55+
username:
56+
description:
57+
- Specifies the user name of an account that can access configuration files and content for this application.
58+
- Required when I(connect_as) is set to C(specific_user).
59+
type: str
60+
password:
61+
description:
62+
- The password associated with I(username).
63+
- Required when I(connect_as) is set to C(specific_user).
64+
type: str
65+
seealso:
66+
- module: microsoft.iis.web_app_pool
67+
- module: microsoft.iis.website
68+
- module: microsoft.iis.web_application_info
69+
author:
70+
- Henrik Wallström (@henrikwallstrom)
71+
72+
EXAMPLES: |
73+
- name: Add ACME web application on IIS.
74+
microsoft.iis.web_application:
75+
name: api
76+
site: acme
77+
state: present
78+
physical_path: C:\apps\acme\api
79+
80+
- name: Change connect_as to be specific user.
81+
microsoft.iis.web_application:
82+
name: api
83+
site: acme
84+
connect_as: specific_user
85+
username: acmeuser
86+
password: acmepassword
87+
88+
- name: Delete ACME web application on IIS.
89+
microsoft.iis.web_application:
90+
state: absent
91+
name: api
92+
site: acme
93+
94+
RETURN: {}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!powershell
2+
3+
# Copyright: (c) 2024, Ansible Project
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
7+
#AnsibleRequires -CSharpUtil Ansible.Basic
8+
9+
function Get-ConnectAsInfo {
10+
param (
11+
[string] $site,
12+
[string] $appName
13+
)
14+
15+
# Construct the IIS path
16+
$appPath = "IIS:\Sites\$($site)\$($appName)"
17+
18+
# Get the properties of the web application or virtual directory
19+
$appProperties = Get-ItemProperty -LiteralPath $appPath
20+
21+
# Determine the Connect-As mode
22+
if ($appProperties.userName -and $appProperties.userName -ne "") {
23+
$connect_as = "specific_user"
24+
$username = $appProperties.userName
25+
}
26+
else {
27+
$connect_as = "pass_through"
28+
$username = ""
29+
}
30+
return @{
31+
connect_as = $connect_as
32+
username = $username
33+
}
34+
}
35+
$spec = @{
36+
options = @{
37+
name = @{ type = "str" }
38+
site = @{ type = "str" }
39+
}
40+
supports_check_mode = $true
41+
}
42+
43+
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
44+
$name = $module.Params.name
45+
$site = $module.Params.site
46+
47+
$module.Result.exists = $false
48+
$module.Result.applications = @()
49+
50+
try {
51+
# Ensure WebAdministration module is loaded
52+
if ($null -eq (Get-Module "WebAdministration" -ErrorAction SilentlyContinue)) {
53+
Import-Module WebAdministration
54+
}
55+
}
56+
catch {
57+
$module.FailJson("Failed to load WebAdministration module, Exception: $($_.Exception.Message)", $_)
58+
}
59+
60+
try {
61+
$getParams = @{}
62+
if ($name) {
63+
$getParams.Name = $name
64+
}
65+
if ($site) {
66+
$getParams.Site = $site
67+
}
68+
$applications = Get-WebApplication @getParams
69+
}
70+
catch {
71+
$module.FailJson("Failed to get web applications, Exception: $($_.Exception.Message)", $_)
72+
}
73+
if ($null -ne $applications) {
74+
$module.Result.exists = $true
75+
}
76+
77+
try {
78+
$module.Result.applications = @(
79+
foreach ($application in $applications) {
80+
# Get site name from the application object
81+
$site_name = $application.GetParentElement().Attributes["name"].Value
82+
$app_name = $application.Path.TrimStart('/')
83+
84+
# Fetch Connect-As information once
85+
$connectAsInfo = Get-ConnectAsInfo -site $site_name -appName $app_name
86+
@{
87+
name = $app_name
88+
site = $site_name
89+
connect_as = $connectAsInfo.connect_as
90+
username = $connectAsInfo.username
91+
application_pool = $application.ApplicationPool
92+
physical_path = $application.PhysicalPath
93+
enabled_protocols = $application.EnabledProtocols
94+
}
95+
}
96+
)
97+
}
98+
catch {
99+
$module.FailJson("Failed to get application details, Exception: $($_.Exception.Message)", $_)
100+
}
101+
102+
$module.ExitJson()

0 commit comments

Comments
 (0)