2014-09-26 03:01:01 +02:00
|
|
|
#!powershell
|
2018-02-22 02:33:48 +01:00
|
|
|
|
|
|
|
# Copyright: (c) 2014, Paul Durivage <paul.durivage@rackspace.com>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
#Requires -Module Ansible.ModuleUtils.Legacy
|
|
|
|
|
|
|
|
Import-Module -Name ServerManager
|
2014-09-26 03:01:01 +02:00
|
|
|
|
2017-03-15 03:01:03 +01:00
|
|
|
$result = @{
|
2014-09-26 03:01:01 +02:00
|
|
|
changed = $false
|
|
|
|
}
|
|
|
|
|
2017-03-15 03:01:03 +01:00
|
|
|
$params = Parse-Args $args -supports_check_mode $true
|
|
|
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
2016-01-07 19:36:32 +01:00
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
$name = Get-AnsibleParam -obj $params -name "name" -type "list" -failifempty $true
|
2017-03-15 03:01:03 +01:00
|
|
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent"
|
2017-05-04 00:52:08 +02:00
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
$include_sub_features = Get-AnsibleParam -obj $params -name "include_sub_features" -type "bool" -default $false
|
|
|
|
$include_management_tools = Get-AnsibleParam -obj $params -name "include_management_tools" -type "bool" -default $false
|
2017-03-15 03:01:03 +01:00
|
|
|
$source = Get-AnsibleParam -obj $params -name "source" -type "str"
|
2014-09-26 03:01:01 +02:00
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
$install_cmdlet = $false
|
|
|
|
if (Get-Command -Name Install-WindowsFeature -ErrorAction SilentlyContinue) {
|
|
|
|
Set-Alias -Name Install-AnsibleWindowsFeature -Value Install-WindowsFeature
|
|
|
|
Set-Alias -Name Uninstall-AnsibleWindowsFeature -Value Uninstall-WindowsFeature
|
|
|
|
$install_cmdlet = $true
|
|
|
|
} elseif (Get-Command -Name Add-WindowsFeature -ErrorAction SilentlyContinue) {
|
|
|
|
Set-Alias -Name Install-AnsibleWindowsFeature -Value Add-WindowsFeature
|
|
|
|
Set-Alias -Name Uninstall-AnsibleWindowsFeature -Value Remove-WindowsFeature
|
|
|
|
} else {
|
|
|
|
Fail-Json -obj $result -message "This version of Windows does not support the cmdlets Install-WindowsFeature or Add-WindowsFeature"
|
2017-01-27 02:56:24 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
if ($state -eq "present") {
|
|
|
|
$install_args = @{
|
2017-03-15 03:01:03 +01:00
|
|
|
Name = $name
|
2018-02-22 02:33:48 +01:00
|
|
|
IncludeAllSubFeature = $include_sub_features
|
2018-04-25 22:13:37 +02:00
|
|
|
Restart = $false
|
2017-03-15 03:01:03 +01:00
|
|
|
WhatIf = $check_mode
|
2018-02-22 02:33:48 +01:00
|
|
|
ErrorAction = "Stop"
|
2016-01-07 19:36:32 +01:00
|
|
|
}
|
2017-03-15 03:01:03 +01:00
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
if ($install_cmdlet) {
|
|
|
|
$install_args.IncludeManagementTools = $include_management_tools
|
|
|
|
$install_args.Confirm = $false
|
2017-01-27 02:56:24 +01:00
|
|
|
if ($source) {
|
2017-03-15 03:01:03 +01:00
|
|
|
if (-not (Test-Path -Path $source)) {
|
2018-02-22 02:33:48 +01:00
|
|
|
Fail-Json -obj $result -message "Failed to find source path $source for feature install"
|
2016-01-07 19:36:32 +01:00
|
|
|
}
|
2018-02-22 02:33:48 +01:00
|
|
|
$install_args.Source = $source
|
2015-09-17 20:44:36 +02:00
|
|
|
}
|
2014-09-26 03:01:01 +02:00
|
|
|
}
|
2017-03-15 03:01:03 +01:00
|
|
|
|
2017-01-27 02:56:24 +01:00
|
|
|
try {
|
2018-02-22 02:33:48 +01:00
|
|
|
$action_results = Install-AnsibleWindowsFeature @install_args
|
|
|
|
} catch {
|
|
|
|
Fail-Json -obj $result -message "Failed to install Windows Feature: $($_.Exception.Message)"
|
2014-09-26 03:01:01 +02:00
|
|
|
}
|
2018-02-22 02:33:48 +01:00
|
|
|
} else {
|
|
|
|
$uninstall_args = @{
|
2017-03-15 03:01:03 +01:00
|
|
|
Name = $name
|
2018-04-25 22:13:37 +02:00
|
|
|
Restart = $false
|
2017-03-15 03:01:03 +01:00
|
|
|
WhatIf = $check_mode
|
2018-02-22 02:33:48 +01:00
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
if ($install_cmdlet) {
|
|
|
|
$uninstall_args.IncludeManagementTools = $include_management_tools
|
2017-01-27 02:56:24 +01:00
|
|
|
}
|
2017-03-15 03:01:03 +01:00
|
|
|
|
|
|
|
try {
|
2018-02-22 02:33:48 +01:00
|
|
|
$action_results = Uninstall-AnsibleWindowsFeature @uninstall_args
|
|
|
|
} catch {
|
|
|
|
Fail-Json -obj $result -message "Failed to uninstall Windows Feature: $($_.Exception.Message)"
|
2014-09-26 03:01:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Loop through results and create a hash containing details about
|
|
|
|
# each role/feature that is installed/removed
|
2018-02-22 02:33:48 +01:00
|
|
|
# $action_results.FeatureResult is not empty if anything was changed
|
|
|
|
$feature_results = @()
|
|
|
|
foreach ($action_result in $action_results.FeatureResult) {
|
|
|
|
$message = @()
|
|
|
|
foreach ($msg in $action_result.Message) {
|
|
|
|
$message += @{
|
|
|
|
message_type = $msg.MessageType.ToString()
|
|
|
|
error_code = $msg.ErrorCode
|
|
|
|
text = $msg.Text
|
2014-09-26 03:01:01 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 02:33:48 +01:00
|
|
|
|
|
|
|
$feature_results += @{
|
|
|
|
id = $action_result.Id
|
|
|
|
display_name = $action_result.DisplayName
|
|
|
|
message = $message
|
|
|
|
reboot_required = ConvertTo-Bool -obj $action_result.RestartNeeded
|
|
|
|
skip_reason = $action_result.SkipReason.ToString()
|
|
|
|
success = ConvertTo-Bool -obj $action_result.Success
|
|
|
|
restart_needed = ConvertTo-Bool -obj $action_result.RestartNeeded
|
|
|
|
}
|
2014-09-26 03:01:01 +02:00
|
|
|
$result.changed = $true
|
|
|
|
}
|
2018-02-22 02:33:48 +01:00
|
|
|
$result.feature_result = $feature_results
|
|
|
|
$result.success = ConvertTo-Bool -obj $action_results.Success
|
|
|
|
$result.exitcode = $action_results.ExitCode.ToString()
|
|
|
|
$result.reboot_required = ConvertTo-Bool -obj $action_results.RestartNeeded
|
|
|
|
# controls whether Ansible will fail or not
|
|
|
|
$result.failed = (-not $action_results.Success)
|
2017-05-04 00:52:08 +02:00
|
|
|
|
2018-04-25 22:13:37 +02:00
|
|
|
# DEPRECATED 2.4, remove in 2.8 (standardize naming to "reboot_required")
|
2017-05-04 00:52:08 +02:00
|
|
|
$result.restart_needed = $result.reboot_required
|
2015-06-22 10:06:48 +02:00
|
|
|
|
2018-02-22 02:33:48 +01:00
|
|
|
Exit-Json -obj $result
|