[dnf] show installations/removals in check_mode (#70892)

Change:
- Previously, we only showed that something would have changed, not what
  would have changed. This allows us to show what will chang as well.

Test Plan:
- Local RHEL8 VM
- New integration tests

Tickets:
- Fixes #66132

Signed-off-by: Rick Elrod <rick@elrod.me>
pull/70950/head
Rick Elrod 4 years ago committed by GitHub
parent ae42d5ebdc
commit 7d32129efb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
minor_changes:
- dnf - now shows specific package changes (installations/removals) under ``results`` in check_mode. (https://github.com/ansible/ansible/issues/66132)

@ -1158,6 +1158,18 @@ class DnfModule(YumDnf):
self.module.exit_json(**response)
else:
response['changed'] = True
# If packages got installed/removed, add them to the results.
# We do this early so we can use it for both check_mode and not.
if self.download_only:
install_action = 'Downloaded'
else:
install_action = 'Installed'
for package in self.base.transaction.install_set:
response['results'].append("{0}: {1}".format(install_action, package))
for package in self.base.transaction.remove_set:
response['results'].append("Removed: {0}".format(package))
if failure_response['failures']:
failure_response['msg'] = 'Failed to install some of the specified packages'
self.module.fail_json(**failure_response)
@ -1178,15 +1190,11 @@ class DnfModule(YumDnf):
)
if self.download_only:
for package in self.base.transaction.install_set:
response['results'].append("Downloaded: {0}".format(package))
# No further work left to do, and the results were already updated above.
# Just return them.
self.module.exit_json(**response)
else:
self.base.do_transaction()
for package in self.base.transaction.install_set:
response['results'].append("Installed: {0}".format(package))
for package in self.base.transaction.remove_set:
response['results'].append("Removed: {0}".format(package))
if failure_response['failures']:
failure_response['msg'] = 'Failed to install some of the specified packages'

@ -48,6 +48,20 @@
- "not dnf_result.changed"
# INSTALL
- name: install sos (check_mode)
dnf:
name: sos
state: present
update_cache: True
check_mode: True
register: dnf_result
- assert:
that:
- dnf_result is success
- dnf_result.results|length > 0
- "dnf_result.results[0].startswith('Installed: ')"
- name: install sos
dnf:
name: sos
@ -74,6 +88,18 @@
- "'results' in dnf_result"
# INSTALL AGAIN
- name: install sos again (check_mode)
dnf:
name: sos
state: present
check_mode: True
register: dnf_result
- assert:
that:
- dnf_result is not changed
- dnf_result.results|length == 0
- name: install sos again
dnf:
name: sos
@ -188,12 +214,33 @@
- "rpm_sos_result.rc == 0"
- "rpm_pciutils_result.rc == 0"
- name: uninstall sos and pciutils (check_mode)
dnf:
name:
- sos
- pciutils
state: removed
check_mode: True
register: dnf_result
- assert:
that:
- dnf_result is success
- dnf_result.results|length == 2
- "dnf_result.results[0].startswith('Removed: ')"
- "dnf_result.results[1].startswith('Removed: ')"
- name: uninstall sos and pciutils
dnf:
name:
- sos
- pciutils
state: removed
register: dnf_result
- assert:
that:
- dnf_result is changed
- name: install non-existent rpm
dnf:
@ -238,6 +285,19 @@
name: sos
state: absent
- name: Test download_only (check_mode)
dnf:
name: sos
state: latest
download_only: true
check_mode: true
register: dnf_result
- assert:
that:
- dnf_result is success
- "dnf_result.results[0].startswith('Downloaded: ')"
- name: Test download_only
dnf:
name: sos

Loading…
Cancel
Save