Batch remote execution of Fabric operations

Source: Internet
Author: User

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.rpm
Yum 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! ") 1
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

Host1
Host2
Host3
... 1
Fabfile. py file
From fabric. api import *
Import OS

Def 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

Fabric automated O & M tools for remote SSH deployment and system management

Deploy website applications using Fabric

Use Vagrant and Fabric for Integration Testing

Fabric: Python Remote Deployment Tool

Install Python-Pip and Fabric in CentOS/Ubuntu

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.