diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f659b1..efe6fde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ and [human-readable changelog](https://keepachangelog.com/en/1.0.0/). ## master +## 0.0.6 + +### Added + +- Add module win_capability + ## 0.0.5 ### Changed diff --git a/galaxy.yml b/galaxy.yml index 2eb8e24..1e679d2 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -1,13 +1,14 @@ --- namespace: 'sbaerlocher' name: 'windows' -version: 0.0.5 +version: 0.0.6 readme: README.md authors: - 'Simon Baerlocher (https://sbaerlocher.ch)' description: 'Ansible Collection for Windows functions.' license: - MIT +license_file: 'LICENSE' tags: - windows dependencies: {} diff --git a/plugins/modules/win_capability.ps1 b/plugins/modules/win_capability.ps1 new file mode 100644 index 0000000..135cb3a --- /dev/null +++ b/plugins/modules/win_capability.ps1 @@ -0,0 +1,55 @@ +#!powershell + +# Copyright: (c) 2020, Simon Baerlocher +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +#AnsibleRequires -CSharpUtil Ansible.Basic + +$spec = @{ + options = @{ + name = @{ type = "list"; elements = "str"; required = $true } + state = @{ type = "str"; default = "present"; choices = @("absent", "present") } + } + supports_check_mode = $true +} + +$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) + +$name = $module.Params.name +$state = $module.Params.state + +$module.Result.reboot_required = $false + +if (-not (Get-Command -Name Get-WindowsCapability -ErrorAction SilentlyContinue)) { + $module.FailJson("This version of Windows does not support the Get-WindowsCapability.") +} + + +foreach ($capability_name in $name) { + try { + $capability_state_start = Get-WindowsCapability -Online -Name $capability_name + } + catch [System.Runtime.InteropServices.COMException] { + $capability_state_start = $null + } + if (-not $capability_state_start) { + $module.FailJson("Failed to find Capability '$capability_name'") + } + + if ($state -eq "present" -and $capability_state_start.State -like "*NotPresent*") { + if (-not $module.CheckMode) { + Add-WindowsCapability -Online -Name $capability_name + #$module.Result.reboot_required = $action_result.RestartNeeded + } + $module.Result.changed = $true + } + elseif ($state -eq "absent" -and $capability_state_start.State -notlike "NotPresent") { + if (-not $module.CheckMode) { + $action_result = Remove-WindowsCapability -Online -Name $capability_name + $module.Result.reboot_required = $action_result.RestartNeeded + } + $module.Result.changed = $true + } +} + +$module.ExitJson()