diff --git a/chocolatey/plugins/module_utils/Packages.psm1 b/chocolatey/plugins/module_utils/Packages.psm1 index 8da4001..176ee44 100644 --- a/chocolatey/plugins/module_utils/Packages.psm1 +++ b/chocolatey/plugins/module_utils/Packages.psm1 @@ -945,6 +945,11 @@ function Uninstall-ChocolateyPackage { [Ansible.Basic.AnsibleModule] $Module = (Get-AnsibleModule), + # Any additional arguments to be passed directly to `choco.exe` + [Parameter()] + [string[]] + $ChocoArgs, + # Set to force choco to reinstall the package if the package (version) # is already installed. [Parameter()] @@ -1005,6 +1010,8 @@ function Uninstall-ChocolateyPackage { if ($PSBoundParameters.ContainsKey('Timeout')) { "--timeout", $timeout } if ($SkipScripts) { "--skip-scripts" } if ($PackageParams) { "--package-parameters", $package_params } + + if ($ChocoArgs) { $ChocoArgs } ) $command = Argv-ToString -Arguments $arguments diff --git a/chocolatey/plugins/modules/win_chocolatey.ps1 b/chocolatey/plugins/modules/win_chocolatey.ps1 index 1b06d12..904ab18 100644 --- a/chocolatey/plugins/modules/win_chocolatey.ps1 +++ b/chocolatey/plugins/modules/win_chocolatey.ps1 @@ -184,6 +184,7 @@ if ($state -in "absent", "reinstalled") { $uninstallParams = @{ ChocoCommand = $chocoCommand Package = $package + ChocoArgs = $choco_args Force = $force PackageParams = $package_params SkipScripts = $skip_scripts @@ -209,7 +210,9 @@ if ($state -in @("downgrade", "latest", "upgrade", "present", "reinstalled")) { $missingPackages = [System.Collections.Generic.List[string]]@() if ($state -eq "present" -and $force) { - $missingPackages.Add($name) + foreach ($package in $name) { + $missingPackages.Add($package) + } } else { foreach ($package in $packageInfo.GetEnumerator()) { diff --git a/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml b/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml index 4e149c9..b584b99 100644 --- a/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml +++ b/chocolatey/tests/integration/targets/win_chocolatey/tasks/tests.yml @@ -751,3 +751,41 @@ that: - ignore_pinned_upgrade is changed - ignore_pinned_upgrade is not failed + +- name: force install multiple packages + win_chocolatey: + name: + - '{{ test_choco_package1 }}' + - '{{ test_choco_package2 }}' + state: present + register: force_install_multiple + +- name: assert force install multiple packages + assert: + that: + - force_install_multiple is changed + - force_install_multiple is not failed + +- name: clear the Chocolatey log file + ansible.windows.win_file: + path: "{{ ansible_env.ChocolateyInstall }}\\logs\\chocolatey.log" + state: absent + +- name: uninstall Chocolatey Package with additional choco_args + chocolatey.chocolatey.win_chocolatey: + name: '{{ test_choco_package1 }}' + state: absent + choco_args: + --skipautouninstaller + +- name: read Chocolatey log file + ansible.windows.slurp: + src: "{{ ansible_env.ChocolateyInstall }}\\logs\\chocolatey.log" + register: uninstall_with_args_log_content + +- name: assert generated choco command includes provided additional choco_args + ansible.builtin.assert: + that: + - log_text is search('(?i)command line.*uninstall\s+' ~ test_choco_package1 ~ '.*--skipautouninstaller') + vars: + log_text: "{{ uninstall_with_args_log_content['content'] | b64decode }}"