navaz
navaz

Reputation: 124

Python Fabric run the commands on multiple machines

I am using python fabric to execute commands in parallel with multiple hosts.

I have following scenario.

def list1():
  env.hosts=[H1,H2,H3]
def myfunction():
   #login into H1 and execute a command and wait for certain string.
   #once get the string login to H2 and H3 in parallel and execute some other command

Now i have to run a command on H1 and expects for some output as soon as i get some desired string from H1 output while it is executing i need to run some other command on both H2 and H3. All are linux machines.

Running

fab -f fabfile.py -P list1 myfunction

like this is not the right way. Is there any way that i can achieve this ?

Thanks

Upvotes: 0

Views: 1724

Answers (1)

tobltobs
tobltobs

Reputation: 2929

I am not sure if I did understand what you want. I will assume, that you want to run a task on h1, wait for the result and use this result as an input for tasks on h2 and h3. If you want something else, forget this answer (and reword your question.)

One option would be to create a python script (not a fab file) which executes the fab tasks.

Something like:

import fabfile    
from fabric.tasks import execute

result = execute(fabfile.command_for_h1, hosts=[h1])
result_h1 = result[h1]
execute( fabfile.command_for_h2_h3, hosts=[h2,h3], result_h1 )

The key is the execute method.

Upvotes: 1

Related Questions