ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
787 stars 1.45k forks source link

[Feature Request] [zypper] Allow refreshing cache without specifying `name:` #3570

Open gotmax23 opened 2 years ago

gotmax23 commented 2 years ago

Summary

Currently, it is not possible to update the package cache with the community.general.zypper module without specifying the name argument.

This task fails with the following error message:

$ ansible all -i inventory -b -m zypper -a update_cache=true
hostname | FAILED! => {
    "changed": false,
    "msg": "missing required arguments: name"
}

This usecase is supported by every other distro package module (or at least the ones I've used). This includes apt, yum, dnf, pacman, and apk.

This missing feature makes updating the package cache in my gotmax23.update role unnecessarily complicated.

- name: Update package cache
  changed_when: false
  when: ansible_pkg_mgr != "zypper"
  ansible.builtin.package:
    update_cache: true

- name: Update package cache (Zypper only)
  when: ansible_pkg_mgr == "zypper"
  changed_when: false
  check_mode: false
  ansible.builtin.command:
    cmd: "zypper refresh"
    warn: false

-- https://git.sr.ht/~gotmax23/ansible-role-update/tree/d3ef47593f85fb917e6cc71833970f03994bf5ab/item/tasks/main.yml

Issue Type

Feature Idea

Component Name

plugins/modules/packaging/os/zypper.py

Additional Information

The example playbook below does not work.

- name: Update Zypper Cache
  community.general.zypper:
    update_cache: true

Code of Conduct

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 years ago

cc @AnderEnder @alxgu @andytom @commel @dcermak @evrardjp @lrupp @sealor @toabctl click here for bot help

nicr9 commented 2 years ago

I've been looking for issues that I can tackle for Hacktoberfest and this looks interesting. I've been reading other modules to better understand how they acheive the desired effect and I think it's mostly replicable for zypper.

My only concern is if zypper refresh will return enough useful information to determine if the repositories have actually been updated. From my testing, it looks like there's no useful info returned to parse because we pass the --quiet flag:

$ zypper clean -a
$ zypper --quiet --non-interactive --xmlout refresh
<?xml version='1.0'?>
<stream>
</stream>

It looks like we'd need to refactor some of the helper functions in the module in order to determine if any repos were actually updated during the run.

With this in mind, I'm going to put together a PR that returns with changed: True if the refresh was merely sucessful (similarly to the approach used by the command module which returns changed: True by default)

gotmax23 commented 2 years ago

With this in mind, I'm going to put together a PR that returns with changed: True if the refresh was merely sucessful (similarly to the approach used by the command module which returns changed: True by default)

Do the other package modules behave like this or are some of them able to figure out if the contents of the repository actually changed? I haven't had a chance to look at this myself.

nicr9 commented 2 years ago

I haven't has a chance to look at all of the other package modules yet but from what I recall in my personal use, other package modules know when the repository data has actually been refreshed.

It's not impossible to tell with zypper. If we remove the --quiet flag, we'll see some <progress ..> tags in the xml data it returns telling us if repositories were already up to date or if it tried downloading and building the latest data.

The helper functions that process the returned xml would need changes and that'd require testing all of the other supported use cases to avoid regressions. I'm not really a OpenSuse user so I thought I should probably keep my changes small/conservitive for now and maybe work on the bigger change later.

gotmax23 commented 2 years ago

Okay, that makes sense. Thank you for working on this issue!

gotmax23 commented 2 years ago

I haven't has a chance to look at all of the other package modules yet but from what I recall in my personal use, other package modules know when the repository data has actually been refreshed.

I was curious, so I tested it myself. All of the package modules I mentioned above besides community.general.pacman and possibly community.general.apk (I haven't tested that) support this. I look forward to your PR being accepted!

ansibullbot commented 1 year ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

Hummdis commented 1 year ago

I was looking for something else, like using zypper clean --all via a native Ansible way when I came across this feature request.

Why add this to zypper when you can use zypper_repository? I'm playing devils advocate here, but you can do this using something like:

- name: Building Zypper Cache...
  community.general.zypper_repository:
    repo: '*'
    runrefresh: true
    auto_import_keys: true

While YUM has the yum_repository module for managing repos and Apt has the apt_repository module, Zypper has zypper_repository.

commel commented 1 year ago

I agree on this. With apt one use the apt_repository module, this should be handle with zypper similarly.