From 7c243a4255e5bb48684191ed5a68223ffb84c7fc Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 18 Apr 2024 16:00:17 -0400 Subject: [PATCH] dpkg_selections fixes for 'unprimed' pkgs --- changelogs/fragments/dpkg_selections_fix.yml | 2 ++ lib/ansible/modules/dpkg_selections.py | 17 +++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/dpkg_selections_fix.yml diff --git a/changelogs/fragments/dpkg_selections_fix.yml b/changelogs/fragments/dpkg_selections_fix.yml new file mode 100644 index 00000000000..fc09be4edb3 --- /dev/null +++ b/changelogs/fragments/dpkg_selections_fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - dpkg_selections now handles existing, but not primed, packages correctly. diff --git a/lib/ansible/modules/dpkg_selections.py b/lib/ansible/modules/dpkg_selections.py index b591636802d..3c5d06d1cdd 100644 --- a/lib/ansible/modules/dpkg_selections.py +++ b/lib/ansible/modules/dpkg_selections.py @@ -76,19 +76,24 @@ def main(): # Get current settings. rc, out, err = module.run_command([dpkg, '--get-selections', name], check_rc=True) - if 'no packages found matching' in err: - module.fail_json(msg="Failed to find package '%s' to perform selection '%s'." % (name, selection)) - elif not out: - current = 'not present' - else: + if rc == 0: current = out.split()[1] + elif 'no packages found matching' in err: + current = 'not found' + else: + current = 'not present' changed = current != selection if module.check_mode or not changed: module.exit_json(changed=changed, before=current, after=selection) - module.run_command([dpkg, '--set-selections'], data="%s %s" % (name, selection), check_rc=True) + rc, out, err = module.run_command([dpkg, '--set-selections'], data="%s %s" % (name, selection), check_rc=True) + if rc != 0: + if 'package not in' in err: + module.fail_json(msg="Failed to find package '%s' to perform selection '%s'." % (name, selection)) + module.fail_json(msg="Unexpected error when setting package '%s' to '%s': %s." % (name, selection, err)) + module.exit_json(changed=changed, before=current, after=selection)