Skip to content

Commit 8f6bf9e

Browse files
committed
Migrate win_iis_webapplication module to new microsoft.iis repository
1 parent 7035882 commit 8f6bf9e

File tree

7 files changed

+402
-0
lines changed

7 files changed

+402
-0
lines changed

plugins/modules/web_application.ps1

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

plugins/modules/web_application.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
options:
11+
name:
12+
description:
13+
- Name of the web application.
14+
type: str
15+
required: true
16+
site:
17+
description:
18+
- Name of the site on which the application is created.
19+
type: str
20+
required: true
21+
state:
22+
description:
23+
- State of the web application.
24+
type: str
25+
choices:
26+
- absent
27+
- present
28+
default: present
29+
physical_path:
30+
description:
31+
- The physical path on the remote host to use for the new application.
32+
- The specified folder must already exist.
33+
type: str
34+
aliases:
35+
- path
36+
application_pool:
37+
description:
38+
- The application pool in which the new site executes.
39+
- If not specified, the application pool of the current website will be used.
40+
type: str
41+
connect_as:
42+
description:
43+
- The type of authentication to use for this application. Either C(pass_through) or C(specific_user).
44+
- If C(pass_through), IIS will use the identity of the user or application pool identity to access the file system or network.
45+
- If C(specific_user), IIS will use the credentials provided in I(username) and I(password) to access the file system or network.
46+
type: str
47+
choices:
48+
- pass_through
49+
- specific_user
50+
username:
51+
description:
52+
- Specifies the user name of an account that can access configuration files and content for this application.
53+
- Required when I(connect_as) is set to C(specific_user).
54+
type: str
55+
password:
56+
description:
57+
- The password associated with I(username).
58+
- Required when I(connect_as) is set to C(specific_user).
59+
type: str
60+
seealso:
61+
- module: microsoft.iis.win_iis_virtualdirectory
62+
- module: microsoft.iis.win_iis_webapppool
63+
- module: microsoft.iis.win_iis_webbinding
64+
- module: microsoft.iis.win_iis_website
65+
author:
66+
- Henrik Wallström (@henrikwallstrom)
67+
68+
EXAMPLES: |
69+
- name: Add ACME webapplication on IIS.
70+
microsoft.iis.web_application:
71+
name: api
72+
site: acme
73+
state: present
74+
physical_path: C:\apps\acme\api
75+
76+
RETURN: {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
windows
2+
shippable/windows/group4
3+
# unstable - Random IIS configuration errors
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
test_app_name: TestApp
3+
4+
test_site_name: 'Test Site'
5+
6+
test_user: testuser
7+
test_password: testpass
8+
9+
test_physical_path: "{{ remote_tmp_dir }}"
10+
test_apppool: 'testapppool'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
dependencies:
3+
- setup_remote_tmp_dir
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
- name: check if we can run the tests
3+
ansible.windows.win_shell: |
4+
$osVersion = [Version](Get-Item -LiteralPath "$env:SystemRoot\System32\kernel32.dll").VersionInfo.ProductVersion
5+
$osVersion -ge [Version]"6.2"
6+
register: run_test
7+
changed_when: False
8+
9+
- name: Run on Server 2012 and higher
10+
when: run_test.stdout | trim | bool
11+
block:
12+
- name: ensure IIS features are installed
13+
ansible.windows.win_feature:
14+
name: Web-Server
15+
state: present
16+
include_management_tools: True
17+
register: feature_install
18+
19+
- name: reboot after feature install
20+
ansible.windows.win_reboot:
21+
when: feature_install.reboot_required
22+
23+
# may be possible that copy corrupts the file
24+
- name: Get iis configuration checksum
25+
ansible.windows.win_stat:
26+
path: C:\Windows\System32\inetsrv\config\applicationHost.config
27+
checksum_algorithm: sha1
28+
register: stat_result
29+
30+
- name: take a copy of the original iis configuration
31+
ansible.windows.win_copy:
32+
src: C:\Windows\System32\inetsrv\config\applicationHost.config
33+
dest: '{{ remote_tmp_dir }}\applicationHost.config'
34+
remote_src: yes
35+
register: copy_result
36+
37+
- assert:
38+
that:
39+
- "stat_result.stat.checksum == copy_result.checksum"
40+
41+
# Tests
42+
- name: run tests on hosts that support it
43+
include_tasks: tests.yml
44+
45+
always:
46+
# Cleanup
47+
- name: remove test application
48+
web_application:
49+
state: absent
50+
site: "{{ test_site_name }}"
51+
name: "{{ test_app_name }}"
52+
53+
- name: remove test application pool
54+
community.windows.win_iis_webapppool:
55+
name: "{{ test_apppool }}"
56+
state: absent
57+
58+
- name: remove test site
59+
win_iis_website:
60+
name: "{{ test_site_name }}"
61+
state: absent
62+
63+
- name: restore iis configuration
64+
ansible.windows.win_copy:
65+
src: '{{ remote_tmp_dir }}\applicationHost.config'
66+
dest: C:\Windows\System32\inetsrv\config\applicationHost.config
67+
remote_src: yes
68+
register: copy_result
69+
70+
- assert:
71+
that:
72+
- "stat_result.stat.checksum == copy_result.checksum"
73+
74+
- name: remove IIS feature if it was installed
75+
ansible.windows.win_feature:
76+
name: Web-Server
77+
state: absent
78+
include_management_tools: True
79+
when: feature_install is changed
80+
register: feature_uninstall
81+
82+
- name: reboot after removing IIS features
83+
ansible.windows.win_reboot:
84+
when: feature_uninstall.reboot_required | default(False)

0 commit comments

Comments
 (0)