What does fabric mean by fabric?
Fabric is a powerful batch remote management and deployment tool in Python. It is often used to execute SSH tasks in batches on multiple remote PCs.
The common usage methods are summarized as follows:
1. First, write the tasks executed in batches to a fabfile. py file,
#-*-Coding: UTF-8-*-from fabric. api import run, local, roles, env, cdenv. hosts = ['2017. 168.1.110 ', '2017. 168.1.111 ', '2017. 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 local PC. # run is used to execute commands in a remote PC. def ls (): with cd ('/home/workspace/Project'): local ('touch 1. log ') with cd ('/home/workspace/project2 '): local ('touch 2. log ') # @ parallel, which can be set to parallel execution # @ serialdef pull (): with cd ('/home/workspace/Project'): run ('git pull ') def clean (): with cd ('/home/workspace/Project'): run ('bash clean. sh') @ hosts ('2017. 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, And the defined functions correspond to the executable commands in a fab.
There are two small notes,
A. In the run. sh of A remote machine, if you want to execute some non-common tools, you 'd better specify them as absolute paths and use nohup as appropriate.
B. It is best to add sleep after executing other scripts or commands to prevent Fabric from prematurely closing the session connected to the remote PC, resulting in task execution failure.
2. Execution Process: fabric execution selects the fabfile. py file in the current directory by default,
Fab clean
Fab pull
Fab robot: hosts = "192.168.1.115", device = 5560
You can pass the specified remote PC to fabric through the hosts parameter. The hosts parameter has a higher priority than env. hosts.
You can also pass parameters to commands in fab, such as device.
In addition, you can use fab-f otherFabFile. py clean to specify other fabric files.
If parallel execution is required, you can also pass parameters such as fab-P-z 15 pull, and 15 indicates the number of PCs in parallel execution.
The above, just some simple usage, if you need more advanced usage, you can pay attention to the github home page of the Project https://github.com/fabric/fabric.