diff --git a/changelogs/fragments/5369-pkgng-fix-update-all.yaml b/changelogs/fragments/5369-pkgng-fix-update-all.yaml new file mode 100644 index 0000000000..783d461a9e --- /dev/null +++ b/changelogs/fragments/5369-pkgng-fix-update-all.yaml @@ -0,0 +1,2 @@ +bugfixes: + - pkgng - fix case when ``pkg`` fails when trying to upgrade all packages (https://github.com/ansible-collections/community.general/issues/5363). diff --git a/plugins/modules/packaging/os/pkgng.py b/plugins/modules/packaging/os/pkgng.py index 5f4a2a5f27..6e94dcaee5 100644 --- a/plugins/modules/packaging/os/pkgng.py +++ b/plugins/modules/packaging/os/pkgng.py @@ -38,7 +38,7 @@ options: state: description: - State of the package. - - 'Note: "latest" added in 2.7' + - 'Note: C(latest) added in 2.7.' choices: [ 'present', 'latest', 'absent' ] required: false default: present @@ -149,10 +149,7 @@ def query_package(module, run_pkgng, name): rc, out, err = run_pkgng('info', '-g', '-e', name) - if rc == 0: - return True - - return False + return rc == 0 def query_update(module, run_pkgng, name): @@ -162,10 +159,7 @@ def query_update(module, run_pkgng, name): # rc = 1, updates available rc, out, err = run_pkgng('upgrade', '-g', '-n', name) - if rc == 1: - return True - - return False + return rc == 1 def pkgng_older_than(module, pkgng_path, compare_version): @@ -191,7 +185,7 @@ def upgrade_packages(module, run_pkgng): pkgng_args = ['upgrade'] pkgng_args.append('-n' if module.check_mode else '-y') - rc, out, err = run_pkgng(*pkgng_args) + rc, out, err = run_pkgng(*pkgng_args, check_rc=(not module.check_mode)) matches = re.findall('^Number of packages to be (?:upgraded|reinstalled): ([0-9]+)', out, re.MULTILINE) for match in matches: diff --git a/tests/integration/targets/pkgng/tasks/freebsd.yml b/tests/integration/targets/pkgng/tasks/freebsd.yml index d17a3083d3..a7cb90b11d 100644 --- a/tests/integration/targets/pkgng/tasks/freebsd.yml +++ b/tests/integration/targets/pkgng/tasks/freebsd.yml @@ -140,6 +140,42 @@ - pkgng_example4.changed - not pkgng_example4_idempotent.changed +## +## pkgng - example - state=latest for out-of-date package without privileges +## +- name: Install intentionally out-of-date package and try to upgrade it with unprivileged user + block: + - ansible.builtin.user: + name: powerless + shell: /bin/bash + + - name: Create out-of-date test package + import_tasks: create-outofdate-pkg.yml + + - name: Install out-of-date test package + command: 'pkg add {{ pkgng_test_outofdate_pkg_path }}' + register: pkgng_example4_nopower_prepare + + - name: Check for any available package upgrades with unprivileged user + become: true + become_user: powerless + pkgng: + name: '*' + state: latest + register: pkgng_example4_nopower_wildcard + ignore_errors: true + + - name: Remove test out-of-date package + pkgng: + name: '{{ pkgng_test_pkg_name }}' + state: absent + + - name: Ensure pkgng upgrades package correctly + assert: + that: + - not pkgng_example4_nopower_prepare.failed + - pkgng_example4_nopower_wildcard.failed + ## ## pkgng - example - Install multiple packages in one command ##