2014-09-26 01:01:01 +00:00
#!powershell
2018-02-22 01:33:48 +00:00
# This file is part of Ansible
# 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 01:01:01 +00:00
2017-03-15 02:01:03 +00:00
$result = @ {
2014-09-26 01:01:01 +00:00
changed = $false
}
2017-03-15 02:01:03 +00: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 18:36:32 +00:00
2018-02-22 01:33:48 +00:00
$name = Get-AnsibleParam -obj $params -name " name " -type " list " -failifempty $true
2017-03-15 02:01:03 +00:00
$state = Get-AnsibleParam -obj $params -name " state " -type " str " -default " present " -validateset " present " , " absent "
2017-05-03 22:52:08 +00:00
# DEPRECATED 2.4, potential removal in 2.6+
2017-03-15 02:01:03 +00:00
$restart = Get-AnsibleParam -obj $params -name " restart " -type " bool " -default $false
2018-02-22 01:33:48 +00: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 02:01:03 +00:00
$source = Get-AnsibleParam -obj $params -name " source " -type " str "
2014-09-26 01:01:01 +00:00
2017-05-03 22:52:08 +00:00
If ( $restart ) {
Add-DeprecationWarning -obj $result -message " The 'restart' parameter causes instability. Use the 'win_reboot' action conditionally on 'reboot_required' return value instead " -version 2.6
}
2014-09-26 01:01:01 +00:00
2018-02-22 01:33:48 +00: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 01:56:24 +00:00
}
2018-02-22 01:33:48 +00:00
if ( $state -eq " present " ) {
$install_args = @ {
2017-03-15 02:01:03 +00:00
Name = $name
Restart = $restart
2018-02-22 01:33:48 +00:00
IncludeAllSubFeature = $include_sub_features
2017-03-15 02:01:03 +00:00
WhatIf = $check_mode
2018-02-22 01:33:48 +00:00
ErrorAction = " Stop "
2016-01-07 18:36:32 +00:00
}
2017-03-15 02:01:03 +00:00
2018-02-22 01:33:48 +00:00
if ( $install_cmdlet ) {
$install_args . IncludeManagementTools = $include_management_tools
$install_args . Confirm = $false
2017-01-27 01:56:24 +00:00
if ( $source ) {
2017-03-15 02:01:03 +00:00
if ( -not ( Test-Path -Path $source ) ) {
2018-02-22 01:33:48 +00:00
Fail-Json -obj $result -message " Failed to find source path $source for feature install "
2016-01-07 18:36:32 +00:00
}
2018-02-22 01:33:48 +00:00
$install_args . Source = $source
2015-09-17 18:44:36 +00:00
}
2014-09-26 01:01:01 +00:00
}
2017-03-15 02:01:03 +00:00
2017-01-27 01:56:24 +00:00
try {
2018-02-22 01:33:48 +00:00
$action_results = Install-AnsibleWindowsFeature @install_args
} catch {
Fail-Json -obj $result -message " Failed to install Windows Feature: $( $_ . Exception . Message ) "
2014-09-26 01:01:01 +00:00
}
2018-02-22 01:33:48 +00:00
} else {
$uninstall_args = @ {
2017-03-15 02:01:03 +00:00
Name = $name
Restart = $restart
WhatIf = $check_mode
2018-02-22 01:33:48 +00:00
ErrorAction = " Stop "
}
if ( $install_cmdlet ) {
$uninstall_args . IncludeManagementTools = $include_management_tools
2017-01-27 01:56:24 +00:00
}
2017-03-15 02:01:03 +00:00
try {
2018-02-22 01:33:48 +00:00
$action_results = Uninstall-AnsibleWindowsFeature @uninstall_args
} catch {
Fail-Json -obj $result -message " Failed to uninstall Windows Feature: $( $_ . Exception . Message ) "
2014-09-26 01:01:01 +00:00
}
}
# Loop through results and create a hash containing details about
# each role/feature that is installed/removed
2018-02-22 01:33:48 +00: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 01:01:01 +00:00
}
}
2018-02-22 01:33:48 +00: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 01:01:01 +00:00
$result . changed = $true
}
2018-02-22 01:33:48 +00: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-03 22:52:08 +00:00
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
$result . restart_needed = $result . reboot_required
2015-06-22 08:06:48 +00:00
2018-02-22 01:33:48 +00:00
Exit-Json -obj $result