Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target a different host inside a Fabric command

Tags:

python

fabric

How do you specify a different target to run a command on other than the one that's currently set for the running Fabric command?

I have a command download_backup() that downloads a database backup file to the localhost from a remote host. Since it has to run locally, the host_string is localhost. However, I need to run a command on the remote host to find what the most recent backup is. When I try to do:

def download_backup():
    env.host_string = 'remotehost'
    env.user = 'remoteuser'
    backup_fn = run('ls -t /usr/local/lib/backups | head -1')

it still attempts to run the ls command on localhost. I do I change this to run on remotehost?

like image 716
Cerin Avatar asked Dec 21 '22 15:12

Cerin


1 Answers

you could use the settings context manager to change the host that a specific command runs on, independent of the host setting for the enclosing task.

from fabric.context_managers import settings
with settings(host_string='remote_server'):
    run('ls -lart')
like image 196
iruvar Avatar answered Jan 10 '23 15:01

iruvar