From 996b7469e5b01fa7ffd5ffc01a0de0a8b9b8a4cd Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Wed, 7 Jan 2026 06:20:02 +1300 Subject: [PATCH] slackpkg: simplify function `query_package()` (#11390) * slackpkg: simplify function query_package() * add changelog frag --- changelogs/fragments/11390-slackpkg-query.yml | 2 ++ plugins/modules/slackpkg.py | 20 +++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/11390-slackpkg-query.yml diff --git a/changelogs/fragments/11390-slackpkg-query.yml b/changelogs/fragments/11390-slackpkg-query.yml new file mode 100644 index 0000000000..8d8f2ba9ea --- /dev/null +++ b/changelogs/fragments/11390-slackpkg-query.yml @@ -0,0 +1,2 @@ +minor_changes: + - slackpkg - refactor function ``query_packages()`` (https://github.com/ansible-collections/community.general/pull/11390). diff --git a/plugins/modules/slackpkg.py b/plugins/modules/slackpkg.py index 6dc422beaa..bd66cc55ef 100644 --- a/plugins/modules/slackpkg.py +++ b/plugins/modules/slackpkg.py @@ -68,14 +68,14 @@ EXAMPLES = r""" state: latest """ +import platform +import os +import re + from ansible.module_utils.basic import AnsibleModule -def query_package(module, slackpkg_path, name): - import platform - import os - import re - +def query_package(name): machine = platform.machine() # Exception for kernel-headers package on x86_64 if name == "kernel-headers" and machine == "x86_64": @@ -91,13 +91,13 @@ def remove_packages(module, slackpkg_path, packages): # Using a for loop in case of error, we can report the package that failed for package in packages: # Query the package first, to see if we even need to remove - if not query_package(module, slackpkg_path, package): + if not query_package(package): continue if not module.check_mode: rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "remove", package]) - if not module.check_mode and query_package(module, slackpkg_path, package): + if not module.check_mode and query_package(package): module.fail_json(msg=f"failed to remove {package}: {out}") remove_c += 1 @@ -112,13 +112,13 @@ def install_packages(module, slackpkg_path, packages): install_c = 0 for package in packages: - if query_package(module, slackpkg_path, package): + if query_package(package): continue if not module.check_mode: rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "install", package]) - if not module.check_mode and not query_package(module, slackpkg_path, package): + if not module.check_mode and not query_package(package): module.fail_json(msg=f"failed to install {package}: {out}", stderr=err) install_c += 1 @@ -136,7 +136,7 @@ def upgrade_packages(module, slackpkg_path, packages): if not module.check_mode: rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "upgrade", package]) - if not module.check_mode and not query_package(module, slackpkg_path, package): + if not module.check_mode and not query_package(package): module.fail_json(msg=f"failed to install {package}: {out}", stderr=err) install_c += 1