Junaid
Junaid

Reputation: 153

Python code using Fabric does not use remote hosts

I am using python 3.6 with Fabric3 1.13.1 . Following is a simplified version of the code that I am using.

from fabric.api import *
from file import func 

env.hosts = ['user@myserver:22' ]
env.passwords={'user@myserver:22':'password'}

def test():

    func() # function defined in func 

This code is saved in the file named fabfile.py. When I run fab test in the terminal, execution starts as

['user@myserver:22' ] Executing task 'test'

But the resources at localhost are being used instead of at myserver, according to htop. And no processes are being started at myserver.

Does Fabric revert to localhost silently if it cannot find myserver ?

EDIT I also tried

execute(func(), hosts = ['myserver'])

in a separate python file, as well as in fabfile, both of these ways excecute on localhost !

EDIT 2
The only thing that seems to run on the remote host is run("commands_to_run") # This picks the host from the list env.hosts

Upvotes: 0

Views: 554

Answers (2)

Saher Ahwal
Saher Ahwal

Reputation: 9237

According to your Edit1, you are not using execute correctly.

The first argument in execute should be calling run. The difference is that the hosts will be picked up from second argument rather than env.hosts.

def func():
    run( "commands_to_run" )


execute(func, hosts = ['myserver'])

Upvotes: 1

Tim Shaffer
Tim Shaffer

Reputation: 166

You need to call run() to execute shell commands on the remote host.

http://docs.fabfile.org/en/1.14/api/core/operations.html#fabric.operations.run

For example:

def test():
    local("hostname")
    run("hostname")

    another_function()

This will run the hostname shell command on both the local computer and remote host.

Fabric does not support executing arbitrary Python functions on a remote host. another_function() will always run on the local computer.

Upvotes: 1

Related Questions