Background: Fabric primarily executes remote shell commands, including file uploads, downloads, and other ancillary functions that prompt user input.
Test system: UBUNTU16
Requirements: Python//system has its own, ubuntu16 usually comes with python2.7.5
First, install the fabric
1. Install Fabric
$ sudo apt-get install python---upgrade pip // upgrade pip$ sudo pip install fabric
2. View the installation results
$ which fab
Second, the use of fabric
1. Write Hello script: vim/tmp/fab/hello.py
1.1 Script Content
# !/usr/bin/env python def hello ():
Print ("Hello world! ")
1.2 Running files and output results
- F hello.py Hello----------------------------------------->Helloworld! Done .
2. Parameter passing: vim/tmp/fab/test1.py
2.1 Script Content
# !/usr/bin/env python def Hello (name): %s! '% name)
2.2 Running files and output results
- F test01.py Hello:jeff
-------------------------------------->
Hello, Jeff! Done .
3. Local operation
3.1 vim/tmp/fab/lslocal.py
# !/usr/bin/env python From fabric.api import localdef lslocal (): /usr/Local ') local (' ls ')
3.2 Running files and output results
- F local.py lslocal
/usr/local && ls
Bin etc Games include Lib man sbin share srcdone.
4. Remote operation
4.1 Script Content
# !/usr/bin/env python # -*-coding:utf-8-*- = { ' server1 ': [' root@172.16.1.201:22',], ' server2 ': [' zun1@172.16.1.206:22 = ' 123456'@roles (' Server1 ') def task1 (): /usr/local && ls ')@ roles (' Server2 ') def task2 (): /home/zun1 && ls ') def Test (): execute (TASK1) execute (TASK2)
4.2 Running files and output results
$ fab- Ftest01.py Test------------------------------------------------------------------------------------>[Root@172.16.1.201:22] Executing task ' task1 ' [root@172.16.1.201:22] Run:cd/usr/local &&Ls[root@172.16.1.201:22] Out:bin etc games include Lib Lib64 libexec Sbin share Src[root@172.16.1.201:22] Out: [Zun1@172.16.1.206:22] Executing task ' task2 ' [zun1@172.16.1.206:22] Run:cd/home/zun1 &&ls[zun1@172.16.1.206:22] Out:test_fabric walle-web.vendor.tgz[zun1@172.16.1.206:22] out:Done.Disconnecting from Root@172.16.1.201... done . Disconnecting from172.16.1.206 ... done.
The above only for the fabric to do a general demonstration, if you want to further research, you can view the official website tutorial.
Installation and use of Python fabric