Check the logs on the remote host to see what goes wrong: either the ssh connection isn’t working or the sudo after that.
Is this a single server foreman installation or do you use different smart proxies for the ssh connections? The proxy basically does something like this as ssh connection
# sudo -u foreman-proxy ssh -i ~foreman-proxy/.ssh/id_rsa_foreman_proxy <ssh_user>@remote.example.com ‘sudo -u root /bin/ls’