Tom Cornebize
Tom Cornebize

Reputation: 1432

How to use the get method with multiple hosts in fabric v2?

I have a fabfile that run several tasks on the hosts. This results in the creation of a file result.txt in each of the hosts.

Now I want to get all these files locally. This is what I tried:

from invoke import task
@task
def getresult(ctx):
    ctx.get('result.txt')

I run with:

fab -H host1 host2 host3 getresult

In the end, I have only one file result.txt in my local machine (it seems to be the copy from the last host of the command line). I would like to get all the files.

Is there a way to do this with fabric v2? I did not find anything in the documentation. It seems that this was possible in fabric v1, but I am not sure for the v2.

Upvotes: 1

Views: 367

Answers (1)

Chaitanya Sama
Chaitanya Sama

Reputation: 330

In Fabric v2, the get API signature is:

get(remote, local=None, preserve_mode=True)

So when you don't specify the name with which it has to be stored locally, it uses the same name it has in the remote location. Hence it is being overwritten for each host it executes on, and you're ending up with the last one.

One way to fix this would be to mention the local file name and add a random suffix or prefix to it. Something like this.

from invoke import task
import random

@task
def getresult(ctx):
    ctx.get('result.txt', 'result%s.txt' % random.random()*100)

This way, each time it executes, it stores the file with a unique name. You can even add the host name to the file, if you can find a way to use it inside the method.

Upvotes: 0

Related Questions