From 3e0eb392d1e215bb7d7744c680f66744808d4228 Mon Sep 17 00:00:00 2001 From: Ross Williams Date: Mon, 11 Oct 2021 19:19:06 +0000 Subject: [PATCH] pkgng: handle space- and comma-separated lists The module expects a list of strings in the `name` parameter, but long-standing documentation showed space- and comma-delimited lists as a valid way of telling the module to act on multiple packages. Passing these lists through to the `pkg` command can have unexpected side-effects of upgrading packages when `state=present` rather than `state=latest` and could result in the module reporting the wrong number of packages for each action performed. --- .../fragments/3526-pkgng-add-integration-tests.yml | 1 + plugins/modules/packaging/os/pkgng.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/changelogs/fragments/3526-pkgng-add-integration-tests.yml b/changelogs/fragments/3526-pkgng-add-integration-tests.yml index 53648082da..4c407d7694 100644 --- a/changelogs/fragments/3526-pkgng-add-integration-tests.yml +++ b/changelogs/fragments/3526-pkgng-add-integration-tests.yml @@ -1,3 +1,4 @@ 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' + - 'pkgng - The module will now convert a single space- or comma-separated name parameter to a list. The documentation had given wrong examples in which multiple space- or comma-separated packages were specified in the `name` parameter, rather than using correct YAML list syntax. `pkgng` module documentation has also been updated' diff --git a/plugins/modules/packaging/os/pkgng.py b/plugins/modules/packaging/os/pkgng.py index 27aceaba0b..0abcf80121 100644 --- a/plugins/modules/packaging/os/pkgng.py +++ b/plugins/modules/packaging/os/pkgng.py @@ -113,12 +113,16 @@ EXAMPLES = ''' - name: Annotate package foo and bar community.general.pkgng: - name: foo,bar + name: + - foo + - bar annotation: '+test1=baz,-test2,:test3=foobar' - name: Remove packages foo and bar community.general.pkgng: - name: foo,bar + name: + - foo + - bar state: absent # "latest" support added in 2.7 @@ -476,6 +480,12 @@ def main(): msgs.append(_msg) # Operate on named packages + if len(pkgs) == 1: + # The documentation used to show multiple packages specified in one line + # with comma or space delimiters. That doesn't result in a YAML list, and + # wrong actions (install vs upgrade) can be reported if those + # comma- or space-delimited strings make it to the pkg command line. + pkgs = re.split(r'[,\s]', pkgs[0]) named_packages = [pkg for pkg in pkgs if pkg != '*'] if p["state"] in ("present", "latest") and named_packages: _changed, _msg, _out, _err = install_packages(module, pkgng_path, named_packages,