Python's bulk remote administration and Deployment Tools Fabric Usage Example _python

Source: Internet
Author: User

This article illustrates the Python bulk remote administration and deployment tools fabric usage. Share to everyone for your reference. Specifically as follows:

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

Common methods of use are summarized as follows:

1. First, to write a batch-executed task into a fabfile.py,

Copy Code code as follows:
#-*-Coding:utf-8-*-

From FABRIC.API import run, local, roles, Env, CD
env.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 a 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 in parallel
# @serial
Def pull ():
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 defined functions correspond to an executable command in a fab.
There are two small things to note:

A. In the run.sh of a remote machine it is best to specify an absolute path if you want to perform some tools that are not common to the system. And you can use the Nohup method appropriately.

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

2. Implementation process: Fabric execution Selects the fabfile.py file in the current directory by default.

Copy Code code as follows:
Fab clean
Fab pull
Fab robot:hosts= "192.168.1.115", device=5560

You can pass the hosts parameter to the specified remote PC for the fabric, which has a higher priority than env.hosts.

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

In addition, you can specify additional fabric files by fab-f otherfabfile.py clean.

If you need to execute in parallel, you can also pass parameters such as Fab-p-Z Pull, 15 for the number of PCs that are executed in parallel.

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

I hope this article will help you with your Python programming.

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.