Introduction to Python Fabric Remote automatic deployment

Source: Internet
Author: User

Fabric is a python (2.5-2.7) library that simplifies application deployment or system administration tasks that use SSH.

It provides actions such as executing local or remote shell commands, uploading/downloading files, and other accessibility features such as prompting for user input, aborting execution, and so on.

This article focuses on the basic methods of automating deployment using fabric on CentOS 6.3.

1. Environment deployment

This section focuses on Python version upgrades, Pip and fabric installation methods.

1.1 Python Version upgrade

CentOS 6.3 comes with a python version of 2.6 and needs to be upgraded to version 2.7 first. Because older versions of Python are deeply dependent, you cannot uninstall the original Python, only the new installation.

1. Download python, select gzipped source tarball,:https://www.python.org/ftp/python/2.7.14/python-2.7.14.tgz

2. Unzip the installation, the command is as follows

$ tar-xvf Python-2.7.14. tgz$ cd Python-2.7.14$. /configure--prefix=/usr/local/python2.7$ make$ make install

3. Create a link to change the system default Python to python2.7

$ ln-fs/usr/local/python2.7/bin/python2.7/usr/bin/python

4. View the Python version

$ python–v

5. Modify the Yum configuration (otherwise Yum will not work)

$ vi/usr/bin/yum

Modify the #!/usr/bin/python of the first line to the original Python version address of the system #!/usr/bin/python2.6

The CentOS 6.3 System Python has been successfully upgraded to the latest version 2.7.14.

1.2 Installing PIP

Pip is a tool for installing and managing Python packages.

The installation method is as follows:

1. Download Pip, Address: https://raw.github.com/pypa/pip/master/contrib/get-pip.py

2. Execute the Install command

$ python get-pip.py

3. Create the connection (otherwise the error message "command does not exist")

$ ln-s/usr/local/python2.7/bin/pip/usr/bin/pip
1.3 Installing the Fabric

1. Execute the Install command

$ pip Install Fabric

2. Create the connection (otherwise the error message "command does not exist")

$ ln-s/usr/local/python2.7/bin/fab/usr/bin/fab
2. Fabric Scripting

This section provides a brief introduction to fabric usage and offers examples for reference.

2.1 Hello,fab

1. In the current directory to create a new file fabfile.py, enter the following:

def Hello ():     Print ("Hello fab! ")

2. Execute the command fab hello with the following result:

$ fab Hellohello fab!

3. When the file name is not fabfile.py, you use-F to specify:

$ mv fabfile.py test.py$ Fab hellofatal error:couldn ' t find any fabfiles!  and for-f test.py Hellohello fab!

4. Parameter passing

Using VI fabfile.py, modify the fabfile.py:

def Hello (name):     Print ' Hello%s! '%name

There are two ways to pass parameters:

$ fab hello:name=fabhello fab!$ fab hello:fabhello fab!
2.2 Local operation

To perform a local operation command using the

1. fabfile.py script content is as follows

 from Import Local def Test ():    local('cd/home/')    local ('ls-l| Wc-l')

2. Execute the command fab test with the following results

/home/-L|WC-L8
2.3 Remote operation

Execute remote operations Command using Run

1. fabfile.py script content is as follows

 from  fabric.api import   cd,run,env,hostsenv.hosts  =[ "  , "  192.168.85.101:22   " ]env.password  = " test    

2. Execute the command fab test with the following results

$ fab test[192.168.85.99:22] Executing task'Test'[192.168.85.99:22] Run:du-sh[192.168.85.99:22] out:392g. [192.168.85.99:22] out:[192.168.85.101:22] Executing task'Test'[192.168.85.101:22] Run:du-sh[192.168.85.101:22] Out:5. 6G. [192.168.85.101:22] Out:disconnecting from192.168.85.99... done . Disconnecting from192.168.85.101 ... done.

3. Multi-server hybrid, you need to do different operations on different servers, you can refer to the following script

 fromFabric.apiImportEnv,roles,run,executeenv.roledefs= {'Server1': ['[Email protected]:22',],'Server2': ['[Email protected]:22', ]} Env.password='Test'@roles ('Server1')defTask1 (): Run ('ls/home/-L | wc-l') @roles ('Server2')defTask2 (): Run ('Du-sh/home')defTest (): Execute (TASK1) execute (TASK2)

The results are as follows:

$ fab test[[email protected]192.168.85.99:22] Executing task'Task1'[[Email protected]192.168.85.99:22] run:ls/home/-L | WC-L[[email protected]192.168.85.99:22] Out:27[[Email protected]192.168.85.99:22] Out:[[email protected]192.168.85.100:22] Executing task'Task2'[[Email protected]192.168.85.100:22] Run:du-sh/Home[[email protected]192.168.85.100:22] OUT:1.4G/Home[[email protected]192.168.85.100:22] Out:disconnecting from192.168.85.99... done . Disconnecting from192.168.85.100 ... done.
3. Reference

Above is just a brief introduction to the Python+fabric Automatic deployment scripting method, in the actual application process according to the specific needs of the corresponding script can refer to the following articles:

1. http://docs.fabfile.org/en/latest/index.html

2. http://wklken.me/posts/2013/03/25/python-tool-fabric.html

Introduction to Python Fabric Remote automatic deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.