Skip to content

Commit

Permalink
dpkg_selections: Check if package exists before selection operation (#…
Browse files Browse the repository at this point in the history
…81406)

* dpkg_selections: Check if the package exists before the selection operation

Fixes: #81404

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Aug 3, 2023
1 parent 95fdd55 commit f10d11b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelogs/fragments/dpkg_selections.yml
@@ -0,0 +1,3 @@
---
bugfixes:
- dpkg_selections - check if the package exists before performing the selection operation (https://github.com/ansible/ansible/issues/81404).
9 changes: 8 additions & 1 deletion lib/ansible/modules/dpkg_selections.py
Expand Up @@ -54,6 +54,7 @@
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.locale import get_best_parsable_locale


def main():
Expand All @@ -67,12 +68,18 @@ def main():

dpkg = module.get_bin_path('dpkg', True)

locale = get_best_parsable_locale(module)
DPKG_ENV = dict(LANG=locale, LC_ALL=locale, LC_MESSAGES=locale, LC_CTYPE=locale)
module.run_command_environ_update = DPKG_ENV

name = module.params['name']
selection = module.params['selection']

# Get current settings.
rc, out, err = module.run_command([dpkg, '--get-selections', name], check_rc=True)
if not out:
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:
current = out.split()[1]
Expand Down
Expand Up @@ -87,3 +87,15 @@
apt:
name: hello
state: absent

- name: Try to select non-existent package
dpkg_selections:
name: kernel
selection: hold
ignore_errors: yes
register: result

- name: Check that module fails for non-existent package
assert:
that:
- "'Failed to find package' in result.msg"

0 comments on commit f10d11b

Please sign in to comment.