首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用fabric2在本地主机上启动命令?

如何使用fabric2在本地主机上启动命令?
EN

Stack Overflow用户
提问于 2018-07-26 18:30:19
回答 2查看 1.3K关注 0票数 1

下面是我的脚本:

代码语言:javascript
复制
from fabric2 import Connection

c = Connection('127.0.0.1')
with c.cd('/home/bussiere/'):
    c.run('ls -l')

但是我有这个错误:

代码语言:javascript
复制
paramiko.ssh_exception.AuthenticationException: Authentication failed.

那么如何在localhost上运行命令呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-29 00:36:52

在Fabric2中,Connection对象有一个local()方法。看一下这个对象的文档here

票数 1
EN

Stack Overflow用户

发布于 2020-07-13 23:50:01

在2020年7月,使用fabric2时,如果您不将参数传递给任务修饰器,则默认情况下您是在本地计算机上。

例如,以下代码将在本地计算机(localhost)上运行:

示例1:仅在本地

代码语言:javascript
复制
#python3
#fabfile.py


from fabric import task, Connection
c = Connection('remote_user@remote_server.com')


@task
def DetailList(c):
    c.run('ls -l') # will run on local server because the decorator @task does not contain the hosts parameter

然后,您可以使用以下命令在您的机器上运行

代码语言:javascript
复制
fab DetailList

如果你想混合应该在远程服务器和本地服务器上运行的代码,你应该将连接作为一个参数传递给@task装饰器。

示例2:在本地和远程上(但功能不同)

代码语言:javascript
复制
#python3
#fabfile.py

#imports
from fabric import task, Connection

#variables
list_of_hosts = ['user@yourserver.com'] #you should have already configure the ssh access
c = Connection(list_of_hosts[0])

working_dir = '/var/www/yourproject'

#will run on remote
@task(hosts = list_of_hosts)
def Update(c):
    c.run('sudo apt get update') # will run on remote server because hosts are passed to the task decorator
    c.run(f'cd {working_dir} && git pull') # will run on remote server because hosts are passed to the task decorator
    c.run('sudo service apache2 restart') # will run on remote server because hosts are passed to the task decorator

#will run on local because you do not specify a host
@task
def DetailsList(c):
    c.run('ls -l') # # will run on local server because hosts are NOT passed to the task decorator

正如Isma a所提到的,还有一个“local”方法可以在传递hosts参数时使用,“local”方法将在localhost上运行,尽管您已经为任务修饰器指定了host参数。请注意,如果您没有指定任何主机参数,则不能使用'local‘方法,而应使用run,如示例1和2所示。

示例3:在远程和本地服务器上都使用,但在相同的函数下,请注意,我们不会修饰在UpdateAndRestart函数中调用的函数。

代码语言:javascript
复制
#python3
#fabfile.py

#imports
from fabric import task, Connection

#variables
list_of_hosts = ['www.yourserver.com'] #you should have already configure the ssh access
c = Connection(list_of_hosts[0])
working_dir = '/var/www/yourproject'


def UpdateServer(c):
    c.run('sudo apt get update') # will run on remote server because hosts are passed to the task decorator
    c.local('echo the remote server is now updated') # will run on local server because you used the local method when hosts are being passed to the decorator


def PullFromGit(c):
    c.run(f'cd {working_dir} && git pull')  # will run on remote server because hosts are passed to the task decorator
    c.local('echo Git repo is now pulled')   # will run on local server because you used the local method when hosts are being passed to the decorator


def RestartServer(c):
    c.run('sudo service apache2 restart') # will run on remote server because hosts are passed to the task decorator
    c.local('echo Apache2 is now restarted') # will run on local server because you used the local method when hosts are being passed to the decorator

    
@task(hosts = list_of_hosts)
def UpdateAndRestart(c):
    UpdateServer(c)
    PullFromGit(c)
    RestartServer(c)
    c.local('echo you have updated, pulled and restarted Apache2') # will run on local server because you used the local method when hosts are being passed to the decorator

您将能够使用以下命令运行整个堆栈:

代码语言:javascript
复制
fab UpdateAndRestart
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51536696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档