编辑脚步fabtest.py,输入
from fabric.api import *
env.roledefs = {
'slave1server':['root@slave1',],
'slave2server':['root@slave2',],
'slave3server':['root@slave3',]
}
#env.password = '123qwe!@#'
@roles('slave1server')
def task1():
run('/usr/zookeeper/bin/zkServer.sh stop')
run('/usr/zookeeper/bin/zkServer.sh start')
@roles('slave2server')
def task2():
run('/usr/zookeeper/bin/zkServer.sh stop')
run('/usr/zookeeper/bin/zkServer.sh start')
@roles('slave3server')
def task3():
run('/usr/zookeeper/bin/zkServer.sh stop')
run('/usr/zookeeper/bin/zkServer.sh start')
def dotask():
execute(task1)
execute(task2)
execute(task3)
fabric执行
[root@master ~]# fab -f fabtest.py dotask
[root@slave1] Executing task 'task1'
[root@slave1] run: /usr/zookeeper/bin/zkServer.sh stop
[root@slave1] out: ZooKeeper JMX enabled by default
[root@slave1] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave1] out: Stopping zookeeper ... STOPPED
[root@slave1] out:
[root@slave1] run: /usr/zookeeper/bin/zkServer.sh start
[root@slave1] out: ZooKeeper JMX enabled by default
[root@slave1] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave1] out: Starting zookeeper ... STARTED
[root@slave1] out:
[root@slave2] Executing task 'task2'
[root@slave2] run: /usr/zookeeper/bin/zkServer.sh stop
[root@slave2] out: ZooKeeper JMX enabled by default
[root@slave2] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave2] out: Stopping zookeeper ... STOPPED
[root@slave2] out:
[root@slave2] run: /usr/zookeeper/bin/zkServer.sh start
[root@slave2] out: ZooKeeper JMX enabled by default
[root@slave2] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave2] out: Starting zookeeper ... STARTED
[root@slave2] out:
[root@slave3] Executing task 'task3'
[root@slave3] run: /usr/zookeeper/bin/zkServer.sh stop
[root@slave3] out: ZooKeeper JMX enabled by default
[root@slave3] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave3] out: Stopping zookeeper ... STOPPED
[root@slave3] out:
[root@slave3] run: /usr/zookeeper/bin/zkServer.sh start
[root@slave3] out: ZooKeeper JMX enabled by default
[root@slave3] out: Using config: /usr/zookeeper/bin/../conf/zoo.cfg
[root@slave3] out: Starting zookeeper ... STARTED
[root@slave3] out:
Done.
Disconnecting from slave3... done.
Disconnecting from slave1... done.
Disconnecting from slave2... done.
[root@master ~]#