Batch remote execution of Fabric operations
Recently, you have to run commands on multiple machines in a cluster, such as starting or stopping services, and running scripts to collect data, so I found a framework Fabric of python. Fabric is a Python library used to simplify application deployment or system management tasks using SSH.
It provides the following functions: execute local or remote shell commands, upload/download files, and other auxiliary functions, such as prompting users to enter and abort execution.
Install
On my CentOS, run the following command to install
yum install http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpmyum install fabric.noarch
Test script
Create a fabfile. py file in the current directory. fabric always uses fabfile. py as its configuration file by default. Of course, you can also use-f to specify the configuration file to be used. The content is as follows:
def hello(): print("Hello fab!")
Run "fab hello" to obtain the following results:
Hello fab!
Next let's take a look at how parameters are passed
def hello(name): print 'Hello %s!'%name
Run "fab hello: name = fab". The following result is displayed:
Hello fab!
Perform local operations
from fabric.api import *def test(): local('cd /tmp') local('ls -l')
Perform remote operations
from fabric.api import *env.hosts=['kongxx@host1:22','kongxx@host2:22']env.password='Letmein'def test(): with('cd /tmp'): run('ls -l')
Env. hosts defines the list of machines to be remotely executed. env. password is the password for logging on to the remote machine.
Batch remote start/stop service
The basic commands are ready for use. Let's take a look at how to implement the functions I need. As I need to start services and execute commands in batches on more than 200 machines, all these machines are configured with password-free login, so you do not need to configure env. password
Machine list file
First, prepare a machine list file, for example, write all the machine names in the/tmp/hosts file, one machine name per line, similar to the following
host1host2host3...
Fabfile. py file
from fabric.api import *import osdef set_hosts(): f=open('/tmp/hosts', 'r') env.hosts=f.readlines() f.close()@parallel(pool_size=5)def start(): print("start service") prepare_hosts() run('/etc/init.d/myservice start')@parallel(pool_size=5)def stop(): print("stop service") prepare_hosts() run('/etc/init.d/myservice stop')@parallel(pool_size=5)def status(): print("check service status") prepare_hosts() run('/etc/init.d/myservice status')
Due to the large number of machines, "@ parallel (pool_size = 5)" is used to make fabric run concurrently, of course, you can also use the command line parameter to specify the concurrency mode "fab-P-z 5 ".
Run the following command to start, stop, and query statuses in batches.
fab set_hosts start|stop|status