From 40b11f86fb76911ba01d97a2a132eedb4dec6db7 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 1 Oct 2025 09:07:29 -0700 Subject: [PATCH] known_hosts: return rc and stderr in fail_json (#85871) * When ssh-keygen fails, return rc and stderr in fail_json in order to help debugging. Fixes: #85850 Signed-off-by: Abhijeet Kasurde (cherry picked from commit 6bee84318d681c5d582d7baf9f3bdee37b49b2e9) --- changelogs/fragments/known_hosts.yml | 3 +++ lib/ansible/modules/known_hosts.py | 8 +++++++- test/integration/targets/known_hosts/tasks/main.yml | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/known_hosts.yml diff --git a/changelogs/fragments/known_hosts.yml b/changelogs/fragments/known_hosts.yml new file mode 100644 index 00000000000..89363306781 --- /dev/null +++ b/changelogs/fragments/known_hosts.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - known_hosts - return rc and stderr when ssh-keygen command fails for further debugging (https://github.com/ansible/ansible/issues/85850). diff --git a/lib/ansible/modules/known_hosts.py b/lib/ansible/modules/known_hosts.py index 0c88e0f79c7..1d0c99fbc11 100644 --- a/lib/ansible/modules/known_hosts.py +++ b/lib/ansible/modules/known_hosts.py @@ -225,7 +225,13 @@ def sanity_check(module, host, key, sshkeygen): rc, stdout, stderr = module.run_command(sshkeygen_command) if stdout == '': # host not found - module.fail_json(msg="Host parameter does not match hashed host field in supplied key") + results = { + "msg": "Host parameter does not match hashed host field in supplied key", + "rc": rc, + } + if stderr: + results["stderr"] = stderr + module.fail_json(**results) def search_for_host_key(module, host, key, path, sshkeygen): diff --git a/test/integration/targets/known_hosts/tasks/main.yml b/test/integration/targets/known_hosts/tasks/main.yml index 2e5ecc7ebbc..507a3859970 100644 --- a/test/integration/targets/known_hosts/tasks/main.yml +++ b/test/integration/targets/known_hosts/tasks/main.yml @@ -507,3 +507,4 @@ that: - result is failed - result.msg == 'Host parameter does not match hashed host field in supplied key' + - result.rc is defined