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