切换到不同的用户使用面料 [英] switch to different user using fabric

查看:178
本文介绍了切换到不同的用户使用面料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我开始研究面料进行远程部署。我需要切换到一个diff用户(从我登录的用户),并且无法弄清楚。甚至有可能吗?我目前的用户没有 sudo 权限。



我尝试更改以下环境变量

  env.sudo_prefix = su newUser -c
env.sudo_prompt =密码:

但是织物不等待'newUser'的密码输入,并且失败。

 出:密码:
[oldUser@ec2-111-11-111-111.compute-1.amazonaws .com] out:su:不正确的密码

致命错误:sudo()在执行时收到非零返回代码1!

请求:触摸x
执行:su newUser -c -uroot/ bin / bash -l -ccd / home / oldUser / upgrade&&& touch x

中止。
断开与oldUser@ec2-111-11-111-111.compute-1.amazonaws.com完成。

更新:



由于JF Sebastian建议, su newUser -c 的作品,但它会为每个服务器的每个命令提示密码,哪种失败了自动化的目的。根据提示(在这种情况下,它总是密码:

解决方案

感谢 JF Sebastian ,有几个抓住。


  1. 织物使连接懒惰,所以我必须在调用su之前做一个虚拟连接,以避免上下文切换。

  2. Pwd需要存储在全局范围内,以便可以重复使用。织物不会将其放在缓存中以覆盖su命令。

这是最后做的。它的工作。

  pwd =无
@hosts('myhost.com')
def test():
与cd('/ home / oldUser / upgrade'):
run('ls')#这是要积极地连接(而不是懒惰)
全局pwd#更改pwd $的范围b $ b如果pwd为None:
pwd = getpass.getpass('为newUser输入密码)

execute(su,pwd,'newUser','touch x')
run('ls')
execute(su,pwd,'newUser','rm x')
run('ls')

def su(pwd,用户,命令):
设置(
password =%s%pwd,
sudo_prefix =su%s -c%user,
sudo_prompt =密码:
):
sudo(command)


I recently started looking at fabric for remote deployment. I need to switch to a diff user (from the one that I login as) and am not able to figure it out. Is it even possible, if so how? My current user doesnt have sudo permissions.

I tried changing following environment variables

env.sudo_prefix = "su newUser -c "
env.sudo_prompt = "Password:"

But fabric does not wait for password input for 'newUser' and fails.

out: Password: 
[oldUser@ec2-111-11-111-111.compute-1.amazonaws.com] out: su: incorrect password

Fatal error: sudo() received nonzero return code 1 while executing!

Requested: touch x
Executed: su newUser -c  -u "root"  /bin/bash -l -c "cd /home/oldUser/upgrade && touch x"

Aborting.
Disconnecting from oldUser@ec2-111-11-111-111.compute-1.amazonaws.com... done.

Update:

As J.F. Sebastian suggested, su newUser -c works, but it prompts password for every command for every server, which kind of defeats the purpose of automation. Is there any way in Fabric to pass in same value based on prompt (in this case, its always Password:)

解决方案

Thanks J F Sebastian, There were couple of catches.

  1. Fabric makes connections lazily, so I had to make a dummy connection before invoking su to avoid context switch.
  2. Pwd need to be stored in global scope and so that it can be reused. Fabric doesnt put it in cache for overridden su command.

Here is what ended up doing. Its working.

pwd = None
@hosts('myhost.com')
def test():
    with cd('/home/oldUser/upgrade'):
        run('ls')  #This is to connect aggressively (instead of lazily)
        global pwd  #Change the scope of pwd
        if pwd is None:
            pwd = getpass.getpass('enter password for newUser')

        execute(su, pwd, 'newUser', 'touch x')  
        run ('ls')
        execute(su, pwd, 'newUser', 'rm x') 
        run ('ls')

def su(pwd, user, command):
    with settings(
        password= "%s" % pwd,
        sudo_prefix="su %s -c " % user,
        sudo_prompt="Password:"
        ):
        sudo(command)

这篇关于切换到不同的用户使用面料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆