mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-08-11 16:44:22 -07:00
win_chocolatey: Clean up parameter handling (#21533)
Changes include: - Use Get-AnsibleParam with -type/-validateset - Replace $result PSObject with normal hash - Deprecate 'upgrade' parameter by using state=latest
This commit is contained in:
parent
78c300412d
commit
b1f6344ecb
3 changed files with 101 additions and 94 deletions
|
@ -20,36 +20,39 @@
|
||||||
# WANT_JSON
|
# WANT_JSON
|
||||||
# POWERSHELL_COMMON
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$result = @{
|
||||||
$result = New-Object PSObject;
|
changed = $false
|
||||||
Set-Attr $result "changed" $false;
|
|
||||||
|
|
||||||
$package = Get-Attr -obj $params -name name -failifempty $true -emptyattributefailmessage "missing required argument: name"
|
|
||||||
$force = Get-Attr -obj $params -name force -default "false" | ConvertTo-Bool
|
|
||||||
$upgrade = Get-Attr -obj $params -name upgrade -default "false" | ConvertTo-Bool
|
|
||||||
$version = Get-Attr -obj $params -name version -default $null
|
|
||||||
|
|
||||||
$source = Get-Attr -obj $params -name source -default $null
|
|
||||||
if ($source) {$source = $source.Tolower()}
|
|
||||||
|
|
||||||
$showlog = Get-Attr -obj $params -name showlog -default "false" | ConvertTo-Bool
|
|
||||||
$state = Get-Attr -obj $params -name state -default "present"
|
|
||||||
|
|
||||||
$installargs = Get-Attr -obj $params -name install_args -default $null
|
|
||||||
$packageparams = Get-Attr -obj $params -name params -default $null
|
|
||||||
$allowemptychecksums = Get-Attr -obj $params -name allow_empty_checksums -default "false" | ConvertTo-Bool
|
|
||||||
$ignorechecksums = Get-Attr -obj $params -name ignore_checksums -default "false" | ConvertTo-Bool
|
|
||||||
$ignoredependencies = Get-Attr -obj $params -name ignore_dependencies -default "false" | ConvertTo-Bool
|
|
||||||
|
|
||||||
# as of chocolatey 0.9.10, nonzero success exit codes can be returned
|
|
||||||
# see https://github.com/chocolatey/choco/issues/512#issuecomment-214284461
|
|
||||||
$successexitcodes = (0,1605,1614,1641,3010)
|
|
||||||
|
|
||||||
if ("present","absent" -notcontains $state)
|
|
||||||
{
|
|
||||||
Fail-Json $result "state is $state; must be present or absent"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$params = Parse-Args $args
|
||||||
|
|
||||||
|
$package = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
||||||
|
$force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $false
|
||||||
|
$upgrade = Get-AnsibleParam -obj $params -name "upgrade" -type "bool" -default $false
|
||||||
|
$version = Get-AnsibleParam -obj $params -name "version" -type "str"
|
||||||
|
$source = Get-AnsibleParam -obj $params -name "source" -type "str"
|
||||||
|
$showlog = Get-AnsibleParam -obj $params -name "showlog" -type "bool" -default $false
|
||||||
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent","latest"
|
||||||
|
$installargs = Get-AnsibleParam -obj $params -name "install_args" -type "str"
|
||||||
|
$packageparams = Get-AnsibleParam -obj $params -name "params" -type "str"
|
||||||
|
$allowemptychecksums = Get-AnsibleParam -obj $params -name "allow_empty_checksums" -type "bool" -default $false
|
||||||
|
$ignorechecksums = Get-AnsibleParam -obj $params -name "ignore_checksums" -type "bool" -default $false
|
||||||
|
$ignoredependencies = Get-AnsibleParam -obj $params -name "ignore_dependencies" -type "bool" -default $false
|
||||||
|
|
||||||
|
if ($source) {$source = $source.Tolower()}
|
||||||
|
|
||||||
|
if ($upgrade)
|
||||||
|
{
|
||||||
|
Add-DeprecateWarning $result "Parameter upgrade=yes is replaced with state=latest"
|
||||||
|
if ($state -eq "present")
|
||||||
|
{
|
||||||
|
$state = "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# As of chocolatey 0.9.10, nonzero success exit codes can be returned
|
||||||
|
# See https://github.com/chocolatey/choco/issues/512#issuecomment-214284461
|
||||||
|
$successexitcodes = (0,1605,1614,1641,3010)
|
||||||
|
|
||||||
Function Chocolatey-Install-Upgrade
|
Function Chocolatey-Install-Upgrade
|
||||||
{
|
{
|
||||||
|
@ -60,24 +63,28 @@ Function Chocolatey-Install-Upgrade
|
||||||
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0
|
$ChocoAlreadyInstalled = get-command choco -ErrorAction 0
|
||||||
if ($ChocoAlreadyInstalled -eq $null)
|
if ($ChocoAlreadyInstalled -eq $null)
|
||||||
{
|
{
|
||||||
|
|
||||||
#We need to install chocolatey
|
#We need to install chocolatey
|
||||||
$install_output = (new-object net.webclient).DownloadString("https://chocolatey.org/install.ps1") | powershell -
|
$install_output = (new-object net.webclient).DownloadString("https://chocolatey.org/install.ps1") | powershell -
|
||||||
if ($LASTEXITCODE -ne 0)
|
if ($LASTEXITCODE -ne 0)
|
||||||
{
|
{
|
||||||
Set-Attr $result "choco_bootstrap_output" $install_output
|
$result.choco_bootstrap_output = $install_output
|
||||||
Fail-Json $result "Chocolatey bootstrap installation failed."
|
Fail-Json $result "Chocolatey bootstrap installation failed."
|
||||||
}
|
}
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
$script:executable = "C:\ProgramData\chocolatey\bin\choco.exe"
|
$script:executable = "C:\ProgramData\chocolatey\bin\choco.exe"
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
$script:executable = "choco.exe"
|
$script:executable = "choco.exe"
|
||||||
|
|
||||||
if ([Version](choco --version) -lt [Version]'0.9.9')
|
if ([Version](choco --version) -lt [Version]'0.9.9')
|
||||||
{
|
{
|
||||||
Choco-Upgrade chocolatey
|
Choco-Upgrade chocolatey
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,12 +99,13 @@ Function Choco-IsInstalled
|
||||||
)
|
)
|
||||||
|
|
||||||
$cmd = "$executable list --local-only $package"
|
$cmd = "$executable list --local-only $package"
|
||||||
$results = invoke-expression $cmd
|
$output = invoke-expression $cmd
|
||||||
|
|
||||||
|
$result.rc = $LastExitCode
|
||||||
if ($LastExitCode -ne 0)
|
if ($LastExitCode -ne 0)
|
||||||
{
|
{
|
||||||
Set-Attr $result "choco_error_cmd" $cmd
|
$result.choco_error_cmd = $cmd
|
||||||
Set-Attr $result "choco_error_log" "$results"
|
$result.choco_error_log = $output
|
||||||
|
|
||||||
Throw "Error checking installation status for $package"
|
Throw "Error checking installation status for $package"
|
||||||
}
|
}
|
||||||
|
@ -107,7 +115,7 @@ Function Choco-IsInstalled
|
||||||
return $matches[1] -gt 0
|
return $matches[1] -gt 0
|
||||||
}
|
}
|
||||||
|
|
||||||
$false
|
return $false
|
||||||
}
|
}
|
||||||
|
|
||||||
Function Choco-Upgrade
|
Function Choco-Upgrade
|
||||||
|
@ -182,12 +190,13 @@ Function Choco-Upgrade
|
||||||
$cmd += " -ignoredependencies"
|
$cmd += " -ignoredependencies"
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = invoke-expression $cmd
|
$output = invoke-expression $cmd
|
||||||
|
|
||||||
|
$result.rc = $LastExitCode
|
||||||
if ($LastExitCode -notin $successexitcodes)
|
if ($LastExitCode -notin $successexitcodes)
|
||||||
{
|
{
|
||||||
Set-Attr $result "choco_error_cmd" $cmd
|
$result.choco_error_cmd = $cmd
|
||||||
Set-Attr $result "choco_error_log" "$results"
|
$result.choco_error_log = $output
|
||||||
Throw "Error installing $package"
|
Throw "Error installing $package"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,22 +236,9 @@ Function Choco-Install
|
||||||
[bool]$ignoredependencies
|
[bool]$ignoredependencies
|
||||||
)
|
)
|
||||||
|
|
||||||
if (Choco-IsInstalled $package)
|
if ((Choco-IsInstalled $package) -and -not $force)
|
||||||
{
|
{
|
||||||
if ($upgrade)
|
return
|
||||||
{
|
|
||||||
Choco-Upgrade -package $package -version $version -source $source -force $force `
|
|
||||||
-installargs $installargs -packageparams $packageparams `
|
|
||||||
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
|
|
||||||
-ignoredependencies $ignoredependencies
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (-not $force)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmd = "$executable install -dv -y $package"
|
$cmd = "$executable install -dv -y $package"
|
||||||
|
@ -287,16 +283,17 @@ Function Choco-Install
|
||||||
$cmd += " -ignoredependencies"
|
$cmd += " -ignoredependencies"
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = invoke-expression $cmd
|
$output = invoke-expression $cmd
|
||||||
|
|
||||||
|
$result.rc = $LastExitCode
|
||||||
if ($LastExitCode -notin $successexitcodes)
|
if ($LastExitCode -notin $successexitcodes)
|
||||||
{
|
{
|
||||||
Set-Attr $result "choco_error_cmd" $cmd
|
$result.choco_error_cmd = $cmd
|
||||||
Set-Attr $result "choco_error_log" "$results"
|
$result.choco_error_log = $output
|
||||||
Throw "Error installing $package"
|
Throw "Error installing $package"
|
||||||
}
|
}
|
||||||
|
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
Function Choco-Uninstall
|
Function Choco-Uninstall
|
||||||
|
@ -334,38 +331,45 @@ Function Choco-Uninstall
|
||||||
$cmd += " -params '$packageparams'"
|
$cmd += " -params '$packageparams'"
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = invoke-expression $cmd
|
$output = invoke-expression $cmd
|
||||||
|
|
||||||
|
$result.rc = $LastExitCode
|
||||||
if ($LastExitCode -notin $successexitcodes)
|
if ($LastExitCode -notin $successexitcodes)
|
||||||
{
|
{
|
||||||
Set-Attr $result "choco_error_cmd" $cmd
|
$result.choco_error_cmd = $cmd
|
||||||
Set-Attr $result "choco_error_log" "$results"
|
$result.choco_error_log = $output
|
||||||
Throw "Error uninstalling $package"
|
Throw "Error uninstalling $package"
|
||||||
}
|
}
|
||||||
|
|
||||||
$result.changed = $true
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
Try
|
Try
|
||||||
{
|
{
|
||||||
Chocolatey-Install-Upgrade
|
Chocolatey-Install-Upgrade
|
||||||
|
|
||||||
if ($state -eq "present")
|
if ($state -eq "present")
|
||||||
{
|
{
|
||||||
Choco-Install -package $package -version $version -source $source `
|
Choco-Install -package $package -version $version -source $source -force $force `
|
||||||
-force $force -upgrade $upgrade -installargs $installargs `
|
-installargs $installargs -packageparams $packageparams `
|
||||||
-packageparams $packageparams -allowemptychecksums $allowemptychecksums `
|
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
|
||||||
-ignorechecksums $ignorechecksums -ignoredependencies $ignoredependencies
|
-ignoredependencies $ignoredependencies
|
||||||
}
|
}
|
||||||
else
|
elseif ($state -eq "latest")
|
||||||
|
{
|
||||||
|
Choco-Upgrade -package $package -version $version -source $source -force $force `
|
||||||
|
-installargs $installargs -packageparams $packageparams `
|
||||||
|
-allowemptychecksums $allowemptychecksums -ignorechecksums $ignorechecksums `
|
||||||
|
-ignoredependencies $ignoredependencies
|
||||||
|
}
|
||||||
|
elseif ($state -eq "absent")
|
||||||
{
|
{
|
||||||
Choco-Uninstall -package $package -version $version -force $force
|
Choco-Uninstall -package $package -version $version -force $force
|
||||||
}
|
}
|
||||||
|
|
||||||
Exit-Json $result;
|
Exit-Json $result
|
||||||
}
|
}
|
||||||
Catch
|
Catch
|
||||||
{
|
{
|
||||||
Fail-Json $result $_.Exception.Message
|
Fail-Json $result $_.Exception.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,9 @@ module: win_chocolatey
|
||||||
version_added: "1.9"
|
version_added: "1.9"
|
||||||
short_description: Installs packages using chocolatey
|
short_description: Installs packages using chocolatey
|
||||||
description:
|
description:
|
||||||
- Installs packages using Chocolatey (http://chocolatey.org/). If Chocolatey is missing from the system, the module will install it. List of packages can be found at http://chocolatey.org/packages
|
- Installs packages using Chocolatey (U(http://chocolatey.org/)).
|
||||||
|
- If Chocolatey is missing from the system, the module will install it.
|
||||||
|
- List of packages can be found at U(http://chocolatey.org/packages)
|
||||||
options:
|
options:
|
||||||
name:
|
name:
|
||||||
description:
|
description:
|
||||||
|
@ -43,25 +45,29 @@ options:
|
||||||
choices:
|
choices:
|
||||||
- present
|
- present
|
||||||
- absent
|
- absent
|
||||||
|
- latest
|
||||||
default: present
|
default: present
|
||||||
force:
|
force:
|
||||||
description:
|
description:
|
||||||
- Forces install of the package (even if it already exists). Using Force will cause ansible to always report that a change was made
|
- Forces install of the package (even if it already exists).
|
||||||
|
- Using C(force) will cause ansible to always report that a change was made
|
||||||
choices:
|
choices:
|
||||||
- yes
|
- yes
|
||||||
- no
|
- no
|
||||||
default: no
|
default: no
|
||||||
upgrade:
|
upgrade:
|
||||||
description:
|
description:
|
||||||
- If package is already installed it, try to upgrade to the latest version or to the specified version
|
- If package is already installed it, try to upgrade to the latest version or to the specified version.
|
||||||
|
- As of Ansible v2.3 this is deprecated, set parameter C(state) to "latest" for the same result.
|
||||||
|
version_removed: '2.3'
|
||||||
choices:
|
choices:
|
||||||
- yes
|
- yes
|
||||||
- no
|
- no
|
||||||
default: no
|
default: no
|
||||||
version:
|
version:
|
||||||
description:
|
description:
|
||||||
- Specific version of the package to be installed
|
- Specific version of the package to be installed.
|
||||||
- Ignored when state == 'absent'
|
- Ignored when C(state) is set to "absent".
|
||||||
source:
|
source:
|
||||||
description:
|
description:
|
||||||
- Specify source rather than using default chocolatey repository
|
- Specify source rather than using default chocolatey repository
|
||||||
|
@ -76,13 +82,11 @@ options:
|
||||||
allow_empty_checksums:
|
allow_empty_checksums:
|
||||||
description:
|
description:
|
||||||
- Allow empty Checksums to be used
|
- Allow empty Checksums to be used
|
||||||
require: false
|
|
||||||
default: false
|
default: false
|
||||||
version_added: '2.2'
|
version_added: '2.2'
|
||||||
ignore_checksums:
|
ignore_checksums:
|
||||||
description:
|
description:
|
||||||
- Ignore Checksums
|
- Ignore Checksums
|
||||||
require: false
|
|
||||||
default: false
|
default: false
|
||||||
version_added: '2.2'
|
version_added: '2.2'
|
||||||
ignore_dependencies:
|
ignore_dependencies:
|
||||||
|
|
|
@ -219,7 +219,6 @@ lib/ansible/modules/web_infrastructure/jira.py
|
||||||
lib/ansible/modules/web_infrastructure/nginx_status_facts.py
|
lib/ansible/modules/web_infrastructure/nginx_status_facts.py
|
||||||
lib/ansible/modules/windows/win_acl.py
|
lib/ansible/modules/windows/win_acl.py
|
||||||
lib/ansible/modules/windows/win_acl_inheritance.py
|
lib/ansible/modules/windows/win_acl_inheritance.py
|
||||||
lib/ansible/modules/windows/win_chocolatey.py
|
|
||||||
lib/ansible/modules/windows/win_command.py
|
lib/ansible/modules/windows/win_command.py
|
||||||
lib/ansible/modules/windows/win_feature.py
|
lib/ansible/modules/windows/win_feature.py
|
||||||
lib/ansible/modules/windows/win_lineinfile.py
|
lib/ansible/modules/windows/win_lineinfile.py
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue