Run ssh-add using Fabric in the machine

I have some deployment tasks with Fabric that need to check / update the Mercurial repository on the computer and then do the appropriate copy / configuration.

Every time I install a new machine (we are currently using EC2 for our infrastructure), or when I start hg pullin the machine, it asks for my ssh key passphrase, which is a little annoying when we need to initialize a dozen machines at a time.

I try to start ssh-addin Fabric when a new EC2 instance is initialized, but it ssh-agentdoes not seem to work for this shell, and I get a message Could not open a connection to your authentication agent.from Fabric output.

How do I do ssh-addwork when connecting to an instance using Fabric script?

+5
source share
1 answer

A comment on the issue of tracking problems on the fabric solved this for me. This is a modified version of the lincolnloop solution . Using this “run” instead of text will translate your commands through ssh locally, allowing the local ssh agent to provide keys.

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 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))

Note that I am running Fabric 1.3.2, and this fix will no longer be needed.

+2
source

All Articles