fabric automation deployment python

1. Install python-devel yum install python-devel

2. pip python installation management
       a. Download and install the setuptools package curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
            python ez_setup.py
       b , Download and install the pip package curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
             python get-pip.py

3. Install fabric pip install fabric
          If there is an abnormal encode setting/ usr/bin/pip add sys.setdefaultencoding('utf8')

http://docs.fabfile.org/en/1.4.0/index.html
Fabric is a Python library that can batch execute tasks on multiple hosts via SSH . You can script tasks and then automate them locally using SSH on a large number of remote servers via Fabric. These capabilities are ideal for automating deployment of applications, or performing system administration tasks.
Let's first look at an example. We know that under *NIX, the uname command is to view the distribution of the system. You can write a Fabric script like this:
from fabric.api import run
def host_type():
    run('uname -s')


=========================================== ==========

Python fabric practical operation
After learning the theory, it's time to practice. Two machines: 10.1.6.186, 10.1.6.159. Fabric is deployed on 10.1.6.186.

1 Execute a simple task to display files in the /home/guol/ directory of both machines
#!/usr/bin/python
from fabric.api import *
from fabric.context_managers import *


env.hosts=['10.1.6.186','10.1.6.159']
env.password='xxxxxx'


def task1():
    with cd ('/home/guol'):
        run('ls -l')


##result
root@vm11:/tmp# fab task1
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:

[10.1.6.159] Executing task 'task1'
[10.1.6.159] run: ls -l
[10.1.6.159] out: total 0
[10.1.6.159] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 159-remote
[10.1.6.159] out:


Done.
Disconnecting from 10.1.6.159... done.
Disconnecting from 10.1.6.186... done.


2 Perform the same task as 1, but exclude the machine 10.1.6.159
#!/usr/bin/python
from fabric.api import *
from fabric.context_managers import *


env.hosts=['10.1.6.186','10.1.6.159']
env.password='xxxxxx'

env.exclude_hosts=['10.1.6.159']


def task1():
    with cd ('/home/guol'):
        run('ls -l')


##implement
root@vm11:/tmp# fab task1
[10.1.6.186] Executing task 'task1'
[10.1.6.186] run: ls -l
[10.1.6.186] out: total 0
[10.1.6.186] out: -rw-r--r-- 1 root root 0 Dec 21 13:32 186-local
[10.1.6.186] out:




Done.
Disconnecting from 10.1.6.186... done.


For more, please see: http://www.fabfile.org/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522585&siteId=291194637