win_optional_feature - support multiple feature in name (#54368)
This commit is contained in:
parent
9e6b6385e8
commit
9e93a84429
4 changed files with 135 additions and 38 deletions
|
@ -7,7 +7,7 @@
|
|||
|
||||
$spec = @{
|
||||
options = @{
|
||||
name = @{ type = "str"; required = $true }
|
||||
name = @{ type = "list"; required = $true }
|
||||
state = @{ type = "str"; default = "present"; choices = @("absent", "present") }
|
||||
source = @{ type = "str" }
|
||||
include_parent = @{ type = "bool"; default = $false }
|
||||
|
@ -28,16 +28,31 @@ if (-not (Get-Command -Name Enable-WindowsOptionalFeature -ErrorAction SilentlyC
|
|||
$module.FailJson("This version of Windows does not support the Enable-WindowsOptionalFeature.")
|
||||
}
|
||||
|
||||
$feature_state_start = Get-WindowsOptionalFeature -Online -FeatureName $name
|
||||
$changed_features = [System.Collections.Generic.List`1[String]]@()
|
||||
foreach ($feature_name in $name) {
|
||||
try {
|
||||
$feature_state_start = Get-WindowsOptionalFeature -Online -FeatureName $feature_name
|
||||
} catch [System.Runtime.InteropServices.COMException] {
|
||||
# Server 2012 raises a COMException and doesn't return $null even with -ErrorAction SilentlyContinue
|
||||
$feature_state_start = $null
|
||||
}
|
||||
if (-not $feature_state_start) {
|
||||
$module.FailJson("Failed to find feature '$name'")
|
||||
$module.FailJson("Failed to find feature '$feature_name'")
|
||||
}
|
||||
|
||||
if ($state -eq "present") {
|
||||
if ($state -eq "present" -and $feature_state_start.State -notlike "Enabled*") {
|
||||
# Matches for "Enabled" and "EnabledPending"
|
||||
if ($feature_state_start.State -notlike "Enabled*") {
|
||||
$changed_features.Add($feature_name)
|
||||
} elseif ($state -eq "absent" -and $feature_state_start.State -notlike "Disabled*") {
|
||||
# Matches for Disabled, DisabledPending, and DisabledWithPayloadRemoved
|
||||
$changed_features.Add($feature_name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ($state -eq "present" -and $changed_features.Count -gt 0) {
|
||||
$install_args = @{
|
||||
FeatureName = $name
|
||||
FeatureName = $changed_features
|
||||
All = $include_parent
|
||||
}
|
||||
|
||||
|
@ -53,12 +68,9 @@ if ($state -eq "present") {
|
|||
$module.Result.reboot_required = $action_result.RestartNeeded
|
||||
}
|
||||
$module.Result.changed = $true
|
||||
}
|
||||
} else {
|
||||
# Matches for Disabled, DisabledPending, and DisabledWithPayloadRemoved
|
||||
if ($feature_state_start.State -notlike "Disabled*") {
|
||||
} elseif ($state -eq "absent" -and $changed_features.Count -gt 0) {
|
||||
$remove_args = @{
|
||||
FeatureName = $name
|
||||
FeatureName = $changed_features
|
||||
}
|
||||
|
||||
if (-not $module.CheckMode) {
|
||||
|
@ -67,5 +79,5 @@ if ($state -eq "present") {
|
|||
}
|
||||
$module.Result.changed = $true
|
||||
}
|
||||
}
|
||||
$module.ExitJson()
|
||||
|
||||
|
|
|
@ -22,10 +22,10 @@ description:
|
|||
options:
|
||||
name:
|
||||
description:
|
||||
- The name of the feature to install.
|
||||
- The name(s) of the feature to install.
|
||||
- This relates to C(FeatureName) in the Powershell cmdlet.
|
||||
- To list all available features use the PowerShell command C(Get-WindowsOptionalFeature).
|
||||
type: str
|
||||
type: list
|
||||
required: yes
|
||||
state:
|
||||
description:
|
||||
|
@ -72,6 +72,13 @@ EXAMPLES = r'''
|
|||
- name: Reboot if installing Linux Subsytem as feature requires it
|
||||
win_reboot:
|
||||
when: wsl_status.reboot_required
|
||||
|
||||
- name: Install multiple features in one task
|
||||
win_optional_feature:
|
||||
name:
|
||||
- NetFx3
|
||||
- Microsoft-Windows-Subsystem-Linux
|
||||
state: present
|
||||
'''
|
||||
|
||||
RETURN = r'''
|
||||
|
|
|
@ -21,5 +21,20 @@
|
|||
register: run_tests
|
||||
|
||||
- name: run tests
|
||||
include_tasks: tests.yml
|
||||
when: run_tests.stdout | trim | bool
|
||||
block:
|
||||
- name: ensure we start test with removed features
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: absent
|
||||
- include_tasks: tests.yml
|
||||
|
||||
always:
|
||||
- name: make sure test features have been removed after test
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: absent
|
||||
|
|
|
@ -16,6 +16,15 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
- name: fail with invalid feature name
|
||||
win_optional_feature:
|
||||
name:
|
||||
- TelnetClient
|
||||
- FakeFeature
|
||||
state: present
|
||||
register: invalid_name
|
||||
failed_when: invalid_name.msg != "Failed to find feature 'FakeFeature'"
|
||||
|
||||
- name: run with check_mode
|
||||
win_optional_feature:
|
||||
name: TelnetClient
|
||||
|
@ -53,6 +62,34 @@
|
|||
that:
|
||||
- not real_feature_check.changed
|
||||
|
||||
- name: install feature with list
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: present
|
||||
include_parent: true
|
||||
register: install_list
|
||||
|
||||
- name: assert install feature with list
|
||||
assert:
|
||||
that:
|
||||
- install_list is changed
|
||||
|
||||
- name: install feature with list (idempotent)
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: present
|
||||
include_parent: true
|
||||
register: install_list_again
|
||||
|
||||
- name: assert install feature with list (idempotent)
|
||||
assert:
|
||||
that:
|
||||
- not install_list_again is changed
|
||||
|
||||
- name: removal run with check_mode
|
||||
win_optional_feature:
|
||||
name: TelnetClient
|
||||
|
@ -86,3 +123,29 @@
|
|||
assert:
|
||||
that:
|
||||
- not real_feature_check.changed
|
||||
|
||||
- name: remove feature with list
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: absent
|
||||
register: remove_feature_list
|
||||
|
||||
- name: assert remove feature with list
|
||||
assert:
|
||||
that:
|
||||
- remove_feature_list is changed
|
||||
|
||||
- name: remove feature with list (idempotent)
|
||||
win_optional_feature:
|
||||
name:
|
||||
- SimpleTCP
|
||||
- TelnetClient
|
||||
state: absent
|
||||
register: remove_feature_list_again
|
||||
|
||||
- name: assert remove feature with list (idempotent)
|
||||
assert:
|
||||
that:
|
||||
- not remove_feature_list_again is changed
|
||||
|
|
Loading…
Reference in a new issue