diff --git a/chocolatey/plugins/modules/win_chocolatey_feature.ps1 b/chocolatey/plugins/modules/win_chocolatey_feature.ps1 index b73ce91..5e51390 100644 --- a/chocolatey/plugins/modules/win_chocolatey_feature.ps1 +++ b/chocolatey/plugins/modules/win_chocolatey_feature.ps1 @@ -24,12 +24,19 @@ param() $ErrorActionPreference = "Stop" # Documentation: https://docs.ansible.com/ansible/2.10/dev_guide/developing_modules_general_windows.html#windows-new-module-development +$ValidStates = @("disabled", "enabled") function Get-ModuleSpec { @{ options = @{ name = @{ type = "str"; required = $true } - state = @{ type = "str"; default = "enabled"; choices = "disabled", "enabled" } + state = @{ type = "str"; default = "enabled"; choices = $ValidStates } + features = @{ type = "dict" } } + mutually_exclusive = @( + , @("features", "name") + , @("feature", "state") + ) + required_one_of = @(, "name", "features") supports_check_mode = $true } } @@ -39,26 +46,41 @@ $spec = Get-ModuleSpec $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) Set-ActiveModule $module -$name = $module.Params.name -$state = $module.Params.state +$FeaturesToSet = if ($module.Params.features) { + $module.Params.features +} else { + @{ + $module.Params.name = $module.Params.state + } +} $chocoCommand = Get-ChocolateyCommand $featureStates = Get-ChocolateyFeature -ChocoCommand $chocoCommand -if ($name -notin $featureStates.Keys) { - $message = "Invalid feature name '$name' specified, valid features are: $($featureStates.Keys -join ', ')" - Assert-TaskFailed -Message $message -} +foreach ($Feature in $FeaturesToSet.GetEnumerator()) { + $name = $Feature.Key + $state = $Feature.Value -$shouldBeEnabled = $state -eq "enabled" -$isEnabled = $featureStates.$name + if ($name -notin $featureStates.Keys) { + $message = "Invalid feature name '$name' specified, valid features are: $($featureStates.Keys -join ', ')" + Assert-TaskFailed -Message $message + } -if ($isEnabled -ne $shouldBeEnabled) { - if (-not $module.CheckMode) { - Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled + if ($state -notin $ValidStates) { + $message = "Invalid state specified for feature '$name', valid states are: $($ValidStates -join ', ')" + Assert-TaskFailed -Message $message } - $module.Result.changed = $true + $shouldBeEnabled = $state -eq "enabled" + $isEnabled = $featureStates.$name + + if ($isEnabled -ne $shouldBeEnabled) { + if (-not $module.CheckMode) { + Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled + } + + $module.Result.changed = $true + } } $module.ExitJson() diff --git a/chocolatey/plugins/modules/win_chocolatey_feature.py b/chocolatey/plugins/modules/win_chocolatey_feature.py index c675044..28ba5c6 100644 --- a/chocolatey/plugins/modules/win_chocolatey_feature.py +++ b/chocolatey/plugins/modules/win_chocolatey_feature.py @@ -32,6 +32,13 @@ type: str choices: [ disabled, enabled ] default: enabled + features: + description: + - A dictionary of multiple features to enable or disable at once. + - Not valid when I(name) is set. + - Features will be set to C(enabled) or C(disabled), as in I(state). + type: dict + version_added: '' seealso: - module: win_chocolatey - module: win_chocolatey_config @@ -51,6 +58,12 @@ win_chocolatey_feature: name: stopOnFirstPackageFailure state: enabled + +- name: Set Multiple Chocolatey Features + win_chocolatey_feature: + features: + checksumFiles: disabled + stopOnFirstPackageFailure: enabled ''' RETURN = r''' diff --git a/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml b/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml index 94bbb7a..0a250b6 100644 --- a/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml +++ b/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml @@ -93,3 +93,4 @@ assert: that: - not disable_again is changed + \ No newline at end of file