Add basic win_winget module
This commit is contained in:
parent
5ed3297984
commit
96cf066d74
96
library/win_winget.ps1
Normal file
96
library/win_winget.ps1
Normal file
@ -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()
|
47
library/win_winget.py
Normal file
47
library/win_winget.py
Normal file
@ -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: ''
|
||||||
|
'''
|
Loading…
x
Reference in New Issue
Block a user