在机器中运行带有Fabric的ssh-add

vic*_*pos 5 python ssh fabric

我正在使用Fabric运行一些部署任务,需要将Mercurial存储库签出/更新到机器,然后执行适当的复制/配置.

每次我安装一台新机器(我们目前正在使用EC2作为我们的基础设施)或者当我hg pull在机器上运行时它会询问我的ssh密钥密码,当我们需要在一台机器上初始化十几台机器时,这有点烦人时间.

我尝试ssh-add在新的EC2实例初始化时运行Fabric,但似乎ssh-agent没有为该shell运行,我Could not open a connection to your authentication agent.从Fabric的输出中得到一条消息.

ssh-add当Fabric脚本连接到实例时,我将如何工作?

sch*_*pet 3

对 Fabric 问题跟踪器的评论为我解决了这个问题。它是lincolnloop 解决方案的修改版本。使用此“运行”而不是 Fabric 的“运行”将通过本地 ssh 传输您的命令,从而允许您的本地 ssh 代理提供密钥。

from fabric.api import env, roles, local, output
from fabric.operations import _shell_escape

def run(command, shell=True, pty=True):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent. 
    This helper uses your system's ssh to do so.
    """
    real_command = command
    if shell:
        cwd = env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        real_command = '%s "%s"' % (env.shell,
            _shell_escape(cwd + real_command))
    if output.debug:
        print("[%s] run: %s" % (env.host_string, real_command))
    elif output.running:
        print("[%s] run: %s" % (env.host_string, command))
    local("ssh -A %s '%s'" % (env.host_string, real_command))
Run Code Online (Sandbox Code Playgroud)

请注意,我正在运行 Fabric 1.3.2,并且不再需要此修复。