From b3408ab80b67853d74e9898c708f6cc45374218e Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:37:34 -0400 Subject: [PATCH] Fix dnf module crash for non-existent url (#81801) * ci_complete ci_coverage --- changelogs/fragments/fix-dnf-install-missing-url.yml | 2 ++ lib/ansible/module_utils/urls.py | 2 +- test/integration/targets/dnf/tasks/dnf.yml | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/fix-dnf-install-missing-url.yml diff --git a/changelogs/fragments/fix-dnf-install-missing-url.yml b/changelogs/fragments/fix-dnf-install-missing-url.yml new file mode 100644 index 00000000000000..c327f59b84c770 --- /dev/null +++ b/changelogs/fragments/fix-dnf-install-missing-url.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix handling missing urls in ansible.module_utils.urls.fetch_file for Python 3. diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 4a462f5d43fa60..c33319270a0558 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -2058,7 +2058,7 @@ def fetch_file(module, url, data=None, headers=None, method=None, try: rsp, info = fetch_url(module, url, data, headers, method, use_proxy, force, last_mod_time, timeout, unredirected_headers=unredirected_headers, decompress=decompress, ciphers=ciphers) - if not rsp: + if not rsp or (rsp.code and rsp.code >= 400): module.fail_json(msg="Failure downloading %s, %s" % (url, info['msg'])) data = rsp.read(bufsize) while data: diff --git a/test/integration/targets/dnf/tasks/dnf.yml b/test/integration/targets/dnf/tasks/dnf.yml index 61c523f62774e2..362ad367e2571f 100644 --- a/test/integration/targets/dnf/tasks/dnf.yml +++ b/test/integration/targets/dnf/tasks/dnf.yml @@ -510,6 +510,7 @@ that: - "dnf_result is failed" - "not dnf_result.changed" + - "'Failure downloading' in dnf_result.msg or 'Failed to download' in dnf_result.msg" - name: verify dnf module outputs assert: