diff --git a/library/win_winget.ps1 b/library/win_winget.ps1 new file mode 100644 index 0000000..1915df3 --- /dev/null +++ b/library/win_winget.ps1 @@ -0,0 +1,96 @@ +#!powershell + +#AnsibleRequires -CSharpUtil Ansible.Basic +#AnsibleRequires -PowerShell Ansible.ModuleUtils.CommandUtil + +$module = [Ansible.Basic.AnsibleModule]::Create($args, @{ + options = @{ + name = @{ + type = "str" + default = $null + } + state = @{ + type = "str" + default = "present" + choices = ("absent", "latest", "present") + } + } + supports_check_mode = $false +}) + +$name = $module.Params.name +$state = $module.Params.state +$winget = Get-ExecutablePath "winget" +$noPackageString = "No installed package found matching input criteria." + +if ($name -and $id) { + $module.FailJson("name `"$name`" and id `"$id`" must not both be provided") +} + +function Test-Installed { + $command = "`"$winget`" list `"$name`"" + $result = Run-Command -command $command + if ($result.rc -eq 0) { + return $true + } + return $false +} + +function Test-UpgradeAvailable { + if (!(Test-Installed)) { + return $true + } + $command = "`"$winget`" list --upgrade-available `"$name`"" + $result = Run-Command -command $command + if ($result.stdout.Contains($noPackageString)) { + return $false + } + return $true +} + +switch ($state) { + "absent" { + if (Test-Installed) { + $command = "`"$winget`" uninstall `"$name`"" + $result = Run-Command -command $command + if ($result.rc -ne 0) { + $module.Result.rc = $result.rc + $module.Result.stdout = $result.stdout + $module.FailJson("Failed to uninstall package `"$name`"") + } + $module.Result.stdout = $result.stdout + $module.Result.changed = $true + } + } + + "latest" { + if (Test-UpgradeAvailable) { + $command = "`"$winget`" install `"$name`"" + $result = Run-Command -command $command + if ($result.rc -ne 0) { + $module.Result.rc = $result.rc + $module.Result.stdout = $result.stdout + $module.FailJson("Failed to install package `"$name`"") + } + $module.Result.stdout = $result.stdout + $module.Result.changed = $true + } + } + + "present" { + if (!(Test-Installed)) { + $command = "`"$winget`" install `"$name`"" + $result = Run-Command -command $command + if ($result.rc -ne 0) { + $module.Result.rc = $result.rc + $module.Result.stdout = $result.stdout + $module.FailJson("Failed to install package `"$name`"") + } + $module.Result.stdout = $result.stdout + $module.Result.changed = $true + } + } +} + +$module.Result.rc = 0 +$module.ExitJson() diff --git a/library/win_winget.py b/library/win_winget.py new file mode 100644 index 0000000..992960f --- /dev/null +++ b/library/win_winget.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +DOCUMENTATION = ''' +module: win_winget +author: + - "Kenneth Benzie (Benie)" +short_desription: Manages packages with WinGet +description: + - Magage packages using WinGet. +options: + name: + description: + - Name of the package to manage. + type: str + state: + description: + - Indicates the desired package state. V(latest) ensures that the latest + version is installed. + type: str + choices: [ absent, present, latest ] + default: present +''' + +EXAMPLES = ''' +- name: Install Apple Music + win_winget: + name: Apple Music + state: present + +- name: Install latest version of Neovim Qt + win_winget: + name: neovim-qt + state: latest + +- name: Uninstall Microsoft OneDrive + win_winget: + id: Microsoft.OneDrive + state: absent +''' + +RETURN = ''' +stdout: + description: Output from WinGet. + returned: Success, when needed. + type: str + sample: '' +'''