mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-02-04 07:51:50 +00:00
slackpkg: simplify function query_package() (#11390)
* slackpkg: simplify function query_package() * add changelog frag
This commit is contained in:
parent
b67c94fc3f
commit
996b7469e5
2 changed files with 12 additions and 10 deletions
2
changelogs/fragments/11390-slackpkg-query.yml
Normal file
2
changelogs/fragments/11390-slackpkg-query.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- slackpkg - refactor function ``query_packages()`` (https://github.com/ansible-collections/community.general/pull/11390).
|
||||||
|
|
@ -68,14 +68,14 @@ EXAMPLES = r"""
|
||||||
state: latest
|
state: latest
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import platform
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
|
||||||
def query_package(module, slackpkg_path, name):
|
def query_package(name):
|
||||||
import platform
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
|
|
||||||
machine = platform.machine()
|
machine = platform.machine()
|
||||||
# Exception for kernel-headers package on x86_64
|
# Exception for kernel-headers package on x86_64
|
||||||
if name == "kernel-headers" and machine == "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
|
# Using a for loop in case of error, we can report the package that failed
|
||||||
for package in packages:
|
for package in packages:
|
||||||
# Query the package first, to see if we even need to remove
|
# 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
|
continue
|
||||||
|
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "remove", package])
|
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}")
|
module.fail_json(msg=f"failed to remove {package}: {out}")
|
||||||
|
|
||||||
remove_c += 1
|
remove_c += 1
|
||||||
|
|
@ -112,13 +112,13 @@ def install_packages(module, slackpkg_path, packages):
|
||||||
install_c = 0
|
install_c = 0
|
||||||
|
|
||||||
for package in packages:
|
for package in packages:
|
||||||
if query_package(module, slackpkg_path, package):
|
if query_package(package):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "install", package])
|
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)
|
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err)
|
||||||
|
|
||||||
install_c += 1
|
install_c += 1
|
||||||
|
|
@ -136,7 +136,7 @@ def upgrade_packages(module, slackpkg_path, packages):
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
rc, out, err = module.run_command([slackpkg_path, "-default_answer=y", "-batch=on", "upgrade", package])
|
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)
|
module.fail_json(msg=f"failed to install {package}: {out}", stderr=err)
|
||||||
|
|
||||||
install_c += 1
|
install_c += 1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue