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 5, 2025
2 parents fa6cdff + f823bb6 commit 68222e3
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 27 deletions.
9 changes: 5 additions & 4 deletions plugins/modules/web_application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ DOCUMENTATION:
- 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.
type: str
default: pass_through
choices:
- pass_through
- specific_user
Expand All @@ -58,10 +59,10 @@ 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: community.windows.win_iis_virtualdirectory
- module: community.windows.win_iis_webapppool
- module: community.windows.win_iis_webbinding
- module: community.windows.win_iis_website
author:
- Henrik Wallström (@henrikwallstrom)

Expand Down
75 changes: 75 additions & 0 deletions plugins/modules/web_application_info.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!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

$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 {
# Application Info
# Get all applications
if ($null -eq $name -and $null -eq $site) {
$applications = Get-WebApplication
}
# Only name was given as input
elseif ($null -ne $name -and $null -eq $site) {
$applications = Get-WebApplication -Name $name
}
# Only site was given as input
elseif ($null -eq $name -and $null -ne $site) {
$applications = Get-WebApplication -Site $site
}
# Both name and site were given as input - handle case where name is not unique
else {
$applications = Get-WebApplication -Site $site -Name $name
}
}
catch {
$module.FailJson("Failed to get web applications, Exception: $($_.Exception.Message)", $_)
}
if ($null -ne $applications) {
$module.Result.exists = $true
}

try {
foreach ($application in $applications) {
$module.Result.applications += @{
name = $application.path.TrimStart('/')
site = $application.GetParentElement().Attributes["name"].Value
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()
102 changes: 102 additions & 0 deletions plugins/modules/web_application_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
# 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.
options:
name:
description:
- Name of the web application.
type: str
site:
description:
- Name of the site on which the application is created.
type: str
seealso:
- module: community.windows.win_iis_virtualdirectory
- module: community.windows.win_iis_webapppool
- module: community.windows.win_iis_webbinding
- module: community.windows.win_iis_website
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"
},
{
"application_pool": "DefaultAppPool",
"enabled_protocols": "http",
"name": "TestAppB",
"physical_path": "C:\\Users\\Administrator\\AppData\\Local\\Temp\\folderb",
"site": "Test Site Two"
}
]'
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
10 changes: 1 addition & 9 deletions tests/integration/targets/web_application/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
---
- name: check if we can run the tests
ansible.windows.win_shell: |
$osVersion = [Version](Get-Item -LiteralPath "$env:SystemRoot\System32\kernel32.dll").VersionInfo.ProductVersion
$osVersion -ge [Version]"6.2"
register: run_test
changed_when: False

- name: Run on Server 2012 and higher
when: run_test.stdout | trim | bool
- name: block to ensure there is a cleanup after the tests
block:
- name: ensure IIS features are installed
ansible.windows.win_feature:
Expand Down
38 changes: 24 additions & 14 deletions tests/integration/targets/web_application/tasks/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
physical_path: "{{ test_physical_path }}"
register: result

- name: Fetch info for test app
web_application_info:
name: "{{ test_app_name }}"
register: test_info

- assert:
that:
- 'result.changed == true'
# - 'result.physical_path == test_physical_path'
- result.changed == true
- test_info.applications[0].physical_path == test_physical_path

- name: create test app (idempotent)
web_application:
Expand All @@ -34,8 +39,7 @@

- assert:
that:
- 'result.changed == false'
# - 'result.physical_path == test_physical_path'
- result.changed == false

- name: set test app credentials
web_application:
Expand All @@ -47,11 +51,15 @@
password: "{{ test_password }}"
register: result

- name: Fetch info for test app creds
web_application_info:
name: "{{ test_app_name }}"
register: test_info

- assert:
that:
- 'result.changed == true'
# - 'result.physical_path == test_physical_path'
# - "result.connect_as == 'specific_user'"
- result.changed == true
- test_info.applications[0].physical_path == test_physical_path

- name: set test app credentials (idempotent)
web_application:
Expand All @@ -65,9 +73,7 @@

- assert:
that:
- 'result.changed == false'
# - 'result.physical_path == test_physical_path'
# - "result.connect_as == 'specific_user'"
- result.changed == false

- name: create new test application pool
community.windows.win_iis_webapppool:
Expand All @@ -83,12 +89,16 @@
application_pool: "{{ test_apppool }}"
register: result

- name: Fetch info test_app with app pool
web_application_info:
name: "{{ test_app_name }}"
register: test_info

- assert:
that:
- 'result.changed == true'
# - 'result.physical_path == test_physical_path'
# - "result.connect_as == 'pass_through'"
# - "result.application_pool == test_apppool"
- result.changed == true
- test_info.applications[0].physical_path == test_physical_path
- test_info.applications[0].application_pool == test_apppool

- name: Verify input failure due to missing username and password when connecting as specific user
web_application:
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/web_application_info/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
windows
shippable/windows/group1
14 changes: 14 additions & 0 deletions tests/integration/targets/web_application_info/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
test_app_name_A: TestAppA
test_app_name_B: TestAppB

test_site_name_A: 'Test Site One'
test_site_name_B: 'Test Site Two'

test_user: testuser
test_password: testpass

test_physical_path_A: "{{ remote_tmp_dir }}"
test_physical_path_B: "{{ remote_tmp_dir }}/folderb"

test_apppool: 'testapppool'
3 changes: 3 additions & 0 deletions tests/integration/targets/web_application_info/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- setup_remote_tmp_dir
Loading

0 comments on commit 68222e3

Please sign in to comment.