尝试使用结构2和身份文件连接到ssh config中描述的主机。
con = Connection('my_host')
@task
def tt(c):
con.run('uname -a')
〜/ .ssh / config:
Host my_host
HostName 123.144.76.84
User ubuntu
IdentityFile ~/.keys/somekey
它失败了
paramiko.ssh_exception.AuthenticationException: Authentication failed.
虽然终端上的
$ ssh my_host
有效。我试图用相同的结果做
fab -i ~/.keys/somekey tt
。
最佳答案
构造accepts a hosts iterable作为任务中的参数。根据文档:
An iterable of host-connection specifiers appropriate for eventually instantiating a Connection. The existence of this argument will trigger automatic parameterization of the task when invoked from the CLI, similar to the behavior of --hosts.
其成员之一可以是:
A string appropriate for being the first positional argument to Connection - see its docs for details, but these are typically shorthand-only convenience strings like hostname.example.com or user@host:port.
对于您的示例,请对fabfile.py尝试以下操作:
host_list = ["my_host"]
@task(hosts=host_list)
def tt(c):
c.run('uname -a')
或者,您可以完全省略fabfile中的主机声明。如果未在
fabfile.py
中指定主机,则只需在调用fab
cli实用程序时将其指定为主机。如果您的fabfile.py是这样的:@task
def tt(c):
c.run('uname -a')
现在,您将运行
fab -H my_host tt
,以通过SSH客户端配置在别名tt
上运行它。希望这可以帮助。
关于python - 使用身份的Fabrics 2.x ssh连接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281810/