[python] Fabric 사용시 ~ / .ssh / config에 나열된 호스트에 연결

Fabric있는 호스트를 인식하지 못하는 데 문제 가 있습니다 ~/.ssh/config.

내는 fabfile.py다음과 같습니다.

from fabric.api import run, env

env.hosts = ['lulu']

def whoami():
    run('whoami')

달리기 $ fab whoami는 다음을 제공합니다.

[루루] 달리기 : whoami

치명적인 오류 : lulu에 대한 이름 조회 실패

이름 lulu~/.ssh/config다음과 같이 내에 있습니다.

Host lulu
     hostname 192.168.100.100
     port 2100
     IdentityFile ~/.ssh/lulu-key

이 같은 추가하고 해결하는 내 첫번째 생각 lulu.lulu에을 /etc/hosts나는 또한 직물에 신원 파일에 통과해야 다음 (내가 맥에있어)하지만 – 내가 아니라 (즉, 내 인증을 유지하는 것이 ~/.ssh/config) (내 배포에서 분리 즉 fabfile.py).

또한 부수적으로 호스트 파일의 호스트에 연결하려고하면에서 fabric.contrib.projects.rsync_project‘포트’를 인식하지 않는 것 같습니다 hosts.env(즉,에 연결을 시도 hosts.env = [lulu:2100]하는 rsync_project것처럼 보이는 호출을 사용 하는 경우 lulu:21).

Fabric이이 lulu이름을 인식하지 못하는 이유가 있습니까?



답변

버전 1.4.0부터 Fabric은 ssh 구성 (일부)을 사용합니다 . 그러나 다음과 같이 명시 적으로 활성화해야합니다.

env.use_ssh_config = True

fabfile의 상단 근처에 있습니다. 이렇게하면 Fabric은 ssh 구성을 읽어야합니다 ( ~/.ssh/config기본적으로 또는에서 env.ssh_config_path).

한 가지 경고 : 1.5.4 이전 버전을 사용하는 경우이 env.use_ssh_config설정되어 있지만 구성 파일이없는 경우 중단이 발생합니다 . 이 경우 다음과 같은 해결 방법을 사용할 수 있습니다.

if env.ssh_config_path and os.path.isfile(os.path.expanduser(env.ssh_config_path)):
    env.use_ssh_config = True


답변

이름이에없는 경우에도 마찬가지입니다 /etc/hosts. 나는 같은 문제가 있었고 그 파일과 ~/.ssh/config.


답변

업데이트 :이 답변 은 이제 구식 입니다.


Fabric은 현재 .ssh / config 파일을 지원하지 않습니다. 이러한 기능을 함수에서 설정 한 다음 CLI에서 호출 할 수 있습니다. 예 : fab 생산 작업; 여기서 프로덕션은 사용자 이름, 호스트 이름, 포트 및 ssh ID를 설정합니다.

rsync 프로젝트의 경우 포트 설정 기능이 있어야합니다. 그렇지 않은 경우 기본적으로 기여한 함수가 수행하는 작업이므로 항상 local ( “rsync …”)을 실행할 수 있습니다.


답변

다음 코드를 사용하여 구성을 읽을 수 있습니다 (원본 코드 : http://markpasc.typepad.com/blog/2010/04/loading-ssh-config-settings-for-fabric.html ).

from fabric.api import *
env.hosts = ['servername']

def _annotate_hosts_with_ssh_config_info():
    from os.path import expanduser
    from paramiko.config import SSHConfig

    def hostinfo(host, config):
        hive = config.lookup(host)
        if 'hostname' in hive:
            host = hive['hostname']
        if 'user' in hive:
            host = '%s@%s' % (hive['user'], host)
        if 'port' in hive:
            host = '%s:%s' % (host, hive['port'])
        return host

    try:
        config_file = file(expanduser('~/.ssh/config'))
    except IOError:
        pass
    else:
        config = SSHConfig()
        config.parse(config_file)
        keys = [config.lookup(host).get('identityfile', None)
            for host in env.hosts]
        env.key_filename = [expanduser(key) for key in keys if key is not None]
        env.hosts = [hostinfo(host, config) for host in env.hosts]

        for role, rolehosts in env.roledefs.items():
            env.roledefs[role] = [hostinfo(host, config) for host in rolehosts]

_annotate_hosts_with_ssh_config_info()


답변