Guide for installing and using Fabric, an automated deployment module of Python, pythonfabric
Fabric is a python2.5 or higher library. You can execute tasks in batches on multiple hosts through ssh. complete system management tasks. it provides a set of basic operations to execute shell commands locally and remotely, or upload and download files to help users input or terminate execution.
The following two methods are available to install the fabric module:
1. Use easy_install (the debain5 environment is shown below)
Root@10.1.6.200: pshell # apt-get install python-dev (install Python header files) root@10.1.6.200: pshell # apt-get install python-setuptools (install easy_install) root@10.1.6.200: pshell # wget http://peak.telecommunity.com/dist/ez_setup.py root@10.1.6.200: pshell # python ez_setup.py root@10.1.6.200: pshell # easy_install fabric
Searching for fabricReading http://pypi.python.org/simple/fabric/Best match: Fabric 1.6.1Downloading http://pypi.python.org/packages/source/F/Fabric/Fabric-1.6.1.tar.gz#md5=c318ac3f7011ede0be1ca9a20f435735Processing Fabric-1.6.1.tar.gzRunning Fabric-1.6.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-CVuLrs/Fabric-1.6.1/egg-dist-tmp-ZFNoWYwarning: no previously-included files matching '*' found under directory 'docs/_build'warning: no previously-included files matching '*.pyc' found under directory 'tests'warning: no previously-included files matching '*.pyo' found under directory 'tests'zip_safe flag not set; analyzing archive contents...fabric.version: module references __file__Adding Fabric 1.6.1 to easy-install.pth fileInstalling fab script to /usr/bin....Installed /usr/lib/python2.5/site-packages/pycrypto-2.6-py2.5-linux-x86_64.eggFinished processing dependencies for fabric
2. Use pip (the debian7 environment is used below)
apt-get install python-pippip install fabricapt-get install python-paramiko
If no error is reported in the import module, the installation is successful.
Instance:
1. When fabric is called, use the command line parameter-H to specify which host
root@10.1.6.201:python# cat fabfile4.py
#!/usr/bin/env python#coding=utf-8from fabric.api import * def printMem(): cmd_output = run('free -m') print cmd_output
Root@10.1.6.201: python # fab-H root@10.1.1.45 printMem-f fabfile4.py [root@10.1.1.45] Executing task 'printmem '[root@10.1.1.45] run: free-m [root@10.1.1.45] Login password for 'root ': # prompt enter password [root@10.1.1.45] out: total used free shared buffers cached [root@10.1.1.45] out: Mem: 1005 968 37 0 36 831 [root@10.1.1.45] out:-/+ buffers/cache: 100 904 [root@10.1.1.45] out: Swap: 1913 0 1913 [root@10.1.1.45] out:
total used free shared buffers cachedMem: 1005 968 37 0 36 831-/+ buffers/cache: 100 904Swap: 1913 0 1913 Done.Disconnecting from 10.1.1.45:22000... done.
2. We need to enter a password to complete the operation. How can we perform the operation automatically? You can configure the HOST in the fabfile4 file, that is, the environment variable.
root@10.1.6.201:python# vim fabfile.py
#! /Usr/bin/env python # coding = utf-8from fabric. api import * from fabric. context_managers import * env. host_string = '10. 1.1.45 'env. port = '000000' # default port 22, Default Login User rootenv. password = 'passwd' def test1 (): with cd ('/home'): run ('LS-l') test1 ()
Root@10.1.6.201: python # python fabfile. py # Script Execution
[10.1.1.45] run: ls -l[10.1.1.45] out: total 8[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian[10.1.1.45] out: drwxr-xr-x 2 root nogroup 4096 2013-05-22 18:07 ftp[10.1.1.45] out:
root@10.1.6.201:python# vim fabfile1.py
#! /Usr/bin/env python # coding = utf-8from fabric. api import * from fabric. context_managers import * env. hosts = ['10. 1.6.200 ', '10. 1.1.45'] env. port = '000000' env. password = 'passwd' def test1 (): with cd ('/home'): # change the directory run ('LS-l ')
Root@10.1.6.201: python # fab test1-f fabfile. py # Use fab to specify the task to execute, note that the default file with fabfile. py
[10.1.6.200] Executing task 'test1'[10.1.6.200] run: ls -l[10.1.6.200] out: total 24[10.1.6.200] out: drwxr-xr-x 2 davehe davehe 4096 2013-02-27 10:00 davehe[10.1.6.200] out: -rw-r--r-- 1 root root 1990 2013-02-27 09:55 davehe.tar.gz[10.1.6.200] out: -rw-r--r-- 1 root root 396 2013-05-17 18:27 rsync_log_130517[10.1.6.200] out: -rw-r--r-- 1 root root 7916 2013-05-20 21:04 rsync_log_130520[10.1.6.200] out: drwxr-xr-x 2 taomee taomee 4096 2013-01-29 04:27 taomee[10.1.6.200] out: [10.1.1.45] Executing task 'test1'[10.1.1.45] run: ls -l[10.1.1.45] out: total 8[10.1.1.45] out: drwxr-xr-x 2 debian debian 4096 2012-08-27 11:54 debian[10.1.1.45] out: drwxr-xr-x 2 root nogroup 4096 2013-05-22 18:07 ftp[10.1.1.45] out: Done.Disconnecting from 10.1.1.45:22000... done.Disconnecting from 10.1.6.200:22000... done.
3. Use get/put. to upload and download files using sftp protocol
root@10.1.6.201:python# cat fabfile1.py
#! /Usr/bin/env python # coding = utf-8from fabric. api import * from fabric. colors import * from fabric. context_managers import * env. hosts = ['10. 1.1.45'] env. port = '000000' env. password = 'passwd' def test1 (): print (red ("I'm 201") local ('LS-l/tmp ') def test2 (): print (green ("I'm get file 45 to 186") get ('/home/ftp/a.txt', '/tmp /') # download # put ('/tmp/', '/home/ftp/') # upload local ('LS-l/tmp ') # Run local command def final (): execute (test1) execute (test2)
root@10.1.6.201:python# fab final -f fabfile1.py
[10.1.1.45] Executing task 'final'[10.1.1.45] Executing task 'test1'i'm 201[localhost] local: ls -l /tmptotal 31684drwxr-xr-x 2 root root 4096 May 13 22:08 bindrwxr-xr-x 3 root root 4096 May 13 22:08 confdrwxr-xr-x 6 root root 4096 May 13 22:08 etc-rwxr-xr-x 1 root root 6797 May 13 22:08 init-rw-r--r-- 1 root root 32400896 May 13 22:07 initrd.img-3.2.0-4-amd64drwxr-xr-x 6 root root 4096 May 13 22:08 libdrwxr-xr-x 2 root root 4096 May 13 22:08 lib64drwxr-xr-x 2 root root 4096 May 13 22:08 rundrwxr-xr-x 2 root root 4096 May 13 22:08 sbindrwxr-xr-x 6 root root 4096 May 13 22:08 scripts[10.1.1.45] Executing task 'test2'i'm get file 45 to 186[10.1.1.45] download: /tmp/a.txt <- /home/ftp/a.txt[localhost] local: ls -l /tmptotal 31688-rw-r--r-- 1 root root 6 May 29 22:29 a.txtdrwxr-xr-x 2 root root 4096 May 13 22:08 bindrwxr-xr-x 3 root root 4096 May 13 22:08 confdrwxr-xr-x 6 root root 4096 May 13 22:08 etc-rwxr-xr-x 1 root root 6797 May 13 22:08 init-rw-r--r-- 1 root root 32400896 May 13 22:07 initrd.img-3.2.0-4-amd64drwxr-xr-x 6 root root 4096 May 13 22:08 libdrwxr-xr-x 2 root root 4096 May 13 22:08 lib64drwxr-xr-x 2 root root 4096 May 13 22:08 rundrwxr-xr-x 2 root root 4096 May 13 22:08 sbindrwxr-xr-x 6 root root 4096 May 13 22:08 scripts Done.Disconnecting from 10.1.1.45:22000... done.
The preceding examples only list several common farbic environment variables, such as env. hosts and env. password. You do not need to enter the password for interaction.
The following are common environment variables for your reference:
- Exclude_hosts: Specifies a host list. during fab execution, the machines in the list are ignored.
- User: the user used by ssh to log on to the remote host.
- Hosts: global host list
- Host_string: user/host/port set when fabric is connected to a remote machine for run or put.
- Password: the password used to connect to the remote host through ssh by default, or the password is prompted by sudo.
- Password: a dictionary for internal use. Set a password for each host. The key is the host and the value is the password.
- Port: Set the default port
- Roledefs: Define the Host ip corresponding to the role name using the dictionary
- Roles: a global role list
from fabric.api import run, roles env.roledefs = { 'db': ['db1', 'db2'], 'web': ['web1', 'web2', 'web3'],} @roles('db')def migrate(): # Database stuff here. pass @roles('web')def update(): # Code updates here. pass
Fab can also use commands to set environment variables. Common commands
- -F FABFILE, -- fabfile = FABFILE default fabfile. py
- -H hosts, -- hosts = HOSTS env. hosts = hosts
- -P PASSWORD, -- password = PASSWORD env. password
- -R roles, -- roles = ROLES env. roles
Articles you may be interested in:
- Simple deployment of Fabric in Python
- Python fabric usage notes
- Example of batch remote management and deployment tool Fabric in Python
- Python fabric remote operations and deployment examples