Nish
Nish

Reputation: 415

Switching environments within a fabric script

I want to try to execute this

env.host_string = "server1.com"
with cd("/tmp"):
    run("some command)

#switch servers
env.host_string = "server2.com"

with cd("/home"):
    run("some other command")

The issue is the commands need to be executed sequentially and not in parrelel. I can't figure out a way to do this in fabric. I've tried with env("hostname"): but that does not work.

Upvotes: 0

Views: 36

Answers (2)

Kris Musard
Kris Musard

Reputation: 11

You can accomplish this using fabric's execute()

task_a():
  with cd("/tmp"):
    run("some command")

task_b():
  with cd("/home"):
    run("some other command")

task_c():
  execute(task_a,hosts=["server1.com"])
  execute(task_b,hosts=["server2.com"])

Upvotes: 1

Ben Graham
Ben Graham

Reputation: 2099

Use the @serial decorator to avoid parallel execution of tasks. You could also try the @hosts decorator (see the same page) to limit each task to a subset of hosts, and argue all hosts when you call Fabric.

Upvotes: 1

Related Questions