When using shell commands for complex operations, the code tends to be complex and difficult to understand, and the Python scripting language is the equivalent of developing a common application, so maintenance and expansion are simpler, More importantly, the Python Ops tool fabric can automatically log on to other servers for a variety of operations, which is hard to do with the shell, but using the fabric implementation is simple, so it is recommended to use Python to write scripts for daily operational deployment of programmers
Linux is the default Python environment, there are two ways to install fabric is PIP directly installed, the other is to download the fabric source code, Use the Setuptools tool to perform setup.py installation, both ways need to network, basically almost, here using Pip to install, when installed when error, generally because the Python-devel module is not installed, need to install, you can use the RPM in advance -qa | grep Python-devel to see if Python-devel is installed, if not installed, use Yum to install the following:
Yum Install Python-devel
PIP installation said before, first download the py file: https://bootstrap.pypa.io/get-pip.py, uploaded to the server after the execution: python get-pip.py install pip
Install fabric with PIP: pip Install fabric waits a little while to install, and the input fab pops up the corresponding option
Next, write a simple Python script fabrictest.py to do the testing:
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 4 fromFabric.apiImport*5 6 #Setting Server login Parameters7Env.roledefs = {8 #A group that performs the same action in a consistent set of actions9 'servers1':['[Email protected]:22',],Ten #Second group One 'servers2':['[Email protected]:22',] A } - - #native Operation the deflocaltask (): -Local'/usr/local/nginx/nginx') - - #servers1 Server Group Operations +@roles ('servers1') - defTask1 (): +Run'/usr/local/tomcat/bin/startup.sh') A at #servers2 Server Group Operations -@roles ('servers2') - defTask2 (): -Run'/usr/local/tomcat/bin/startup.sh') - - #Perform Tasks in defDoworks (): - Execute (localtask) to Execute (TASK1) +Execute (TASK2)
The above code, is simple to start the Nginx server locally, in Linux1 and LINUX2 launched the Tomcat server, in order to accept the proxy of Nginx server, here is dedicated to use the grouping method in order to adapt to the machine more than the need of the cluster, and there is no server password set here, One is for the security of the server, in addition to the cluster recommended to set up SSH password-free login, the script does not have to set passwords, the method Doworks is the final summary of the task, after saving, began to execute:
Fab-f fabrictest.py Doworks
After execution, the script automatically executes the specified command sequentially, the output can be seen from the console, or the output can be added in the appropriate location of the program to print some necessary information
Here, we see that the functionality of the fabric tool is really powerful, and more programming interfaces can be found on the website's documentation: http://www.fabfile.org/
Installation of the Python Automated operations tool fabric