Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No hosts found: Fabric

Tags:

python

fabric

when I run my python code it is asking for host.

No hosts found. Please specify (single) host string for connection:

I have the following code:

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();

I even tried running fab with -H option and I am getting the same message. I'm using Ubuntu 10.10 any help is appreciated. Btw I am a newbie in Python.

like image 343
bonzi Avatar asked Apr 03 '11 15:04

bonzi


3 Answers

In order to get hosts to work in a script outside of the fab command-line tool and fabfile.py, you'll have to use execute():

from fabric.api import run
from fabric.tasks import execute

def mytask():
    run('uname -a')

results = execute(mytask)
like image 82
Gringo Suave Avatar answered Oct 22 '22 09:10

Gringo Suave


If it's only one host, you can use env.host_string = 'somehost or ipaddress'.

You also don’t need the ; at the end of your 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()
like image 15
Colin Wood Avatar answered Oct 22 '22 07:10

Colin Wood


I am not exactly sure what remoteRun(); is supposed to do in your example.

Is it part of your fabfile or is this your terminal command to invoke the script?

The correct way would be a command like this in your shell:

fab remoteRun

Generally it's better to specify the concrete hosts your command is supposed to run on like this:

def localhost():
    env.hosts = [ '127.0.0.1']

def remoteRun():
    print "ENV %s" %(env.hosts)
    out = run('uname -r')
    print "Output %s"%(out)

You can run it like this from a terminal (assuming you are in the directory that contains your fabfile):

fab localhost remoteRun

As an alternative you could specify the host with the -H parameter:

fab -H 127.0.0.1 remoteRun

If you have a list of hosts you want to invoke the command for, do it like this: http://readthedocs.org/docs/fabric/latest/usage/execution.html

Adjusted to your example:

env.hosts = [ 'localhost', '127.0.0.1']

def remoteRun():
    print "ENV %s" %(env.hosts)
    out = run('uname -r')
    print "Output %s"%(out)

And called via: fab remoteRun

This way the remoteRun is performed on all hosts in env.hosts.

like image 6
arie Avatar answered Oct 22 '22 08:10

arie