While writing my pyinfra deploy scripts, I first tried to use sudo_user as that's what I do normally, and was automating my exact steps. I kept hitting errors on operations inside the postgresql package, where sudo_user="postgres" was not being applied to the subsequent call to psql.
This snippet reproduces the issue, I can reproduce with any other user as well, but I'm using postgres since that comes by default on Ubuntu's PostgreSQL installs. I SSH to the machine as root:
from pyinfra.operations import server
server.shell(commands="id -u -n", sudo_user="postgres")
And this is my output with pyinfra -vvv:
[...]
--> Beginning operation run...
--> Starting operation: Server/Shell (commands=id -u -n)
[...] >>> sh -c 'id -u -n'
[...] root
[...] Success
[...]
If I do it directly, here's what I get:
root@somewhere:~# sudo -u postgres id -u -n
postgres
While sudo_user does not work, su_user does, even though they're implemented differently, the behavior should be the same.
pyinfra --output:
System: Linux
Platform: Linux-5.10.25_1-x86_64-with-glibc2.32
Release: 5.10.25_1
Machine: x86_64
pyinfra: v1.3.10
Executable: /home/luna/.local/bin/pyinfra
Python: 3.9.4 (CPython, GCC 10.2.1 20201203)
One thing to keep in mind is my inventory file when I run the deploy script:
$ cat inventory/website.py
website_hosts = [("...", {"ssh_user": "root"})]
Maybe ssh_user overwrites sudo_user in the SSH connector? I would assume they would come one after the other (login as the user defined in ssh_user, and then invoke sudo -u {value_of_sudo_user})
Hi @lun-4, thank you for submitting this! The ssh_user value shouldn't overwrite sudo_user ever, it certainly looks like the sudo_user flag is being ignored, which is unexpected. I'm hoping to get a chance to look at this today/over the weekend!
@lun-4 I've just realized the issue here (staring me in the face!) - for sudo_user to work sudo=True must also be set! So:
from pyinfra.operations import server
server.shell(commands="id -u -n", sudo_user="postgres", sudo=True)
This is a poor user experience and I'm keen to fix that. I think a warning should show in this case, as I'd rather avoid implicit setting of sudo when sudo_user is set (also see: https://github.com/Fizzadar/pyinfra/issues/414#issue-679713555).
This has now been released in v1.4!