apt: properly set changed for autoremove/autoclean (#38329)

This commit is contained in:
Martin Krizek 2018-04-09 11:10:42 +02:00 committed by GitHub
parent 81a8c42115
commit bb6f90ff6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 12 deletions

View file

@ -287,6 +287,12 @@ APT_LISTS_PATH = "/var/lib/apt/lists"
APT_UPDATE_SUCCESS_STAMP_PATH = "/var/lib/apt/periodic/update-success-stamp" APT_UPDATE_SUCCESS_STAMP_PATH = "/var/lib/apt/periodic/update-success-stamp"
APT_MARK_INVALID_OP = 'Invalid operation' APT_MARK_INVALID_OP = 'Invalid operation'
CLEAN_OP_CHANGED_STR = dict(
autoremove='The following packages will be REMOVED',
# "Del python3-q 2.4-1 [24 kB]"
autoclean='Del ',
)
HAS_PYTHON_APT = True HAS_PYTHON_APT = True
try: try:
import apt import apt
@ -720,6 +726,10 @@ def remove(m, pkgspec, cache, purge=False, force=False,
def cleanup(m, purge=False, force=False, operation=None, def cleanup(m, purge=False, force=False, operation=None,
dpkg_options=expand_dpkg_options(DPKG_OPTIONS)): dpkg_options=expand_dpkg_options(DPKG_OPTIONS)):
if operation not in frozenset(['autoremove', 'autoclean']):
raise AssertionError('Expected "autoremove" or "autoclean" cleanup operation, got %s' % operation)
if force: if force:
force_yes = '--force-yes' force_yes = '--force-yes'
else: else:
@ -744,7 +754,10 @@ def cleanup(m, purge=False, force=False, operation=None,
diff = {} diff = {}
if rc: if rc:
m.fail_json(msg="'apt-get %s' failed: %s" % (operation, err), stdout=out, stderr=err, rc=rc) m.fail_json(msg="'apt-get %s' failed: %s" % (operation, err), stdout=out, stderr=err, rc=rc)
m.exit_json(changed=bool(len(diff)), stdout=out, stderr=err, diff=diff)
changed = CLEAN_OP_CHANGED_STR[operation] in out
m.exit_json(changed=changed, stdout=out, stderr=err, diff=diff)
def upgrade(m, mode="yes", force=False, default_release=None, def upgrade(m, mode="yes", force=False, default_release=None,

View file

@ -153,6 +153,9 @@
- "not apt_result.changed" - "not apt_result.changed"
- "apt_result.failed" - "apt_result.failed"
- name: autoclean during install
apt: pkg=hello state=present autoclean=yes
# https://github.com/ansible/ansible/issues/23155 # https://github.com/ansible/ansible/issues/23155
- name: create a repo file - name: create a repo file
copy: copy:
@ -210,12 +213,3 @@
with_items: with_items:
- libcaca-dev - libcaca-dev
- libslang2-dev - libslang2-dev
- name: autoclean during install
apt: pkg=hello state=present autoclean=yes
- name: autoclean
apt: autoclean=yes
- name: autoremove
apt: autoremove=yes

View file

@ -16,8 +16,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- block: - block:
- include: 'apt.yml'
- block: - block:
- include: 'repo.yml' - include: 'repo.yml'
always: always:
@ -28,6 +26,8 @@
name: "{{ repodir }}" name: "{{ repodir }}"
state: absent state: absent
- include: 'apt.yml'
- include: 'apt-multiarch.yml' - include: 'apt-multiarch.yml'
when: when:
- ansible_userspace_architecture != apt_foreign_arch - ansible_userspace_architecture != apt_foreign_arch

View file

@ -61,6 +61,86 @@
that: that:
- "dpkg_result is failed" - "dpkg_result is failed"
# https://github.com/ansible/ansible/issues/26298
- name: Clean up
apt:
name: "{{ item }}"
state: absent
with_items:
- foo
- foobar
- name: Install foobar, installs foo as a dependency
apt:
name: foobar=1.0.0
allow_unauthenticated: yes
- name: Upgrade foobar to a version which does not depend on foo
apt:
upgrade: dist
force: yes # workaround for --allow-unauthenticated used along with upgrade
- name: autoremove should remove foo
apt:
autoremove: yes
register: autoremove_result
- name: Check that autoremove correctly reports changed=True
assert:
that:
- "autoremove_result is changed"
- name: Check foo with dpkg
shell: dpkg-query -l foo
register: dpkg_result
ignore_errors: yes
- name: Check that foo was removed by autoremove
assert:
that:
- "dpkg_result is failed"
- name: Nothing to autoremove
apt:
autoremove: yes
register: autoremove_result
- name: Check that autoremove correctly reports changed=False
assert:
that:
- "autoremove_result is not changed"
- name: Create a fake .deb file for autoclean to remove
file:
name: /var/cache/apt/archives/python3-q_2.4-1_all.deb
state: touch
- name: autoclean fake .deb file
apt:
autoclean: yes
register: autoclean_result
- name: Check if the .deb file exists
stat:
path: /var/cache/apt/archives/python3-q_2.4-1_all.deb
register: stat_result
- name: Check that autoclean correctly reports changed=True and file was removed
assert:
that:
- "autoclean_result is changed"
- "not stat_result.stat.exists"
- name: Nothing to autoclean
apt:
autoclean: yes
register: autoclean_result
- name: Check that autoclean correctly reports changed=False
assert:
that:
- "autoclean_result is not changed"
always: always:
- name: Clean up - name: Clean up
apt: apt: