diff --git a/lib/ansible/modules/packaging/os/dnf.py b/lib/ansible/modules/packaging/os/dnf.py index d2afbdb7d2..4aa94f3beb 100644 --- a/lib/ansible/modules/packaging/os/dnf.py +++ b/lib/ansible/modules/packaging/os/dnf.py @@ -393,7 +393,10 @@ def ensure(module, base, state, names, autoremove): # best effort causes to install the latest package # even if not previously installed base.conf.best = True - base.install(pkg_spec) + try: + base.install(pkg_spec) + except dnf.exceptions.MarkingError as e: + failures.append((pkg_spec, to_native(e))) else: # state == absent diff --git a/test/integration/targets/dnf/tasks/dnf.yml b/test/integration/targets/dnf/tasks/dnf.yml index 70631ff8b8..811e8475e5 100644 --- a/test/integration/targets/dnf/tasks/dnf.yml +++ b/test/integration/targets/dnf/tasks/dnf.yml @@ -192,7 +192,7 @@ - sharutils state: removed -- name: install non-existent rpm +- name: install non-existent rpm dnf: name: "{{ item }}" with_items: @@ -410,3 +410,19 @@ that: - "'changed' in dnf_result" - "'results' in dnf_result" + +# https://github.com/ansible/ansible/issues/39704 +- name: install non-existent rpm, state=latest + dnf: + name: non-existent-rpm + state: latest + ignore_errors: yes + register: dnf_result + +- name: verify the result + assert: + that: + - "dnf_result is failed" + - "'non-existent-rpm' in dnf_result['failures'][0]" + - "'no package matched' in dnf_result['failures'][0]" + - "'Failed to install some of the specified packages' in dnf_result['msg']"