Rohit
Rohit

Reputation: 4178

how to get host being processed from fabric2 group

I wanted to try fabric2 for some of my tasks which required running multiple commands on multiple hosts. So i used threadingGroup since i wanted to do it concurrently. Here is below code

 with fabric.ThreadingGroup(*hosts) as tg:    
    try:                                     
        tg.run('uptime')
        tg.run('last -xF| grep boot')

Now i need to know at one point which host is being processed at one time by fabric because i need to do some processing on the host based on the result of second run command. I tried to goggle a lot on this but could not find much on this. Even fabric documentation has very little about groups in general.

I can achieve this if i am willing to sacrifice concurrency like below.

 def runner(cxn):                                         
     try:                                                 
         cxn.run('uptime')                                
         reboots = cxn.run("last -xF|grep boot")                     
     except Exception:                                    
         pass                                             


 for c in Group(*hosts):                                  
     runner(c)                                            

But this approach defeats the whole approach of using a threadingGroup as this does not run in parallel and executes sequentially.

So is there a way to achieve this with fabric2 or should i stick with fabric1 only.

Upvotes: 1

Views: 463

Answers (1)

2ps
2ps

Reputation: 15936

The GroupResult from the run operation will tell you the output of the command on each host.

for example:


with fabric.ThreadingGroup(*hosts) as tg:    
    try:                                     
        tg.run('uptime')
        results = tg.run('last -xF| grep boot')
        for connection, result in results.items():
            if connection.host == 'host1':
                # do some stuff here

Upvotes: 2

Related Questions