Howto remove one apt package if the version differ

Hi,

I need to remove one apt package but only if differ the version, example should be:

  • name: remove package
    apt:
    name:
  • “htop!=2.2.0-2build1”
    status: absent

Or something as:

  • name: remove package
    apt:
    name:
  • htop
    status: absent
    when: htop version != ‘2.2.0-2build1’

Howto write this task?

Regards

2 ways I can think of, first is use 'package_facts' to get the info
for version, the other is a shell task with something like `apt show
htop>grep Version|cut -f 2 -d':'`

Then you write the apt task using version test.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/version_test.html