Here are three different approaches all using the execute
method
from fabric.api import env,run,execute,hosts
# 1 - Set the (global) host_string
env.host_string = "[email protected]"
def foo():
run("ps")
execute(foo)
# 2 - Set host string using execute's host param
execute(foo, hosts=['[email protected]'])
# 3 - Annotate the function and call it using execute
@hosts('[email protected]')
def bar():
run("ps -ef")
execute(bar)
For using keyfiles, you'll need to set either env.key
or env.key_filename
, as so:
env.key_filename = 'path/to/my/id_rsa'
# Now calls with execute will use this keyfile
execute(foo, hosts=['[email protected]'])
You can also supply multiple keyfiles and whichever one logs you into that host will be used