diff --git a/changelogs/fragments/10601-pear-cmd-list.yml b/changelogs/fragments/10601-pear-cmd-list.yml new file mode 100644 index 0000000000..d5ab2d3d0e --- /dev/null +++ b/changelogs/fragments/10601-pear-cmd-list.yml @@ -0,0 +1,2 @@ +minor_changes: + - pear - using safer mechanism to run external command (https://github.com/ansible-collections/community.general/pull/10601). diff --git a/plugins/modules/pear.py b/plugins/modules/pear.py index 5eb84b509d..5bc180763c 100644 --- a/plugins/modules/pear.py +++ b/plugins/modules/pear.py @@ -151,34 +151,33 @@ def get_repository_version(pear_output): return None -def query_package(module, name, state="present"): +def query_package(module, name): """Query the package status in both the local system and the repository. Returns a boolean to indicate if the package is installed, and a second boolean to indicate if the package is up-to-date.""" - if state == "present": - lcmd = "%s info %s" % (_get_pear_path(module), name) - lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False) - if lrc != 0: - # package is not installed locally - return False, False - - rcmd = "%s remote-info %s" % (_get_pear_path(module), name) - rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False) - - # get the version installed locally (if any) - lversion = get_local_version(rstdout) - - # get the version in the repository - rversion = get_repository_version(rstdout) - - if rrc == 0: - # Return True to indicate that the package is installed locally, - # and the result of the version number comparison - # to determine if the package is up-to-date. - return True, (lversion == rversion) - + lcmd = [_get_pear_path(module), "info", name] + lrc, lstdout, lstderr = module.run_command(lcmd, check_rc=False) + if lrc != 0: + # package is not installed locally return False, False + rcmd = [_get_pear_path(module), "remote-info", name] + rrc, rstdout, rstderr = module.run_command(rcmd, check_rc=False) + + # get the version installed locally (if any) + lversion = get_local_version(rstdout) + + # get the version in the repository + rversion = get_repository_version(rstdout) + + if rrc == 0: + # Return True to indicate that the package is installed locally, + # and the result of the version number comparison + # to determine if the package is up-to-date. + return True, (lversion == rversion) + + return False, False + def remove_packages(module, packages): remove_c = 0 @@ -189,7 +188,7 @@ def remove_packages(module, packages): if not installed: continue - cmd = "%s uninstall %s" % (_get_pear_path(module), package) + cmd = [_get_pear_path(module), "uninstall", package] rc, stdout, stderr = module.run_command(cmd, check_rc=False) if rc != 0: @@ -258,7 +257,7 @@ def install_packages(module, state, packages, prompts): prompt_regex = None data = default_stdin - cmd = "%s %s %s" % (_get_pear_path(module), command, package) + cmd = [_get_pear_path(module), command, package] rc, stdout, stderr = module.run_command(cmd, check_rc=False, prompt_regex=prompt_regex, data=data, binary_data=True) if rc != 0: module.fail_json(msg="failed to install %s: %s" % (package, to_text(stdout + stderr)))