Batch remote management and deployment tools in fabric---python

Source: Internet
Author: User

Fabric is a very powerful batch remote management and deployment tool in Python that is often used to perform SSH tasks in batches on multiple remote PCs.

The common use methods are summarized as follows:

1, first, to write a batch of tasks into a fabfile.py,

#-*-Coding:utf-8-*-from fabric.api import Run, local, roles, env, cdenv.hosts=[    ' 192.168.1.110 ',    ' 192.168.1.111 ',    ' 192.168.1.112 ']env.user= "username" env.password= "password" env.port=22#env.parallel=true# env.skip_bad_hosts=true#env.timeout=1#env.warn_only=true# Local is used to execute commands on the native PC. # Run is used to execute commands on the remote PC. Def ls (): With    CD ( '/home/workspace/project '):        local (' Touch 1.log ') with    CD ('/home/workspace/project2 '):        local (' Touch 2. Log ') # @parallel, you can set whether to execute # @serialdef pull () in parallel: With    CD ('/home/workspace/project '):        run (' git pull ') def clean (): With    CD ('/home/workspace/project '):        run (' Bash clean.sh ') @hosts (' 192.168.1.113 ') def Robot (device): With    CD ('/home/workspace/project '):        run (' bash run.sh%s robot && sleep 1 '% device)
The above is a simple fabfile.py, where the function defined corresponds to an executable command in a fab.

There are two small caveats,

A, in the remote machine's run.sh, if you want to perform some non-system common tools, it is best to specify the absolute path. And can be used appropriately in the Nohup way.

B, it is best to add sleep after executing other scripts or commands to prevent fabric from prematurely shutting down the session connected to the remote PC, causing the task to fail.

2, execution process: Fabric execution will select the fabfile.py file under the current directory by default,

Fab clean

Fab pull

Fab robot:hosts= "192.168.1.115", device=5560

The hosts parameter allows you to pass in the specified remote PC to the fabric, which has a higher priority than env.hosts.

You can also pass parameters, such as device, to commands in the fab.

In addition, additional fabric files can be specified by Fab-f otherfabfile.py clean.

Above, just some simple usage, if you need more advanced usage, you can follow the project's GitHub homepage https://github.com/fabric/fabric.




Batch remote management and deployment tools in fabric---python

Related Article

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.