Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make apt clean mark as change only if something was actually cleaned #84880

@nipil

Description

@nipil

Summary

Limitation

I'm using ansible [core 2.17.9] and an apt task with a single clean: true it always returns changed: true, even if there was nothing to be cleaned.

Proposal

This could be improved if we detected that nothing will be done, as apt-get can tell us what it will be doing.
An improvement could be done based on the apt-get clean --dry-run which outputs (r)glob expressions :

sudo apt-get clean --dry-run
Del /var/cache/apt/archives/* /var/cache/apt/archives/partial/*
Del /var/lib/apt/lists/partial/*
Del /var/cache/apt/pkgcache.bin /var/cache/apt/srcpkgcache.bin

This feature and format has been present in upstream APT for about 10 years :
https://salsa.debian.org/apt-team/apt/-/blob/1.1_exp15/apt-private/private-download.cc#L301

Issue Type

Feature Idea

Component Name

apt

Additional Information

Implementation suggestion

The changed result could be set to true if any of the (r?)glob expressions provided by --dry-run match anything.
Stopping on first match found, and maybe even starting from the two non-glob expression, should make it pretty fast.

The change could be done in lib.ansible.modules.apt.aptclean(). It would be something along the line of :

  • start by setting local var changed to false
  • run m.run_command(['apt-get', 'clean', '--dry-run'])
  • test for errors and if none found, split by lines
  • match if line starts with Del , and split the remainder by whitespaces
  • for each expression, check if it is a glob, and maybe optimize by processing non-globs first
  • for non glob expression, test if exists, and for glob expressions try to find a match
  • if any is found for any exprssion, set local changed var to true and stop processing
  • then do the remainder of the current function, actually runing apt-get clean
  • finally return the pre-computed cleanup variable as 4th value
  • in the caller, get the "changed" value from the 4th return value
  • update the caller to update the changed return value in combination with other apt operations
    (which might be the most complicated thing for a newcomer like me)

I would love to provide a PR, as i can actually write some Python, but to be honest, understanding how to run a development version of Ansible to test my proposal above, might take me far more time than someone "more used" to ansible might take to code the above proposal. In any case, i could do it myself but i would certainly need some help or some explicit pointers and advice on how to test ansible in a development environment.

So i'll leave this suggestion up to you, or i'll require a "looks good to me" from you, before i actually spend some time on this. Thanks in advance for your feedback.

Code of Conduct

  • I agree to follow the Ansible Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureThis issue/PR relates to a feature request.moduleThis issue/PR relates to a module.waiting_on_contributorThis would be accepted but there are no plans to actively work on it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions