Install Fabric
Pip Install Fabric
Ln-s/usr/local/python2.7/bin/fab/usr/bin/fab #创建软连接 (Create FAB as environment variable, no need to do this if the Python installation directory is not customized)
Test
Local: Rs1
Remote: rs2,ip:192.168.11.190;rs3,ip:192.168.11.20
First, basic usage (fab-l view available commands)
1.vim fabfile.py
def hello (): print ("Hello fab!") [Email protected] tmp]# Fab Hellohello fab! Done.
2. When the file name is not fabfile.py, you need to specify
MV fabfile.py Test.py[[email protected] tmp]# fab hellofatal error:couldn ' t find any fabfiles! Remember That-f can is used to specify Fabfile path, and use-h for help. Aborting. [Email protected] tmp]# fab-f test.py Hellohello fab! Done. [Email protected] tmp]# Fab hello-f Test.pyhello fab! Done.
3. Parameter passing
def hello (name): print ' Hello%s! ' %name
[Email protected] tmp]# Fab Hello:name=hyhello hy! Done. [Email protected] tmp]# Fab Hello:hyhello hy! Done.
Second, local operation locally
Vim fabfile.py
From FABRIC.API import localdef test (): local (' uname-a ')
Fab test
[localhost] local:uname-alinux rs1 2.6.32-504.el6.i686 #1 SMP Wed Oct 03:02:07 UTC i686 i686 i386 gnu/linuxdone.
Third, remote operation run
1. The command to execute is in the script
Vim fabfile.py
From FABRIC.API import rundef test (): run (' uname-a ')
Fab test
[[email protected]] Executing task ' test ' [[email protected]] Run:uname-a[[email protected]] Login password for ' root ': [[email protected]] O Ut:linux rs2 2.6.32-504.el6.i686 #1 SMP Wed Oct 03:02:07 UTC i686 i686 i386 gnu/linux[[email protected]] Out:don E.disconnecting from 192.168.11.190 ... Done.
2. Preach what commands you want to execute
From FABRIC.API import rundef Test (command): run
Fab test: ' uname-a '-h [email protected]
View Code
3. Omit-h when executing. The host is placed in the script.
View Code
Fab test: ' Uname-a '
View Code
4.host, the password is placed in the script, do not specify
View Code
Fab test: ' Uname-a '
View Code
More than one server
Same operation
Fab test: ' Uname-a
View CodeView Code
Multiple operations
| 12345 |
[[email protected] tmp]# fab -lAvailable commands: test test1 |
View Code
Fab test: ' Uname-a '
View Code
Fab test1: ' Uname-a '
[[Email protected]:22] Executing task ' test1 ' [[email protected]:22] Run:uname-a[[email protected]:22] out:linux rs3 2.6.32-504.el6.i686 #1 SMP Wed Oct 03:02:07 UTC i686 i686 i386 gnu/linux[[email protected]:22] out:servergroup2 execution complete, please instruct done.disconnecting F Rom 192.168.11.20 ... Done.
Fabric Deployment Detailed