[英]No hosts found: Fabric
當我運行我的python代碼時,它要求主機。
找不到主機。 請指定(單個)主機字符串以進行連接:
我有以下代碼:
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = [ 'ipaddress' ]
def remoteRun():
print "ENV %s" %(env.hosts)
out = run('uname -r')
print "Output %s"%(out)
remoteRun();
我甚至嘗試使用-H選項運行fab,我收到相同的消息。 我正在使用Ubuntu 10.10任何幫助表示贊賞。 順便說一下,我是Python的新手。
為了讓主機在fab命令行工具和fabfile.py之外的腳本中工作, 你必須使用execute() :
from fabric.api import run
from fabric.tasks import execute
def mytask():
run('uname -a')
results = execute(mytask)
如果它只是一個主機,則可以使用env.host_string = 'somehost or ipaddress'
。
你也不需要;
在你的remoteRun
結束時。
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.api import env, run
env.host_string = 'ipaddress'
def remoteRun():
print "ENV %s" %(env.hosts)
out = run('uname -r')
print "Output %s"%(out)
remoteRun()
我不確定remoteRun();
應該在你的例子中做。
它是fabfile的一部分還是這個調用腳本的終端命令?
正確的方法是在shell中使用這樣的命令:
fab remoteRun
通常,最好指定命令應該運行的具體主機,如下所示:
def localhost():
env.hosts = [ '127.0.0.1']
def remoteRun():
print "ENV %s" %(env.hosts)
out = run('uname -r')
print "Output %s"%(out)
您可以從終端運行它(假設您在包含fabfile的目錄中):
fab localhost remoteRun
作為替代方法,您可以使用-H參數指定主機:
fab -H 127.0.0.1 remoteRun
如果您有要為其調用命令的主機列表,請執行以下操作: http : //readthedocs.org/docs/fabric/latest/usage/execution.html
調整到您的示例:
env.hosts = [ 'localhost', '127.0.0.1']
def remoteRun():
print "ENV %s" %(env.hosts)
out = run('uname -r')
print "Output %s"%(out)
並通過: fab remoteRun
這樣, remoteRun
就在env.hosts
所有主機上env.hosts
。
@Nerdatastic是對的,簡單來說:不要使用env.hosts,而是使用env.host_string。 例如
def setup_db_server
env.host_string = 'db01.yoursite.com' # or the ip address
run("mysqladmin ...")
end
並運行$ fab setup_db_server
將在目標服務器上執行該腳本。
Nerdatastic是正確的,您需要為fabric指定env.host_string,以了解要使用的主機字符串。 我遇到了這個問題,試圖使用Task的子類並調用run()方法。 它似乎忽略了env.hosts,除了在1.3版本中使用fabric.tasks中的execute時。
我有同樣的問題。
我認為這是一個錯誤。 因為今天之前所有工作。 我將我的env存儲在.fabricrc中。
現在我有與你相同的信息。 不知道為什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.