如何使用 fabric2 在本地主机上启动命令?
How to launch command on localhost with fabric2?
这是我的脚本:
from fabric2 import Connection
c = Connection('127.0.0.1')
with c.cd('/home/bussiere/'):
c.run('ls -l')
但是我有这个错误:
paramiko.ssh_exception.AuthenticationException: Authentication failed.
那么我如何 运行 在本地主机上执行命令?
在 Fabric2 中,Connection
对象有一个 local()
方法。
查看此对象的文档 here.
截至 2020 年 7 月,对于 fabric2,如果默认情况下您不将参数传递给任务装饰器,则您位于本地计算机上。
例如,以下将 运行 在您的本地计算机 (localhost) 上:
示例 1:仅在本地
#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
然后您将 运行 在您的计算机上
fab DetailList
如果您想在远程服务器和本地混合使用 运行ning 代码,您应该将连接作为参数传递给 @task 装饰器。
示例 2:本地和远程(但功能不同)
#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ïl 所提到的,还有一个 'local' 方法可以在传递 hosts 参数时使用,'local' 方法将 运行 在本地主机上,尽管您已经指定了任务装饰器的主机参数。请注意,如果您没有指定任何主机参数,则不能使用 'local' 方法,请改用 运行,如示例 1 和 2 所示。
示例 3:在远程和本地服务器上使用相同的函数,请注意,我们没有装饰在 UpdateAndRestart 函数中调用的函数。
#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
您将能够 运行 整个堆栈:
fab UpdateAndRestart
这是我的脚本:
from fabric2 import Connection
c = Connection('127.0.0.1')
with c.cd('/home/bussiere/'):
c.run('ls -l')
但是我有这个错误:
paramiko.ssh_exception.AuthenticationException: Authentication failed.
那么我如何 运行 在本地主机上执行命令?
在 Fabric2 中,Connection
对象有一个 local()
方法。
查看此对象的文档 here.
截至 2020 年 7 月,对于 fabric2,如果默认情况下您不将参数传递给任务装饰器,则您位于本地计算机上。
例如,以下将 运行 在您的本地计算机 (localhost) 上:
示例 1:仅在本地
#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
然后您将 运行 在您的计算机上
fab DetailList
如果您想在远程服务器和本地混合使用 运行ning 代码,您应该将连接作为参数传递给 @task 装饰器。
示例 2:本地和远程(但功能不同)
#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ïl 所提到的,还有一个 'local' 方法可以在传递 hosts 参数时使用,'local' 方法将 运行 在本地主机上,尽管您已经指定了任务装饰器的主机参数。请注意,如果您没有指定任何主机参数,则不能使用 'local' 方法,请改用 运行,如示例 1 和 2 所示。
示例 3:在远程和本地服务器上使用相同的函数,请注意,我们没有装饰在 UpdateAndRestart 函数中调用的函数。
#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
您将能够 运行 整个堆栈:
fab UpdateAndRestart