whi
whi

Reputation: 2750

fabric API direct call in python

i remember fabric API can be called directly in py script but forgot where to start anybody give a clue?

Upvotes: 10

Views: 9968

Answers (2)

Morgan
Morgan

Reputation: 4131

There is a whole section on using fabric as a library in the docs. I'd refer to that and how best to approach fabric's use in this way.

Specifically, you'll want to address the problem of performing task X on a list of hosts Y by using the execute function as execute( X, hosts=Y ). When you are done, you should disconnect from all of the connected hosts. Normally the fab tool does this for you, but you must do this manually when using fabric as a library. Fabric 0.9.4 and later have a disconnect_all() function. Ideally, this should be in the finally clause of a try...finally statement.

Upvotes: 6

Fedor Gogolev
Fedor Gogolev

Reputation: 10551

Yes, you can call it, for example:

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


def do_something():
    run("echo $RANDOM")

if __name__ == "__main__":
    execute(do_something, hosts=["username@host"])

Upvotes: 26

Related Questions