@ -1,25 +1,12 @@
#!powershell
#!powershell
# This file is part of Ansible.
# This file is part of Ansible
#
# Copyright 2014, Paul Durivage <paul.durivage@rackspace.com>
# 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)
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# Requires -Module Ansible.ModuleUtils.Legacy
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
Import-Module -Name ServerManager
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# WANT_JSON
# POWERSHELL_COMMON
Import-Module Servermanager
$result = @ {
$result = @ {
changed = $false
changed = $false
@ -28,141 +15,107 @@ $result = @{
$params = Parse-Args $args -supports_check_mode $true
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
$check_mode = Get-AnsibleParam -obj $params -name " _ansible_check_mode " -type " bool " -default $false
$name = Get-AnsibleParam -obj $params -name " name " -type " str " -failifempty $true
$name = Get-AnsibleParam -obj $params -name " name " -type " li st" -failifempty $true
$state = Get-AnsibleParam -obj $params -name " state " -type " str " -default " present " -validateset " present " , " absent "
$state = Get-AnsibleParam -obj $params -name " state " -type " str " -default " present " -validateset " present " , " absent "
# DEPRECATED 2.4, potential removal in 2.6+
# DEPRECATED 2.4, potential removal in 2.6+
$restart = Get-AnsibleParam -obj $params -name " restart " -type " bool " -default $false
$restart = Get-AnsibleParam -obj $params -name " restart " -type " bool " -default $false
$include subfeatures = Get-AnsibleParam -obj $params -name " include_sub_features " -type " bool " -default $false
$include _ sub_ features = Get-AnsibleParam -obj $params -name " include_sub_features " -type " bool " -default $false
$include managementtools = Get-AnsibleParam -obj $params -name " include_management_tools " -type " bool " -default $false
$include _ management_ tools = Get-AnsibleParam -obj $params -name " include_management_tools " -type " bool " -default $false
$source = Get-AnsibleParam -obj $params -name " source " -type " str "
$source = Get-AnsibleParam -obj $params -name " source " -type " str "
$name = $name -split ',' | % { $_ . Trim ( ) }
If ( $restart ) {
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
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
}
}
# Determine which cmdlets we need to work with. Then we can set options appropriate for the cmdlet
$install_cmdlet = $false
$installWF = $false
if ( Get-Command -Name Install-WindowsFeature -ErrorAction SilentlyContinue ) {
$addWF = $false
Set-Alias -Name Install-AnsibleWindowsFeature -Value Install-WindowsFeature
Set-Alias -Name Uninstall-AnsibleWindowsFeature -Value Uninstall-WindowsFeature
try {
$install_cmdlet = $true
# We can infer uninstall/remove if install/add cmdlets exist
} elseif ( Get-Command -Name Add-WindowsFeature -ErrorAction SilentlyContinue ) {
if ( Get-Command " Install-WindowsFeature " -ErrorAction SilentlyContinue ) {
Set-Alias -Name Install-AnsibleWindowsFeature -Value Add-WindowsFeature
$addCmdlet = " Install-WindowsFeature "
Set-Alias -Name Uninstall-AnsibleWindowsFeature -Value Remove-WindowsFeature
$removeCmdlet = " Uninstall-WindowsFeature "
} else {
$installWF = $true
Fail-Json -obj $result -message " This version of Windows does not support the cmdlets Install-WindowsFeature or Add-WindowsFeature "
}
elseif ( Get-Command " Add-WindowsFeature " -ErrorAction SilentlyContinue ) {
$addCmdlet = " Add-WindowsFeature "
$removeCmdlet = " Remove-WindowsFeature "
$addWF = $true
}
else {
throw [ System.Exception ] " Not supported on this version of Windows "
}
}
}
catch {
Fail-Json $result $_ . Exception . Message
}
If ( $state -eq " present " ) {
# Base params to cover both Add/Install-WindowsFeature
if ( $state -eq " present " ) {
$ InstallParam s = @ {
$install_args = @ {
Name = $name
Name = $name
Restart = $restart
Restart = $restart
IncludeAllSubFeature = $includesubfeatures
IncludeAllSubFeature = $include_sub_features
ErrorAction = " SilentlyContinue "
WhatIf = $check_mode
WhatIf = $check_mode
ErrorAction = " Stop "
}
}
# IncludeManagementTools and source are options only for Install-WindowsFeature
if ( $install_cmdlet ) {
if ( $installWF ) {
$install_args . IncludeManagementTools = $include_management_tools
$install_args . Confirm = $false
if ( $source ) {
if ( $source ) {
if ( -not ( Test-Path -Path $source ) ) {
if ( -not ( Test-Path -Path $source ) ) {
Fail-Json $result " Failed to find source path $source "
Fail-Json -obj $result -message " Failed to find source path $source for feature install "
}
}
$install_args . Source = $source
$InstallParams . add ( " Source " , $source )
}
if ( $IncludeManagementTools ) {
$InstallParams . add ( " IncludeManagementTools " , $includemanagementtools )
}
}
}
}
try {
try {
$featureresult = Invoke-Expression " $addCmdlet @InstallParams "
$action_results = Install-AnsibleWindowsFeature @install_args
}
} catch {
catch {
Fail-Json -obj $result -message " Failed to install Windows Feature: $( $_ . Exception . Message ) "
Fail-Json $result $_ . Exception . Message
}
}
}
} else {
ElseIf ( $state -eq " absent " ) {
$uninstall_args = @ {
$UninstallParams = @ {
Name = $name
Name = $name
Restart = $restart
Restart = $restart
ErrorAction = " SilentlyContinue "
WhatIf = $check_mode
WhatIf = $check_mode
ErrorAction = " Stop "
}
if ( $install_cmdlet ) {
$uninstall_args . IncludeManagementTools = $include_management_tools
}
}
try {
try {
$featureresult = Invoke-Expression " $removeCmdlet @UninstallParams "
$action_results = Uninstall-AnsibleWindowsFeature @uninstall_args
}
} catch {
catch {
Fail-Json -obj $result -message " Failed to uninstall Windows Feature: $( $_ . Exception . Message ) "
Fail-Json $result $_ . Exception . Message
}
}
}
}
# Loop through results and create a hash containing details about
# Loop through results and create a hash containing details about
# each role/feature that is installed/removed
# each role/feature that is installed/removed
$installed_features = @ ( )
# $action_results.FeatureResult is not empty if anything was changed
#$featureresult.featureresult is filled if anything was changed
$feature_results = @ ( )
If ( $featureresult . FeatureResult )
foreach ( $action_result in $action_results . FeatureResult ) {
{
ForEach ( $item in $featureresult . FeatureResult ) {
$message = @ ( )
$message = @ ( )
ForEach ( $msg in $item . Message ) {
foreach ( $msg in $action_result . Message ) {
$message + = @ {
$message + = @ {
message_type = $msg . MessageType . ToString ( )
message_type = $msg . MessageType . ToString ( )
error_code = $msg . ErrorCode
error_code = $msg . ErrorCode
text = $msg . Text
text = $msg . Text
}
}
}
}
$installed_features + = @ {
id = $item . Id
display_name = $item . DisplayName
message = $message
reboot_required = $item . RestartNeeded . ToString ( ) | ConvertTo-Bool
skip_reason = $item . SkipReason . ToString ( )
success = $item . Success . ToString ( ) | ConvertTo-Bool
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
$feature_results + = @ {
restart_needed = $item . RestartNeeded . ToString ( ) | ConvertTo-Bool
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
}
}
$result . changed = $true
$result . changed = $true
}
}
$result . feature_result = $feature_results
$result . feature_result = $installed_features
$result . success = ConvertTo-Bool -obj $action_results . Success
$result . success = ( $featureresult . Success . ToString ( ) | ConvertTo-Bool )
$result . exitcode = $action_results . ExitCode . ToString ( )
$result . exitcode = $featureresult . ExitCode . ToString ( )
$result . reboot_required = ConvertTo-Bool -obj $action_results . RestartNeeded
$result . reboot_required = ( $featureresult . RestartNeeded . ToString ( ) | ConvertTo-Bool )
# controls whether Ansible will fail or not
$result . failed = ( -not $action_results . Success )
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
# DEPRECATED 2.4, potential removal in 2.6+ (standardize naming to "reboot_required")
$result . restart_needed = $result . reboot_required
$result . restart_needed = $result . reboot_required
If ( $result . success ) {
Exit-Json -obj $result
Exit-Json $result
}
ElseIf ( $state -eq " present " ) {
Fail-Json $result " Failed to add feature "
}
Else {
Fail-Json $result " Failed to remove feature "
}