fabric run命令作为系统用户

时间:2014-07-13 15:16:41

标签: python fabric

如何从登录用户切换到系统用户并以该用户身份运行命令。使用面料。

def sometask():
    run('mkdir foo')

@hosts(user1@host)
def dothis()
    with change_user(user2):<-- this is what i want to do change user to user2 who is a system user with no password and can't login via ssh
        with cd('~'): <-- cd to users ~
            sometask() <-- run this as user2
            yetanothertask() <-- run this as user2
            .............

1 个答案:

答案 0 :(得分:0)

您可以通过使用sudo()允许简单切换,并让它为用户提供一个变量,您可以使用您想要创建的目标上下文管理器进行切换。这看起来有点像:

def set_user(user):
    #Make into context manager
    env.custom_user = user

def bar():
    with set_user("foo"):
        execute("sometask")

def sometask():
    sudo("command", user=env.custom_user)