Search code examples
pythonfabric

No hosts found: 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.


Solution

  • 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.