summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRick Elrod <rick@elrod.me>2020-07-28 10:23:55 -0500
committerRick Elrod <rick@elrod.me>2020-10-23 14:06:56 -0500
commit2beeeb8a27b22499a70d199a98a50da7e54d1e94 (patch)
tree6136415edb905ecf47799feaf5a112f51a35d074
parent49d5462f16bb4e1edb0409a2badd14de8cdef2c1 (diff)
downloadansible-2beeeb8a27b22499a70d199a98a50da7e54d1e94.tar.gz
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>
-rw-r--r--changelogs/fragments/66132_dnf_show_pkgs_in_check_mode.yml2
-rw-r--r--lib/ansible/modules/packaging/os/dnf.py20
-rw-r--r--test/integration/targets/dnf/tasks/dnf.yml60
3 files changed, 76 insertions, 6 deletions
diff --git a/changelogs/fragments/66132_dnf_show_pkgs_in_check_mode.yml b/changelogs/fragments/66132_dnf_show_pkgs_in_check_mode.yml
new file mode 100644
index 0000000000..7ec57b78bf
--- /dev/null
+++ b/changelogs/fragments/66132_dnf_show_pkgs_in_check_mode.yml
@@ -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)
diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py
index 8fbcc5e73b..189473ccd9 100644
--- a/lib/ansible/modules/packaging/os/dnf.py
+++ b/lib/ansible/modules/packaging/os/dnf.py
@@ -1180,6 +1180,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)
@@ -1220,15 +1232,11 @@ class DnfModule(YumDnf):
self.module.fail_json(msg=msg)
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'
diff --git a/test/integration/targets/dnf/tasks/dnf.yml b/test/integration/targets/dnf/tasks/dnf.yml
index 24e7b8073c..5d9048fd41 100644
--- a/test/integration/targets/dnf/tasks/dnf.yml
+++ b/test/integration/targets/dnf/tasks/dnf.yml
@@ -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:
@@ -240,6 +287,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