Amir Rossert
Amir Rossert

Reputation: 1053

python fabric multiple commands

I'm trying to understand how fabric is working with multiple commands on one machine. I need to run several commands on each host (thousands of hosts) and would like to know what would be best.

Using multiple runs:

res_1 = run(command_1)
res_2 = run(command_2)
...
...
res_n = run(command_n)

Or:

res = run(command_1 && command_2 && ... command_n)
res.splitlines()
res_1 = res[0]
res_2 = res[1]
...
...
res_n = res[n-1]

What I want to know is how fabric handles multiple runs, will it open multiple sessions or do all commands in the same session?

Upvotes: 1

Views: 5228

Answers (2)

e4c5
e4c5

Reputation: 53734

Regardless of whether you use multiple run calls or a single run call with &&, AFAIK, fabric will open only one network connection. The difference between the two is that each new run executes in a different environment. For example you can try this.

run('ls')
run('cd /tmp/')
run('ls')

Both times it will show you a listing of your home directory. But if you try this

run('ls')
run('cd /tmp/ && ls')

It will show you your home directory the first time, followed by a listing of /tmp/. Thus if you want the state to be preserved from one command to another you should do run('cmd1 && cmd1') but if you are not bothered about it, you should use multiple run calls.

Upvotes: 5

Błotosmętek
Błotosmętek

Reputation: 12927

The important difference between those two approaches is that the first one will run all commands regardless of the exit status of former commands; the second one, however, will only execute commmand_2 if command_1 returned no error, etc. So it is up to you if you want this behaviour or the other.

Upvotes: 1

Related Questions