diff --git a/changelogs/fragments/82611_fix_alignment_apt_clean_with_ansible_apt_clean.yml b/changelogs/fragments/82611_fix_alignment_apt_clean_with_ansible_apt_clean.yml new file mode 100644 index 00000000000..bca3cfdf00d --- /dev/null +++ b/changelogs/fragments/82611_fix_alignment_apt_clean_with_ansible_apt_clean.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible.builtin.apt - calling clean = true does not properly clean certain cache files such as /var/cache/apt/pkgcache.bin and /var/cache/apt/pkgcache.bin (https://github.com/ansible/ansible/issues/82611) diff --git a/lib/ansible/modules/apt.py b/lib/ansible/modules/apt.py index 98a641aa66f..33d33731067 100644 --- a/lib/ansible/modules/apt.py +++ b/lib/ansible/modules/apt.py @@ -1315,7 +1315,7 @@ def main(): aptclean_stdout, aptclean_stderr, aptclean_diff = aptclean(module) # If there is nothing else to do exit. This will set state as # changed based on if the cache was updated. - if not p['package'] and not p['upgrade'] and not p['deb']: + if not p['package'] and p['upgrade'] == 'no' and not p['deb']: module.exit_json( changed=True, msg=aptclean_stdout, diff --git a/test/integration/targets/apt/tasks/apt.yml b/test/integration/targets/apt/tasks/apt.yml index a0bc19929f6..55ab51d21d2 100644 --- a/test/integration/targets/apt/tasks/apt.yml +++ b/test/integration/targets/apt/tasks/apt.yml @@ -594,3 +594,26 @@ - elpa-yaml-mode state: absent purge: yes + +# https://github.com/ansible/ansible/issues/82611 +- name: clean apt cache + apt: + clean: true + +- name: check for /var/cache/apt/pkgcache.bin + stat: + path: /var/cache/apt/pkgcache.bin + register: pkgcache_bin + +- name: check for /var/cache/apt/srcpkgcache.bin + stat: + path: /var/cache/apt/srcpkgcache.bin + register: srcpkgcache_bin + +- name: verify apt cache files are cleaned + assert: + that: + - not pkgcache_bin.stat.exists + - not srcpkgcache_bin.stat.exists + fail_msg: "apt cache files still exist." + success_msg: "apt cache files are cleaned."