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 16, 2025
1 parent d89519b commit bd0440b
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 79 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: 22 additions & 6 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 @@ -44,7 +48,6 @@ 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 @@ -59,19 +62,32 @@ DOCUMENTATION:
- Required when I(connect_as) is set to C(specific_user).
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
- 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: {}
39 changes: 16 additions & 23 deletions plugins/modules/web_application_info.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,14 @@ catch {
}

try {
# Application Info
# Get all applications
if ($null -eq $name -and $null -eq $site) {
$applications = Get-WebApplication
$getParams = @{}
if ($name) {
$getParams.Name = $name
}
# 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
if ($site) {
$getParams.Site = $site
}
$applications = Get-WebApplication @getParams
}
catch {
$module.FailJson("Failed to get web applications, Exception: $($_.Exception.Message)", $_)
Expand All @@ -58,15 +49,17 @@ if ($null -ne $applications) {
}

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
$module.Result.applications = @(
foreach ($application in $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)", $_)
Expand Down
12 changes: 8 additions & 4 deletions plugins/modules/web_application_info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ DOCUMENTATION:
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: community.windows.win_iis_virtualdirectory
- module: community.windows.win_iis_webapppool
- module: community.windows.win_iis_webbinding
- module: community.windows.win_iis_website
- module: microsoft.iis.web_app_pool
- module: microsoft.iis.website
- module: microsoft.iis.web_application
author:
- Ron Gershburg (@rgershbu)

Expand Down
3 changes: 1 addition & 2 deletions tests/integration/targets/web_application/aliases
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
windows
shippable/windows/group4
# unstable - Random IIS configuration errors
shippable/windows/group1
16 changes: 2 additions & 14 deletions tests/integration/targets/web_application/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
name: "{{ test_app_name }}"

- name: remove test application pool
community.windows.win_iis_webapppool:
web_app_pool:
name: "{{ test_apppool }}"
state: absent

- name: remove test site
win_iis_website:
website:
name: "{{ test_site_name }}"
state: absent

Expand All @@ -62,15 +62,3 @@
- assert:
that:
- "stat_result.stat.checksum == copy_result.checksum"

- name: remove IIS feature if it was installed
ansible.windows.win_feature:
name: Web-Server
state: absent
include_management_tools: True
when: feature_install is changed
register: feature_uninstall

- name: reboot after removing IIS features
ansible.windows.win_reboot:
when: feature_uninstall.reboot_required | default(False)
69 changes: 66 additions & 3 deletions tests/integration/targets/web_application/tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name: test site exists, but stopped in case of duplicate web binding
community.windows.win_iis_website:
website:
name: "{{ test_site_name }}"
state: stopped
physical_path: 'C:\inetpub\wwwroot'
Expand All @@ -11,6 +11,25 @@
site: "{{ test_site_name }}"
name: "{{ test_app_name }}"

- name: create test app - check mode
web_application:
state: present
site: "{{ test_site_name }}"
name: "{{ test_app_name }}"
physical_path: "{{ test_physical_path }}"
check_mode: true
register: result

- name: Fetch info for test app - check mode
web_application_info:
name: "{{ test_app_name }}"
register: test_info_check

- assert:
that:
- result.changed == true
- not test_info_check.exists

- name: create test app
web_application:
state: present
Expand Down Expand Up @@ -76,7 +95,7 @@
- result.changed == false

- name: create new test application pool
community.windows.win_iis_webapppool:
web_app_pool:
name: "{{ test_apppool }}"
state: present

Expand Down Expand Up @@ -108,4 +127,48 @@
connect_as: specific_user
application_pool: "{{ test_apppool }}"
register: failure
failed_when: "'connect_as is specific_user but all of the following are missing: username, password' not in failure.msg"
failed_when: "'missing required arguments: username' not in failure.msg"

- name: Remove test app - check mode
web_application:
state: absent
site: "{{ test_site_name }}"
name: "{{ test_app_name }}"
check_mode: true

- name: Fetch info for test app after removal - check mode
web_application_info:
name: "{{ test_app_name }}"
register: test_info_check_removal

- assert:
that:
- result.changed == true
- test_info_check_removal.exists

- name: Remove test app
web_application:
state: absent
site: "{{ test_site_name }}"
name: "{{ test_app_name }}"

- name: Fetch info for test app after removal
web_application_info:
name: "{{ test_app_name }}"
register: test_info_check_removal

- assert:
that:
- result.changed == true
- not test_info_check_removal.exists

- name: Remove test app (idempotent)
web_application:
state: absent
site: "{{ test_site_name }}"
name: "{{ test_app_name }}"
register: result

- assert:
that:
- result.changed == false
18 changes: 3 additions & 15 deletions tests/integration/targets/web_application_info/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@
name: "{{ test_app_name_B }}"

- name: remove test application pool
community.windows.win_iis_webapppool:
web_app_pool:
name: "{{ test_apppool }}"
state: absent

- name: remove test site A
win_iis_website:
website:
name: "{{ test_site_name_A }}"
state: absent

- name: remove test site B
win_iis_website:
website:
name: "{{ test_site_name_B }}"
state: absent

Expand All @@ -87,15 +87,3 @@
- assert:
that:
- "stat_result.stat.checksum == copy_result.checksum"

- name: remove IIS feature if it was installed
ansible.windows.win_feature:
name: Web-Server
state: absent
include_management_tools: True
when: feature_install is changed
register: feature_uninstall

- name: reboot after removing IIS features
ansible.windows.win_reboot:
when: feature_uninstall.reboot_required | default(False)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
test_sites:
- "{{ test_site_name_A }}"
- "{{ test_site_name_B }}"
community.windows.win_iis_website:
website:
name: "{{ item }}"
state: stopped
physical_path: 'C:\inetpub\wwwroot'
Expand Down

0 comments on commit bd0440b

Please sign in to comment.