diff --git a/changelogs/fragments/3526-pkgng-add-integration-tests.yml b/changelogs/fragments/3526-pkgng-add-integration-tests.yml index 150b9435df..53648082da 100644 --- a/changelogs/fragments/3526-pkgng-add-integration-tests.yml +++ b/changelogs/fragments/3526-pkgng-add-integration-tests.yml @@ -1,2 +1,3 @@ bugfixes: - 'pkgng - `name=* state=latest` check for upgrades did not count "Number of packages to be reinstalled" as a `changed` action, giving incorrect results in both regular and check mode' + - 'pkgng - PR #3393 (https://github.com/ansible-collections/community.general/pull/3393) broke `check_mode` so that the module always reports `not changed`; fix regression so module reports number of upgrade or install actions that would be performed' diff --git a/plugins/modules/packaging/os/pkgng.py b/plugins/modules/packaging/os/pkgng.py index 5d45820521..a672987d74 100644 --- a/plugins/modules/packaging/os/pkgng.py +++ b/plugins/modules/packaging/os/pkgng.py @@ -277,29 +277,35 @@ def install_packages(module, pkgng_path, packages, cached, pkgsite, dir_arg, sta else: action_queue["install"].append(package) - if not module.check_mode: - # install/upgrade all named packages with one pkg command - for (action, package_list) in action_queue.items(): - packages = ' '.join(package_list) - if old_pkgng: - rc, out, err = module.run_command("%s %s %s %s -g -U -y %s" % (batch_var, pkgsite, pkgng_path, action, packages)) + # install/upgrade all named packages with one pkg command + for (action, package_list) in action_queue.items(): + if module.check_mode: + # Do nothing, but count up how many actions + # would be performed so that the changed/msg + # is correct. + action_count[action] += len(package_list) + continue + + packages = ' '.join(package_list) + if old_pkgng: + rc, out, err = module.run_command("%s %s %s %s -g -U -y %s" % (batch_var, pkgsite, pkgng_path, action, packages)) + else: + rc, out, err = module.run_command("%s %s %s %s %s -g -U -y %s" % (batch_var, pkgng_path, dir_arg, action, pkgsite, packages)) + stdout += out + stderr += err + + # individually verify packages are in requested state + for package in package_list: + verified = False + if action == 'install': + verified = query_package(module, pkgng_path, package, dir_arg) + elif action == 'upgrade': + verified = not query_update(module, pkgng_path, package, dir_arg, old_pkgng, pkgsite) + + if verified: + action_count[action] += 1 else: - rc, out, err = module.run_command("%s %s %s %s %s -g -U -y %s" % (batch_var, pkgng_path, dir_arg, action, pkgsite, packages)) - stdout += out - stderr += err - - # individually verify packages are in requested state - for package in package_list: - verified = False - if action == 'install': - verified = query_package(module, pkgng_path, package, dir_arg) - elif action == 'upgrade': - verified = not query_update(module, pkgng_path, package, dir_arg, old_pkgng, pkgsite) - - if verified: - action_count[action] += 1 - else: - module.fail_json(msg="failed to %s %s" % (action, package), stdout=stdout, stderr=stderr) + module.fail_json(msg="failed to %s %s" % (action, package), stdout=stdout, stderr=stderr) if sum(action_count.values()) > 0: past_tense = {'install': 'installed', 'upgrade': 'upgraded'}