When trying to solve this same problem in the ansible
synchronize
module, I came across this question and would like to draw some attention to the solution https://askubuntu.com/a/1263657/874618 by Simon Schmid. Can't upvote or comment using my account (not enough reputation points), so I'll post this as another answer:
Simon's solution not only is the safest alternative in that it requires no changes to sudoers
and passes the password using the environment (so that it will not pop up in history files and logs), it also works without changes to the target machine, which is very convenient if you want to rsync
to lots of fresh machines and do not want to reconfigure them just for this command.
After playing around with it a bit, I've managed to inline the shell commands used for wrapping ssh
on the host machine as well, so it is possible to just use a single rsync
line:
PASSWORD=<SUDOPASS> rsync -avzue '/bin/sh -c "{ echo $PASSWORD; cat - ; } | ssh $0 $* &"' --rsync-path "sudo -S rsync" SRC DST
Happy syncing ;-)